-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.py
59 lines (45 loc) · 1.76 KB
/
collect.py
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
import redis
import fileinput
import requests
import argparse
import os
import settings
class UnistoreClient(object):
def __init__(self, url, token):
self.url = url
self.token = token
self.redis = redis.StrictRedis(host='localhost', port=6379, db=0)
def _upload_file(self, type_id, file_name, content):
response = requests.post(self.url,
data={'type_id': type_id},
files={'file': (file_name, content)},
headers={'Token': self.token})
return response.status_code == 200 and response.json['id'] or None
def _memorize(self, url, id):
return self.redis.set(url, id)
def put_file(self, url, type_id, file_name, content):
id = self._upload_file(type_id, file_name, content)
return id and self._memorize(url, id) or False
def main():
parser = argparse.ArgumentParser(prog=__file__)
parser.add_argument('token', help='Unistore access token')
parser.add_argument('url_list', help='File that contains URLs (one URL per line)')
args = parser.parse_args()
unistore = UnistoreClient(settings.UNISTORE_URL, args.token)
for line in fileinput.input(args.url_list):
url = line.strip()
print url,
try:
response = requests.get(url)
print '.',
if response.status_code == 200:
type_id = os.path.basename(args.url_list)
file_name = url.rsplit('/', 1)[1]
succeed = unistore.put_file(url, type_id, file_name, response.content)
print (succeed and 'OK' or 'F')
else:
print 'E'
except:
print 'E!'
if __name__ == '__main__':
main()