Guest User

Youtube Sync Script

a guest
Aug 2nd, 2022
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. import os
  2. from os.path import exists
  3.  
  4.  
  5. def get_playlist():
  6.     playlist = "youtube.com/playlist"
  7.     getplist="youtube-dl --get-filename -x --audio-format mp3 '"+ playlist +"' >> temp_plist"
  8.     file_exist = exists("temp_plist")
  9.     file_extent = ".mp4, .webm"
  10.     if file_exist == True:
  11.         os.remove("temp_plist")
  12.         fp = open('temp_plist', 'w')
  13.         fp.close()
  14.     else:
  15.         fp = open('temp_plist', 'w')
  16.         fp.close()
  17.  
  18.  
  19.     os.system(getplist)
  20.     print("Cleaning Data")
  21.     with open('temp_plist', 'r') as file:
  22.         content = file.read()
  23.         content = content.replace(".m4a", "")
  24.         content = content.replace(".webm", "")
  25.     with open(r'temp_plist', 'w') as file:
  26.         file.write(content)
  27.  
  28.  
  29. def get_local_list():
  30.     global path
  31.     path = "/Path/to/Local/"
  32.     getllist = "ls "+ path +"*.mp3 >> temp_llist"
  33.     file_exist = exists("temp_llist")
  34.     if file_exist == True:
  35.         os.remove("temp_llist")
  36.         fp = open('temp_llist', 'w')
  37.         fp.close()
  38.     else:
  39.         fp = open('temp_llist', 'w')
  40.         fp.close()
  41.     os.system(getllist)
  42.     with open('temp_llist', 'r') as file:
  43.         content = file.read()
  44.         content = content.replace(path, "")
  45.         content = content.replace(".mp3", "")
  46.     with open(r'temp_llist', 'w') as file:
  47.         file.write(content)
  48.  
  49.  
  50. def conv_file_to_list():
  51.     global plist
  52.     global llist
  53.     with open('temp_plist') as pfile:
  54.          plist = pfile.readlines()
  55.          pfile.close()
  56.     os.remove("temp_plist")
  57.     with open('temp_llist') as lfile:
  58.          llist = lfile.readlines()
  59.          lfile.close()
  60.     os.remove("temp_llist")
  61.     plist.sort(reverse=False)
  62.     llist.sort(reverse=False)
  63.  
  64.  
  65. def comp_lists():
  66.     global newllist
  67.     global newplist
  68.     newlist  = list(set(plist) & set(llist))
  69.     newplist = list(set(plist) - set(newlist))
  70.     newllist = list(set(llist) - set(newlist))
  71.  
  72.  
  73.  
  74. def get_missing_file():
  75.     for i in newplist:
  76.         os.system("youtube-dl -x --audio-format mp3 ytsearch:'"+ i +"' -o'"+ path +"%(title)s-%(id)s.%(ext)s'")
  77.  
  78.  
  79. def main():
  80.     print("Getting Youtube playlist")
  81.     get_playlist();
  82.     print("Getting local list")
  83.     get_local_list();
  84.     conv_file_to_list();
  85.     if plist != llist:
  86.         print("Discrepancy detected, updating..")
  87.         comp_lists();
  88.         get_missing_file();
  89.         print("Update finished.")
  90.         if len(newllist) != 0:
  91.             print("Titles not existend in the Youtube playlist:"+ str(newllist))
  92.     else:
  93.         print("Everything is updated.")
  94.  
  95. if __name__ == "__main__":
  96.     main()
  97.  
Advertisement
Add Comment
Please, Sign In to add comment