-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_album_from_downloaded_files_wc.py
259 lines (247 loc) · 9.82 KB
/
make_album_from_downloaded_files_wc.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""This script should create a single book from an album"""
import json
import qrcode
import flickr_photo
import pandas as pd
# NOTE you will need to replace any line with REPLACEME
# with values appropriate to your system to use this script
ONEUP_FORMAT = False
PAPER_WIDTH = 8.5
PAPER_HEIGHT = 11.0
TOP_MARGIN = 0.5
BOTTOM_MARGIN = 0.5
LEFT_MARGIN = 0.5
RIGHT_MARGIN = 0.5
# four lines of 10pt or 12pt font fit in 0.5in
TEXT_HEIGHT = 0.5
LANDSCAPE_WIDTH = PAPER_WIDTH - LEFT_MARGIN - RIGHT_MARGIN
PORTRAIT_WIDTH = PAPER_WIDTH - LEFT_MARGIN - RIGHT_MARGIN
LANDSCAPE_HEIGHT = PAPER_HEIGHT - TOP_MARGIN - BOTTOM_MARGIN - TEXT_HEIGHT
PORTRAIT_HEIGHT = PAPER_HEIGHT - TOP_MARGIN - BOTTOM_MARGIN - TEXT_HEIGHT
PHOTO_MAX_DIMS = {'landscape_width': LANDSCAPE_WIDTH,
'portrait_width': PORTRAIT_WIDTH,
'landscape_height': LANDSCAPE_HEIGHT,
'portrait_height': PORTRAIT_HEIGHT
}
# if you run the script for downloading all files from a flickr photoset
# then the files will be in a directory in ./cache/ corresponding to an album code
# and metadata about those images will be in a file in cache
# called photoset_info.json
# this file contains json hash keyed on album id
# entries are an album title and sequential list of photos
# with id caption and url
if ONEUP_FORMAT:
ALBUM_CODE = '72177720312512993' #REPLACEME
else:
ALBUM_CODE = '72177720310657841' #REPLACEME
# OTHER ALBUM CODES
#album_code = '72177720310657841' #REPLACEME
#album_code = '72177720311316693' #REPLACEME
#album_code = '72177720310604095' #REPLACEME
#album_code = '72177720310546202' #REPLACEME
#album_code = '72177720310176444' #REPLACEME
#album_code = '72157621908701594' # sample album code
# '72177720314495494' faces
if ONEUP_FORMAT:
BOOK_FILENAME = 'DFTM_REVISED_AGAIN_SUCKA3'
else:
BOOK_FILENAME = 'downloaded_album_666_2'
def create_book_from_downloaded_album():
"""This is the main method which creates the book"""
with open('./cache/' + ALBUM_CODE + '/photoset_info.json', 'r', encoding='utf-8') as myinfile:
album_hash = json.load(myinfile)
# this file contains a json hash keyed on photo id
# entries are metadata for individual photos
# with additional data about datetaken (possibly inaccurate) width and height
# the owner (author) id can be obtained as well which could allow for
# constructing some of the hard-coded urls used in the code if desired
# e.g. owner has id 99753978@N03
album_code_list = []
for key in album_hash.keys():
album_code_list.append(key)
# sort by album title
album_code_list.sort(key = lambda x: album_hash[x]['title'])
# process albums in alphabetical order by title
album_list = []
for this_album_code in album_code_list:
this_album = get_album_info(album_hash,this_album_code)
album_list.append(this_album)
all_sections = []
for this_album in album_list:
# build list of photo objects
photo_list = []
for thisphoto_hash in this_album.album_entries:
thisphoto = get_photo_info(thisphoto_hash,this_album)
photo_list.append(thisphoto)
page_list = get_page_list(photo_list)
this_section = get_section(this_album,page_list)
all_sections.append(this_section)
make_one_multi_section_book(all_sections)
def get_photo_info(thisphoto_hash,this_album):
"""Get photo info from photo hash"""
myid = thisphoto_hash['id']
url = thisphoto_hash['source']
photo_prefix = this_album.id + '/'
photo_filename = myid + '.jpg'
location = photo_prefix + photo_filename
caption = thisphoto_hash['title']
altcaptionlist = get_altcaptionlist(caption)
#caption = ''
width = thisphoto_hash['width']
height = thisphoto_hash['height']
thisphoto = flickr_photo.Photo(myid,url,location,caption,width,height)
# bonus info
thisphoto.album_title = this_album.title
# thisphoto.altcaptionlist = altcaptionlist
return thisphoto
def get_altcaptionlist(title):
return []
myindex = int(title)
title = 'Moby Dick'
author = 'Herman Melville'
df = pd.read_csv('cache/litlist.csv')
thisrow = df.iloc[myindex]
title = thisrow['title']
author = thisrow['author']
return [title,author]
def get_section(this_album,page_list):
"""Get a single section with qr code """
this_section = flickr_photo.Section()
qrdimmax = PAPER_WIDTH - LEFT_MARGIN - RIGHT_MARGIN
if qrdimmax < 5.19:
qrdim = qrdimmax
else:
qrdim = 5.19
this_section.qrdim = qrdim
for thispage in page_list:
this_section.add_page(thispage)
this_section.title = this_album.title
this_section.author = this_album.author
this_section.date = this_album.date
this_section.url = this_album.url
qr_path = create_qr_code(this_album)
this_section.qr = qr_path
if ONEUP_FORMAT:
this_section.blank_after_qr = True
else:
this_section.blank_after_qr = False
return this_section
def get_album_info(album_hash,this_album_code):
"""Get album info from album hash"""
# owner_id can be extracted from all_info_file if desired and consistent
this_album = flickr_photo.Album(this_album_code)
this_album.id = this_album_code
this_album.author = album_hash[this_album_code]['owner_name']
this_album.owner_id = album_hash[this_album_code]['owner_id']
this_album.date = ''
this_album.title = album_hash[this_album_code]['title']
this_album.url = ('https://www.flickr.com/photos/'
+ this_album.owner_id
+ '/albums/'
+ this_album.id)
this_album.album_entries = album_hash[this_album_code]['photoset_hash']
return this_album
def make_one_multi_section_book(all_sections):
"""This method makes one multi-section book"""
section_list = []
for this_section in all_sections:
section_list.append(this_section)
total_pages = 0
for this_section in section_list:
print(this_section.title)
pages_in_section = len(this_section.page_list)
print("Pages in section: " + str(pages_in_section))
total_pages += len(this_section.page_list)
print("Pages in book so far: " + str(total_pages))
output_filename = 'cache/' + BOOK_FILENAME + '.tex'
with open(output_filename, 'w', encoding='utf-8') as myoutfile:
if ONEUP_FORMAT:
paper_dimensions={
'paper_width':PAPER_WIDTH,
'paper_height':PAPER_HEIGHT,
'top_margin':TOP_MARGIN,
'bottom_margin':BOTTOM_MARGIN,
'left_margin':LEFT_MARGIN,
'right_margin':RIGHT_MARGIN,
}
this_book = flickr_photo.Book(myoutfile,paper_dimensions,ONEUP_FORMAT)
else:
paper_dimensions={}
this_book = flickr_photo.Book(myoutfile,paper_dimensions,ONEUP_FORMAT)
this_book.title = ''
this_book.author = ''
this_book.date = ''
this_book.url = ''
this_book.section_list = section_list
print('Creating tex file for ' + BOOK_FILENAME)
this_book.print_book()
print('Album tex file created, see ' + output_filename)
def make_all_single_section_books(all_sections):
"""Make all single section books"""
for this_section in all_sections:
# for now we are restricting books to one section...
section_list = [this_section]
output_filename = 'texfiles/' + this_section.title + '.tex'
with open(output_filename, 'w', encoding='utf-8') as myoutfile:
if ONEUP_FORMAT:
paper_dimensions = {'paper_width':PAPER_WIDTH,
'paper_height':PAPER_HEIGHT,
'top_margin':TOP_MARGIN,
'bottom_margin':BOTTOM_MARGIN,
'left_margin':LEFT_MARGIN,
'right_margin':RIGHT_MARGIN,
}
this_book = flickr_photo.Book(myoutfile,paper_dimensions,ONEUP_FORMAT)
else:
paper_dimensions={}
this_book = flickr_photo.Book(myoutfile,paper_dimensions,ONEUP_FORMAT)
this_book.title = this_section.title
this_book.author = this_section.author
this_book.date = this_section.date
this_book.url = this_section.url
this_book.section_list = section_list
print('Creating tex file for ' + this_section.title)
this_book.print_book()
def create_qr_code(this_album):
"""Create a qr code corresponding to url for an album"""
print('Creating qr code for ' + this_album.url)
qr_img = qrcode.make(this_album.url)
qr_path = 'qr/' + this_album.id + '.jpg'
qr_img.save('cache/' + qr_path)
return qr_path
def get_page_list(photo_list):
"""Get list of pages"""
print('Using: LW ' + str(LANDSCAPE_WIDTH)
+ ' LH ' + str(LANDSCAPE_HEIGHT)
+ ' PW ' + str(PORTRAIT_WIDTH)
+ ' PH ' + str(PORTRAIT_HEIGHT))
page_list = []
if ONEUP_FORMAT:
# current_page = flickr_photo.Page(landscape_width=LANDSCAPE_WIDTH,
# landscape_height=LANDSCAPE_HEIGHT,
# portrait_width=PORTRAIT_WIDTH,
# portrait_height=PORTRAIT_HEIGHT,one_up=True)
current_page = flickr_photo.Page(PHOTO_MAX_DIMS,one_up=True)
else:
current_page = flickr_photo.Page({},one_up=False)
for thisphoto in photo_list:
if (thisphoto.orientation == 'L') and (current_page.canfit_l()):
current_page.add_photo(thisphoto)
elif (thisphoto.orientation == 'P') and (current_page.canfit_p()):
current_page.add_photo(thisphoto)
else:
page_list.append(current_page)
if ONEUP_FORMAT:
# current_page = flickr_photo.Page(landscape_width=LANDSCAPE_WIDTH,
# landscape_height=LANDSCAPE_HEIGHT,
# portrait_width=PORTRAIT_WIDTH,
# portrait_height=PORTRAIT_HEIGHT,one_up=True)
current_page = flickr_photo.Page(PHOTO_MAX_DIMS,one_up=True)
else:
current_page = flickr_photo.Page({},one_up=False)
current_page.add_photo(thisphoto)
# add final page
page_list.append(current_page)
return page_list
if __name__ == '__main__':
create_book_from_downloaded_album()