-
Notifications
You must be signed in to change notification settings - Fork 0
/
fad.py
executable file
·148 lines (122 loc) · 4.85 KB
/
fad.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
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
#!/usr/bin/python3
import flickrapi
import json
import os
import urllib
import urllib.parse
import shutil
import csv
import configparser
from multiprocessing import Pool
from ratelimit import limits, sleep_and_retry
# Config
class FAD():
def __init__(self):
self.read_config('fad.conf')
# Connect
self.flickr = flickrapi.FlickrAPI(self.api_key, self.api_secret, format='parsed-json')
self.flickr_etree = flickrapi.FlickrAPI(self.api_key, self.api_secret)
# Get albums
self.sets = self.flickr.photosets.getList(user_id=self.user_id)
def read_config(self, configfile):
config = configparser.ConfigParser()
config.read(configfile)
self.api_key = config['flickr']['api_key']
self.api_secret = config['flickr']['api_secret']
self.user_id = config['flickr']['user_id']
self.flickr_url = config['flickr']['flickr_url']
self.dest_dir = config['flickr']['dest_dir']
def make_dir(self, dest_dir):
try:
os.makedirs(dest_dir, 0o755, exist_ok=True)
except OSError:
print (f"Creation of directory {dest_dir} failed.")
pass
else:
print (f"Creation of directory {dest_dir} succeeded.")
pass
def download_no_album(self):
self.flickr_etree.authenticate_via_browser(perms='read')
photos = self.flickr_etree.photos.getNotInSet(extras='url_o')
# Create album directory
album_dir = os.path.join(self.dest_dir, 'Untitled')
self.make_dir(album_dir)
# Create index to order photos
index = 1
try:
os.remove(os.path.join(album_dir, 'album.csv'))
except:
pass
csvfile = open(os.path.join(album_dir, 'album.csv'), 'a')
csvwriter = csv.writer(csvfile)
for photo in photos.find('photos'):
url = photo.get('url_o')
print(url)
url_ = urllib.parse.urlparse(url)
filename = str(index).zfill(3) + '_' + os.path.basename(url_.path)
title = photo.get('title')
# Write photo title to CSV file
csvwriter.writerow([str(index).zfill(3), filename, title])
full_dest_path = os.path.join(album_dir, filename)
# Download file
self.download_photo(url, album_dir, full_dest_path)
index += 1
def download_all_albums(self):
# Create parent dir
self.make_dir(self.dest_dir)
album_and_photolist = [ [set['title']['_content'], list(self.get_photo_list(set['id']))] for set in self.sets['photosets']['photoset'] ]
pool = Pool(5)
results = pool.map(self.download_album, album_and_photolist)
pool.close()
pool.join()
def download_album(self, album_and_photolist):
print(album_and_photolist[0])
# Create album directory
album_dir = os.path.join(self.dest_dir, album_and_photolist[0])
self.make_dir(album_dir)
# TODO
# - write album API info to file
# album_api_response = open(os.path.join(self.dest_dir, 'album_api_response.txt'), 'w')
# album_api_response.write(photos)
# Create index to order photos
index = 1
try:
os.remove(os.path.join(album_dir, 'album.csv'))
except:
pass
csvfile = open(os.path.join(album_dir, 'album.csv'), 'a')
csvwriter = csv.writer(csvfile)
for photo in album_and_photolist[1]:
url = photo.get('url_o')
print(url)
url_ = urllib.parse.urlparse(url)
filename = str(index).zfill(3) + '_' + os.path.basename(url_.path)
title = photo.get('title')
# Write photo title to CSV file
csvwriter.writerow([str(index).zfill(3), filename, title])
full_dest_path = os.path.join(album_dir, filename)
# Download file
self.download_photo(url, album_dir, full_dest_path)
index += 1
csvfile.close()
def download_photo(self, url, album_dir, full_dest_path):
try:
# urllib.request.urlretrieve(url, full_dest_path)
# response = urllib.request.urlopen(url)
# out_file = open(full_dest_path, 'wb')
# shutil.copyfileobj(response, out_file)
os.system(f'wget --continue {url} -O "{full_dest_path}" -a "{album_dir}/download.log" --random-wait --waitretry=5 --retry-connrefused --retry-on-host-error')
except urllib.error.URLError as e:
print(f"{url} failed to download")
print(e)
@sleep_and_retry
@limits(calls=1, period=1)
def get_photo_list(self, album_id):
photos = self.flickr_etree.walk_set(album_id, extras='url_o')
return photos
def main():
fad = FAD()
fad.download_all_albums()
fad.download_no_album()
if __name__ == '__main__':
main()