|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (c) 2016 dotzero <[email protected]> |
|
4
|
|
|
# |
|
5
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
# in the Software without restriction, including without limitation the rights |
|
8
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
# furnished to do so, subject to the following conditions: |
|
11
|
|
|
# |
|
12
|
|
|
# The above copyright notice and this permission notice shall be included |
|
13
|
|
|
# in all copies or substantial portions of the Software. |
|
14
|
|
|
# |
|
15
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
# SOFTWARE. |
|
22
|
|
|
|
|
23
|
1 |
|
import json |
|
24
|
|
|
|
|
25
|
1 |
|
try: |
|
26
|
|
|
# python3 |
|
27
|
1 |
|
from urllib.request import urlopen |
|
28
|
1 |
|
from urllib.error import URLError |
|
29
|
1 |
|
from urllib.parse import urlencode |
|
30
|
|
|
except ImportError: |
|
31
|
|
|
# python2 |
|
32
|
|
|
from urllib2 import urlopen, URLError |
|
33
|
|
|
from urllib import urlencode |
|
34
|
|
|
|
|
35
|
1 |
|
from tilda.project import TildaProject |
|
36
|
1 |
|
from tilda.page import TildaPage |
|
37
|
1 |
|
from tilda.error import TildaError, NetworkError |
|
38
|
|
|
|
|
39
|
1 |
|
ENDPOINT = 'http://api.tildacdn.info/v1' |
|
40
|
1 |
|
STATUS_OK = 'FOUND' |
|
41
|
1 |
|
STATUS_ERR = 'ERROR' |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
1 |
|
class Client(object): |
|
45
|
1 |
|
def __init__(self, public, secret): |
|
46
|
1 |
|
self.public = public |
|
47
|
1 |
|
self.secret = secret |
|
48
|
|
|
|
|
49
|
1 |
|
def _request(self, method, params=None): |
|
50
|
1 |
|
payload = { |
|
51
|
|
|
'publickey': self.public, |
|
52
|
|
|
'secretkey': self.secret |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
if params is not None: |
|
56
|
1 |
|
payload.update(params) |
|
57
|
|
|
|
|
58
|
1 |
|
try: |
|
59
|
1 |
|
url = ENDPOINT + method + '?' + urlencode(payload) |
|
60
|
1 |
|
response = urlopen(url) |
|
61
|
1 |
|
except URLError as e: |
|
62
|
1 |
|
response = e |
|
63
|
|
|
|
|
64
|
1 |
|
try: |
|
65
|
1 |
|
json_data = response.read() |
|
66
|
1 |
|
data = json.loads(json_data.decode('utf-8')) |
|
67
|
|
|
except ValueError as e: |
|
68
|
|
|
raise NetworkError('Invalid server response') |
|
69
|
|
|
|
|
70
|
1 |
|
if data.get('status') != STATUS_OK: |
|
71
|
1 |
|
raise TildaError(data) |
|
72
|
|
|
|
|
73
|
1 |
|
return data.get('result') |
|
74
|
|
|
|
|
75
|
1 |
|
def get_projects_list(self): |
|
76
|
|
|
""" Get projects list """ |
|
77
|
1 |
|
try: |
|
78
|
1 |
|
result = self._request('/getprojectslist/') |
|
79
|
1 |
|
return [TildaProject(**p) for p in result] |
|
80
|
|
|
except NetworkError: |
|
81
|
|
|
return [] |
|
82
|
|
|
|
|
83
|
1 |
|
def get_project(self, project_id): |
|
84
|
|
|
""" Get project info """ |
|
85
|
1 |
|
try: |
|
86
|
1 |
|
result = self._request('/getproject/', |
|
87
|
|
|
{'projectid': project_id}) |
|
88
|
1 |
|
return TildaProject(**result) |
|
89
|
|
|
except NetworkError: |
|
90
|
|
|
return [] |
|
91
|
|
|
|
|
92
|
1 |
|
def get_project_export(self, project_id): |
|
93
|
|
|
""" Get project info for export """ |
|
94
|
1 |
|
try: |
|
95
|
1 |
|
result = self._request('/getprojectexport/', |
|
96
|
|
|
{'projectid': project_id}) |
|
97
|
1 |
|
return TildaProject(**result) |
|
98
|
|
|
except NetworkError: |
|
99
|
|
|
return [] |
|
100
|
|
|
|
|
101
|
1 |
|
def get_pages_list(self, project_id): |
|
102
|
|
|
""" Get pages list """ |
|
103
|
1 |
|
try: |
|
104
|
1 |
|
result = self._request('/getpageslist/', |
|
105
|
|
|
{'projectid': project_id}) |
|
106
|
1 |
|
return [TildaPage(**p) for p in result] |
|
107
|
|
|
except NetworkError: |
|
108
|
|
|
return [] |
|
109
|
|
|
|
|
110
|
1 |
|
def get_page(self, page_id): |
|
111
|
|
|
""" Get short page info and body html code """ |
|
112
|
1 |
|
try: |
|
113
|
1 |
|
result = self._request('/getpage/', |
|
114
|
|
|
{'pageid': page_id}) |
|
115
|
1 |
|
return TildaPage(**result) |
|
116
|
|
|
except NetworkError: |
|
117
|
|
|
return [] |
|
118
|
|
|
|
|
119
|
1 |
|
def get_page_full(self, page_id): |
|
120
|
|
|
""" Get full page info and full html code """ |
|
121
|
1 |
|
try: |
|
122
|
1 |
|
result = self._request('/getpagefull/', |
|
123
|
|
|
{'pageid': page_id}) |
|
124
|
1 |
|
return TildaPage(**result) |
|
125
|
|
|
except NetworkError: |
|
126
|
|
|
return [] |
|
127
|
|
|
|
|
128
|
1 |
|
def get_page_export(self, page_id): |
|
129
|
|
|
""" Get short page info for export and body html code """ |
|
130
|
1 |
|
try: |
|
131
|
1 |
|
result = self._request('/getpageexport/', |
|
132
|
|
|
{'pageid': page_id}) |
|
133
|
1 |
|
return TildaPage(**result) |
|
134
|
|
|
except NetworkError: |
|
135
|
|
|
return [] |
|
136
|
|
|
|
|
137
|
1 |
|
def get_page_full_export(self, page_id): |
|
138
|
|
|
""" Get full page info for export and body html code """ |
|
139
|
1 |
|
try: |
|
140
|
1 |
|
result = self._request('/getpagefullexport/', |
|
141
|
|
|
{'pageid': page_id}) |
|
142
|
1 |
|
return TildaPage(**result) |
|
143
|
|
|
except NetworkError: |
|
144
|
|
|
return [] |
|
145
|
|
|
|