-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandler.py
33 lines (26 loc) · 1.01 KB
/
filehandler.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
import os
import multiprocessing
import urllib.request
class FileHandler:
def __init__(self, path, img_path):
self.path = path
self.img_path = img_path
def write(self, data, user=''):
with multiprocessing.Pool(5) as pool:
pool.map(self.download, [(user, d) for d in data])
def download(self, userdata):
user, data = userdata
link = data[0]
filename = link.split('/')[-1].strip()
hashtags = data[1].replace(' ', '')
file = os.path.join(self.img_path, filename)
urllib.request.urlretrieve(link, file)
self.add_to_index(filename, hashtags, username=user)
def add_to_index(self, filename, hashtags, username='', attempt=1):
if attempt > 5:
return
try:
with open(os.path.join(self.path, 'index.csv'), "a") as f:
f.write(filename + ',' + username + ',' + hashtags + '\n')
except PermissionError:
self.add_to_index(filename, hashtags, attempt=attempt+1)