-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_flickr_api_json_caption_only_hack.py
265 lines (253 loc) · 9.74 KB
/
parse_flickr_api_json_caption_only_hack.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
260
261
262
263
264
265
"""Hack to do book with captions only"""
# This )is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import json
import qrcode
import flickr_photo_caption_only_hack
def parse_file():
"""Parse json file with all image metadata"""
# this file contains json hash keyed on album id
# entries are an album title and sequential list of photos
# with id caption and url
with open('allalbumswithurls4.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
with open('all_info_file.json', 'r', encoding='utf-8') as myinfile:
all_info_hash = json.load(myinfile)
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:
# owner_id can be extracted from all_info_file if desired and consistent
owner_id = '99753978@N03'
this_album = flickr_photo_caption_only_hack.Album(this_album_code)
this_album.id = this_album_code
this_album.author = 'Patrick Swickard'
this_album.date = ''
this_album.title = album_hash[this_album_code]['title']
this_album.url = 'https://www.flickr.com/photos/' + owner_id + '/albums/' + this_album.id
this_album.album_entries = album_hash[this_album_code]['photoset_hash']
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_thisphoto_info(thisphoto_hash,this_album,all_info_hash)
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_big_book_all_text(all_sections)
def get_thisphoto_info(thisphoto_hash,this_album,all_info_hash):
"""Get thisphoto info"""
myid = thisphoto_hash['id']
url = thisphoto_hash['url']
prefix = '/home/swickape/Pictures/flickr/Downloads/' + this_album.title + '/'
photo_filename = myid + '.jpg'
location = prefix + photo_filename
caption = thisphoto_hash['title']
width = all_info_hash[myid]['width_o']
height = all_info_hash[myid]['height_o']
thisphoto = flickr_photo_caption_only_hack.Photo(myid,url,location,caption,width,height)
# bonus info
thisphoto.album_title = this_album.title
return thisphoto
def make_one_multi_section_book(all_sections):
"""Make a single multi-section book"""
section_list = []
for this_section in all_sections:
section_list.append(this_section)
#book_filename = 'ctr_001'
#section_list = section_list[0:5]
#book_filename = 'ctr_002'
#section_list = section_list[5:8]
#book_filename = 'ctr_003'
#section_list = section_list[8:10]
#book_filename = '_ctr_004'
#section_list = section_list[10:14]
#book_filename = 'ctr_005'
#section_list = section_list[14:18]
#book_filename = 'ctr_006'
#section_list = section_list[18:23]
#book_filename = 'ctr_007'
#section_list = section_list[23:27]
#book_filename = 'ctr_008'
#section_list = section_list[27:31]
#book_filename = 'ctr_009'
#section_list = section_list[31:35]
#book_filename = 'ctr_010'
#section_list = section_list[35:38]
#book_filename = 'ctr_011'
#section_list = section_list[38:44]
#book_filename = 'ctr_012'
#section_list = section_list[44:51]
#book_filename = 'ctr_013'
#section_list = section_list[51:56]
#book_filename = 'ctr_014'
#section_list = section_list[56:59]
#book_filename = 'ctr_015'
#section_list = section_list[59:64]
#book_filename = 'ctr_016'
#section_list = section_list[64:67]
#book_filename = 'ctr_017'
#section_list = section_list[67:69]
#book_filename = 'ctr_018'
#section_list = section_list[69:73]
#book_filename = 'ctr_019'
#section_list = section_list[73:76]
#book_filename = 'ctr_020'
#section_list = section_list[76:103]
#book_filename = 'ctr_021'
#section_list = section_list[103:115]
#book_filename = 'ctr_022'
#section_list = section_list[115:131]
#book_filename = 'ctr_023'
#section_list = section_list[131:142]
#book_filename = 'ctr_024'
#section_list = section_list[142:156]
#book_filename = 'ctr_025'
#section_list = section_list[156:170]
#book_filename = 'ctr_027'
#section_list = section_list[178:190]
#book_filename = 'ctr_028'
#section_list = section_list[191:205]
#book_filename = 'ctr_029'
#section_list = section_list[205:207]
#book_filename = 'ctr_030'
#section_list = section_list[207:212]
#book_filename = 'ctr_031'
#section_list = section_list[212:218]
#book_filename = 'ctr_032'
#section_list = section_list[218:222]
#book_filename = 'ctr_033'
#section_list = section_list[222:230]
#book_filename = 'ctr_034'
#section_list = section_list[230:242]
#book_filename = 'ctr_035'
#section_list = section_list[242:251]
#book_filename = 'ctr_036'
#section_list = section_list[251:260]
#book_filename = 'ctr_037'
#section_list = section_list[260:268]
#book_filename = 'ctr_038'
#section_list = section_list[268:274]
#book_filename = 'ctr_039'
#section_list = section_list[274:278]
#book_filename = 'ctr_040'
#section_list = section_list[278:281]
#book_filename = 'ctr_041'
#section_list = section_list[281:284]
#book_filename = 'ctr_042'
#section_list = section_list[284:290]
#book_filename = 'ctr_043'
#section_list = section_list[290:295]
#book_filename = 'ctr_044'
#section_list = section_list[295:298]
#book_filename = 'ctr_045'
#section_list = section_list[299:304]
#book_filename = 'ctr_046'
#section_list = section_list[304:308]
#book_filename = 'ctr_047'
#section_list = section_list[308:315]
#book_filename = 'ctr_048'
#section_list = section_list[315:322]
book_filename = 'ctr_049'
section_list = section_list[322:327]
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 = 'texfiles2/' + book_filename + '.tex'
with open(output_filename, 'w', encoding='utf-8') as myoutfile:
this_book = flickr_photo_caption_only_hack.Book(myoutfile)
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()
def make_all_single_section_books(all_sections):
"""Make all books with a single section"""
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:
this_book = flickr_photo_caption_only_hack.Book(myoutfile)
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 make_one_big_book_all_text(all_sections):
"""Make one big book with text only captions"""
section_list = []
for this_section in all_sections:
section_list.append(this_section)
book_filename = 'everything2'
output_filename = 'texfiles3/' + book_filename + '.tex'
with open(output_filename, 'w', encoding='utf-8') as myoutfile:
this_book = flickr_photo_caption_only_hack.Book(myoutfile)
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_caption_only()
def create_qr_code(this_album):
"""Create a qr code jpg image that points to an album/section"""
print('Creating qr code for ' + this_album.url)
qr_img = qrcode.make(this_album.url)
qr_path = '/home/swickape/Pictures/flickr/Downloads/qr/' + this_album.id + '.jpg'
qr_img.save(qr_path)
return qr_path
def get_page_list(photo_list):
"""Get page list"""
page_list = []
current_page = flickr_photo_caption_only_hack.Page()
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)
current_page = flickr_photo_caption_only_hack.Page()
current_page.add_photo(thisphoto)
# add final page
page_list.append(current_page)
return page_list
def get_section(this_album,page_list):
"""Get section"""
this_section = flickr_photo_caption_only_hack.Section()
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
return this_section
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
parse_file()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/