generated from deep5050/godfather
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
51 lines (38 loc) · 1.17 KB
/
main.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
import json
from urllib.request import urlopen
import os
import re
import pickle
from time import sleep
ids = []
url = 'https://tg.i-c-a.su/json/programmerjokes/1?limit=100'
r = urlopen(url)
data = json.loads(str(r.read().decode("utf-8")))
count = 0
with open('ids.data', 'rb') as filehandle:
# read the data as binary data stream
ids = pickle.load(filehandle)
for message in data['messages']:
#print(message['id'])
id = message['id']
# check if already in the database
if int(id) in ids:
print(str(id)+': already in the database')
continue
try:
# download image
img_url = 'https://tg.i-c-a.su/media/programmerjokes/'+str(id)
response = urlopen(img_url)
img_file = open('memes/3/'+str(id)+'.png','wb')
img_file.write(response.read())
img_file.close()
print(str(id)+': downloaded')
sleep(2)
ids.append(int(message['id']))
count = count + 1
except:
print("Error while downloading:"+str(id))
with open('ids.data', 'wb') as filehandle:
# store the data as binary data stream
pickle.dump(sorted(ids), filehandle)
print("count: ",str(count))