-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs_image_get_threaded_test.py
62 lines (55 loc) · 1.95 KB
/
rs_image_get_threaded_test.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
60
61
62
import requests
import json
import csv
import rfc6266
from os import path, mkdir
import queue
from threading import Thread
data_file = 'rs_data.test'
base_link = 'http://2007.runescape.wikia.com/api/v1/Articles/AsSimpleJson?id='
dirname = 'rsimg'
base_dir = '/'+ dirname +'/'
que = queue.LifoQueue()
def check_img_dir():
if not path.isdir(path.join(path.dirname(__file__) + base_dir)):
mkdir(path.join(path.dirname(__file__) + base_dir))
def queue_factory(data_file):
f = open(data_file, "r")
csv_reader = csv.reader(f, delimiter=',')
for row in csv_reader:
que.put(base_link + row[0])
def check_file_exists(filename):
if path.isfile(filename):
print("FILE EXISTS")
return True
else:
return False
def do_task():
while not que.empty():
url = que.get()
page = requests.get(url, timeout=None)
data = json.loads(page.text)
title = data['sections'][0]['title'][0]
try:
image_url = data['sections'][0]['images'][0]['src']
image_data = requests.get(image_url)
img_filename = rfc6266.parse_requests_response(image_data).filename_unsafe
write_path = path.join(path.dirname(__file__) + base_dir + img_filename)
with open(write_path, 'wb') as img_file:
img_file.write(image_data.content)
img_file.close()
print("WROTE: " + title)
except (IndexError, KeyError) as e:
print("ERROR: " + title.rjust(20))
que.task_done()
def main():
check_img_dir() # this is to make sure the dir exists
try:
queue_factory(data_file)
for i in range(5): # aka number of threadtex
t1 = Thread(target = do_task) # target is the above function
t1.start() # start the thread
que.join()
except KeyboardInterrupt:
raise
if __name__ == "__main__": main()