99
1010# Standard library
1111import json
12+ import os .path
1213import sys
1314import time
1415import traceback
1819import pandas as pd
1920import query_secrets
2021
22+ CWD = os .path .dirname (os .path .abspath (__file__ ))
23+ RETRIES = 0
24+
2125
2226def to_df (datalist , namelist ):
2327 """
@@ -139,10 +143,11 @@ def page1_reset(final_csv, raw_data):
139143 return raw_data ["photos" ]["pages" ]
140144
141145
142- retries = 0
143-
144-
145146def main ():
147+ final_csv_path = os .path .join (CWD , "final.csv" )
148+ record_txt_path = os .path .join (CWD , "rec.txt" )
149+ hs_csv_path = os .path .join (CWD , "hs.csv" )
150+
146151 flickr = flickrapi .FlickrAPI (
147152 query_secrets .api_key , query_secrets .api_secret , format = "json"
148153 )
@@ -172,7 +177,7 @@ def main():
172177 # use rec txt to record j(current page), i(current license), and total
173178 # every time iterating through one page of photos
174179 # to pick up from where the script errors or stops
175- with open ("rec.txt" ) as f :
180+ with open (record_txt_path ) as f :
176181 readed = f .read ().split (" " )
177182 j = int (readed [0 ])
178183 i = int (readed [1 ])
@@ -188,7 +193,7 @@ def main():
188193 # change total equals to the total picture number
189194 # and set the final CSV as empty
190195 if j == 1 :
191- total = page1_reset ("final.csv" , photos )
196+ total = page1_reset (final_csv_path , photos )
192197
193198 # use getInfo method to get more detailed photo
194199 # info from inputting photo id
@@ -224,14 +229,14 @@ def main():
224229 "in license" ,
225230 i ,
226231 "with retry number" ,
227- retries ,
232+ RETRIES ,
228233 )
229234
230235 # save csv
231- df_to_csv (temp_list , name_list , "hs.csv" , "final.csv" )
232- # update j(the current page number in txt)
233- with open ("rec.txt" , "w" ) as f :
234- f .write (str ( j ) + " " + str ( i ) + " " + str ( total ) )
236+ df_to_csv (temp_list , name_list , hs_csv_path , final_csv_path )
237+ # update j (the current page number in txt)
238+ with open (record_txt_path , "w" ) as f :
239+ f .write (f" { j } { i } { total } " )
235240
236241 # set list to empty everytime after saving the data into
237242 # the csv file to prevent from saving duplicate data
@@ -240,13 +245,14 @@ def main():
240245 # if current page has reached the max limit of total pages
241246 # reset j to 1 and update i to the license in the dictionary
242247 if j == total + 1 or j > total :
243- clean_saveas_csv ("final.csv" , "license" + str (i ) + ".csv" )
248+ license_i_path = os .path .join (CWD , f"license{ i } .csv" )
249+ clean_saveas_csv (final_csv_path , license_i_path )
244250 i += 1
245251 j = 1
246252 while i not in license_list :
247253 i += 1
248- with open ("rec.txt" , "w" ) as f :
249- f .write (str ( j ) + " " + str ( i ) + " " + str ( total ) )
254+ with open (record_txt_path , "w" ) as f :
255+ f .write (f" { j } { i } { total } " )
250256
251257 # below is to clear list everytime
252258 # before rerun (to prevent duplicate)
@@ -263,11 +269,11 @@ def main():
263269 except KeyboardInterrupt :
264270 print ("INFO (130) Halted via KeyboardInterrupt." , file = sys .stderr )
265271 sys .exit (130 )
266- except Exception as e :
267- retries += 1
272+ except Exception :
273+ RETRIES += 1
268274 print ("ERROR (1) Unhandled exception:" , file = sys .stderr )
269275 print (traceback .print_exc (), file = sys .stderr )
270- if retries <= 20 :
276+ if RETRIES <= 20 :
271277 continue
272278 else :
273279 sys .exit (1 )
0 commit comments