forked from soju6jan/offcloud2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffcloud_api.py
More file actions
executable file
·190 lines (149 loc) · 5.96 KB
/
offcloud_api.py
File metadata and controls
executable file
·190 lines (149 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf-8 -*-
#########################################################
# python
import traceback
import json
import logging
# third-party
import requests
# sjva 공용
from framework import py_urllib, py_urllib2
# 패키지
from .plugin import logger, package_name
#########################################################
class Offcloud(object):
@staticmethod
def get_cache_list(key, magnet_list):
try:
url = 'https://offcloud.com/api/torrent/check?key=%s' % (key)
params = {'hashes' : magnet_list}
res = requests.post(url, json=params)
result = res.json()
return result['cachedItems']
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
return None
@staticmethod
def get_remote_account(key):
try:
url = 'https://offcloud.com/api/remote/accounts'
params = {'key' : key}
postdata = py_urllib.urlencode(params).encode('utf-8')
request = py_urllib2.Request(url, postdata)
response = py_urllib2.urlopen(request)
data = json.load(response)
if 'data' in data:
return data['data']
else:
return None
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
return None
# 피드로
@staticmethod
def add_remote(key, feed, remote_options_id):
try:
result = {}
url = 'https://offcloud.com/api/remote'
params = {
'key' : key,
'url' : feed.link,
'remoteOptionId' : remote_options_id,
'folderId' : feed.oc_folderid,
}
postdata = py_urllib.urlencode(params).encode('utf-8')
request = py_urllib2.Request(url, postdata)
response = py_urllib2.urlopen(request)
data = response.read()
result = json.loads(data)
logger.debug('ADD REMOTE ret: %s', result)
if 'error' in result and result['error'].startswith("You have more than 100"):
return 'over'
feed.oc_requestId = result['requestId'] if 'requestId' in result else ''
feed.oc_status = result['status'] if 'status' in result else ''
feed.oc_createdOn = result['createdOn'] if 'createdOn' in result else ''
feed.oc_error = result['error'] if 'error' in result else ''
feed.oc_json = result
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
# 걍 마그넷으로
@staticmethod
def add_remote_by_magnet(key, magnet, remote_options_id, folder_id):
try:
url = 'https://offcloud.com/api/remote'
params = {
'key' : key,
'url' : magnet,
'remoteOptionId' : remote_options_id,
'folderId' : folder_id,
}
postdata = py_urllib.urlencode(params).encode('utf-8')
request = py_urllib2.Request(url, postdata)
response = py_urllib2.urlopen(request)
data = response.read()
result = json.loads(data)
return result
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def refresh_status(key, feed):
try:
url = 'https://offcloud.com/api/remote/status'
params = {'key' : key, 'requestId' : feed.oc_requestId}
postdata =py_urllib.urlencode(params).encode('utf-8')
request = py_urllib2.Request(url, postdata)
response = py_urllib2.urlopen(request)
data = response.read()
data = json.loads(data)
feed.oc_json = data
if 'status' in data:
feed.oc_status = data['status']['status']
if feed.oc_status == 'error':
pass
elif feed.oc_status == 'downloaded':
feed.oc_fileSize = int(data['status']['fileSize'])
feed.oc_fileName = data['status']['fileName']
else:
logger.debug('NO STATUS %s %s', url, params)
feed.oc_status = 'NOSTATUS'
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def retry(key, entity):
try:
url = 'https://offcloud.com/api/remote/retry/%s?key=%s' % (entity.requestId, key)
request = py_urllib2.Request(url)
response = py_urllib2.urlopen(request)
except Exception as e:
logger.debug('Exception:%s', e)
logger.debug(traceback.format_exc())
return None
@staticmethod
def cache(key, magnet_list, remoteOptionId):
try:
url = 'https://offcloud.com/api/torrent/check?key=%s' % (key)
params = {'hashes' : magnet_list}
res = requests.post(url, json=params)
result = res.json()
for i in result['cachedItems']:
url = 'https://offcloud.com/api/remote'
params = {
'key' : key,
'url' : 'magnet:?xt=urn:btih:%s' % i,
'remoteOptionId' : remoteOptionId,
'folderId' : '10b5Z-0RA08d6p2iMNQ9D__7e5gWOV6v5',
}
postdata = py_urllib.urlencode(params).encode('utf-8')
request = py_urllib2.Request(url, postdata)
response = py_urllib2.urlopen(request)
data = response.read()
result = json.loads(data)
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
return None