Source code for Sorting_iplayer_recordings

#!/usr/bin/env python

"""Python3 code to rename multiple  
files in a directory or folder""" 
  
# importing modules 
import os
import datetime
import helper_functions as h
import DatabaseManager as dbm
import data as d


[docs]def update(path1, filename, path2, stub, db, bulk_flag): """The update function moves an mp3 file from it's original directory to a new directory. It will rename the file in the process. It will also record what it has done in the 'iPlayer_archive' on b4Pio. """ __src = path1 + filename __dst = stub + ".mp3" __dst = path2 + __dst __a = "Whatever the user decides!" if not path2 in bulk_flag: __possible_answers = ('Y', 'Yes', 'y', 'yes', 'No', 'N', 'n', 'no', 'B', 'Bulk', 'b', 'bulk', 'Q', 'Quit', 'q', 'quit') while not (__a in __possible_answers): __a = input("\tSuggest moving it to '{}', is that OK? (Yes, No, Bulk) ".format(__dst)) # rename() function will # rename all the files if __a in ('Q', 'Quit', 'q', 'quit'): print("Don't know how to stop!") elif __a in ('No', 'N', 'n', 'no'): print("\tLet it be") elif __a in ('B', 'Bulk', 'b', 'bulk'): bulk_flag.append(path2) if __a in ('Y', 'Yes', 'y', 'yes') or path2 in bulk_flag: os.rename(__src, __dst) print("\tMoved to '{}'.".format(__dst)) __date = datetime.datetime.now() __data = (__dst, __src, __date) __recordings = d.recordings __sql = __recordings.insertSQL() __sqlText = "INSERT INTO recordings ({}) VALUES ({})".format(__sql[0], __sql[1]) db.singleupdateTable(__sqlText, __data)
# Function to rename multiple files
[docs]def main(): """The mainloop, points the source to the 'newfiles' directory under landing stage, opens the iPlayer_archive database, goes through all the source files and compares with various patterns (in a series of 'if' statements), defines new names as appropriate and calls the update function to move and rename each matched file. Finally closes the iPlayer_archive database. """ h.cls() path1 = '/home/andrew/Documents/00-landing_stage/newfiles/' files = os.listdir(path1) credentials = d.entity("servers")["localhost"] iPlayer_archive = dbm.DbMan(credentials, "iPlayer_archive") bulk_flag = [] # os.rename(os.path.join(path1, file), os.path.join(path2, ''.join([name, '.mp3']))) for count, filename in enumerate(files, start=1): print("\n{}: Found '{}'.".format(count, filename)) if filename[:41] == "15_Minute_Drama_Incredible_Women_Series_1": path2 = '/home/andrew/Documents/05-Audio_Recordings/Incredible_Women/Series1/' stub = filename[44:46] update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:18] == "A_Point_of_View_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/A_Point_of_View/' stub = filename[18:] stub = stub[:-22] update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:33] == "A_Point_of_View_After_the_Vote_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/A_Point_of_View_After_the_Vote/' stub = filename[33:] stub = stub[:-22] stub = stub.replace("._", "-") update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:23] == "After_Henry_Series_2_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/After_Henry/Series2/' stub = filename.replace("After_Henry_Series_2_-_", "") stub = stub[:-22] stub = stub.replace("._", "-") update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:27] == "Annika_Stranded_Series_1_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/Annika_Stranded/Series1/' stub = filename.replace("Annika_Stranded_Series_1_-_", "") stub = stub[:-22] stub = stub.replace("._", "-") update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:16] == "Beyond_Belief_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/Beyond_Belief/' stub = filename.replace("Beyond_Belief_-_", "") stub = stub[:-22] update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:20] == "Chambers_Series_2_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/Chambers/Series2/' stub = filename[20:] stub = stub[:-22] stub = stub.replace("._", "-") update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:52] == "Curious_Under_the_Stars_Series_10_Things_of_Stone_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/Curious_Under_the_Stars/Series10/' stub = filename.replace("Curious_Under_the_Stars_Series_10_Things_of_Stone_-_", "Things_of_Stone-") stub = stub[:-30] update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:21] == "Dads_Army_Series_3_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/Dads_Army/Series3/' stub = filename.replace("Dads_Army_Series_3_-_", "") stub = stub[:-22] stub = stub.replace("._", "-") update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:36] == "GF_Newmans_The_Corrupted_Series_5_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/The_Corrupted/Series5/' stub = filename.replace('GF_Newmans_The_Corrupted_Series_5_-_', '') stub = stub[:-33] update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:14] == "HR_Series_2_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/HR/Series2/' stub = filename.replace("HR_Series_2_-_", "") stub = stub[:-22] stub = stub.replace("._", "-") update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:40] == "Im_Sorry_Ill_Read_That_Again_Series_4_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/ISIRTA/Series4/' stub = filename.replace("Im_Sorry_Ill_Read_That_Again_Series_4_-_", "") stub = stub[:-33] stub = stub.replace("._", "-") update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:12] == "Seriously_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/Seriously/' stub = filename[12:] stub = stub[:-21] update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) elif filename[:33] == "Think_the_Unthinkable_Series_3_-_": path2 = '/home/andrew/Documents/05-Audio_Recordings/Think_the_Unthinkable/Series3/' stub = filename[33:] stub = stub[:-22] stub = stub.replace("._", "-") update(path1, filename, path2, stub, iPlayer_archive, bulk_flag) else: print("\tbut it did not match criteria so left it be.") iPlayer_archive.close()
# Driver Code if __name__ == '__main__': # Calling main() function main()