|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
import sys |
|
5
|
|
|
import optparse |
|
6
|
|
|
import tilda |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def parse_options(args=None, values=None): |
|
10
|
|
|
usage = "%prog [options] PUBLIC_KEY SECRET_KEY" |
|
11
|
|
|
desc = "A Python implementation of Tilda.cc API." |
|
12
|
|
|
|
|
13
|
|
|
parser = optparse.OptionParser(usage=usage, description=desc) |
|
14
|
|
|
parser.add_option('--projects', action='store_true', dest='projects', help='Get projects list') |
|
15
|
|
|
parser.add_option('--project', type='int', dest='project', metavar='PROJECT_ID', help='Get project info') |
|
16
|
|
|
parser.add_option('--pages', type='int', dest='pages', metavar='PROJECT_ID', help='Get pages list') |
|
17
|
|
|
parser.add_option('--page', type='int', dest='page', metavar='PAGE_ID', help='Get page info') |
|
18
|
|
|
parser.add_option('-o', '--output', dest='output_dir', metavar='DIR', help='Export to DIR') |
|
19
|
|
|
(options, args) = parser.parse_args(args, values) |
|
20
|
|
|
|
|
21
|
|
|
if len(args) != 2: |
|
22
|
|
|
print 'ERROR: No PUBLIC_KEY and SECRET_KEY' |
|
23
|
|
|
sys.exit(2) |
|
24
|
|
|
|
|
25
|
|
|
return args, options |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def export_page(page, output_dir): |
|
29
|
|
|
filepath = output_dir + '/' + page.filename |
|
30
|
|
|
with open(filepath, 'w') as f: |
|
31
|
|
|
print('- Write file %s' % filepath) |
|
32
|
|
|
f.write(page.html.encode('utf-8')) |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
def run(): |
|
36
|
|
|
keys, options = parse_options() |
|
37
|
|
|
|
|
38
|
|
|
api = tilda.Client(public=keys[0], secret=keys[1]) |
|
39
|
|
|
|
|
40
|
|
|
if options.projects: |
|
41
|
|
|
print('[Get projects list]') |
|
42
|
|
|
for project in api.get_projects_list(): |
|
43
|
|
|
print('- (%s) %s' % (project.id, project.title)) |
|
44
|
|
|
|
|
45
|
|
|
if options.project: |
|
46
|
|
|
print('[Get project info]') |
|
47
|
|
|
project = api.get_project_export(options.project) |
|
48
|
|
|
print('- (%s) %s' % (project.id, project.title)) |
|
49
|
|
|
print('-- %s' % (project.descr)) |
|
50
|
|
|
|
|
51
|
|
|
if options.pages: |
|
52
|
|
|
print('[Get pages list]') |
|
53
|
|
|
for page in api.get_pages_list(options.pages): |
|
54
|
|
|
print('- (%s) %s' % (page.id, page.title)) |
|
55
|
|
|
|
|
56
|
|
|
if options.page: |
|
57
|
|
|
print('[Get page info]') |
|
58
|
|
|
page = api.get_page_full(options.page) |
|
59
|
|
|
print('- (%s) %s' % (page.id, page.title)) |
|
60
|
|
|
|
|
61
|
|
|
if options.output_dir: |
|
62
|
|
|
print('[Export to %s]' % options.output_dir) |
|
63
|
|
|
export_page(page, options.output_dir) |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
if __name__ == '__main__': |
|
67
|
|
|
run() |
|
68
|
|
|
|