Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. import requests
  2. import os
  3. import shutil
  4. import zipfile
  5. import json
  6. import html
  7. import io
  8. import string
  9.  
  10. api = "https://beatsaver.com/api.php?mode=new&off={}"
  11. offset_inc = 15
  12. offset = 0
  13.  
  14. download = "https://beatsaver.com/files/{}.zip"
  15. downloaded_songs = []
  16. this_session = 0
  17. processing = True
  18.  
  19. if os.path.isfile("songs.json"):
  20. with open("songs.json", "r") as handle:
  21. downloaded_songs = json.loads(handle.read())
  22.  
  23. def escape(s): # Function for ensuring that song names are proper foldernames for windows
  24. valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
  25. filename = ''.join(c for c in s if c in valid_chars)
  26. filename = filename.replace(' ','') # Remove spaces to avoid issues
  27. return filename
  28.  
  29. def extractZip(zip_file, song_name): # Extract zip files into folder with song name ignoring the first directory within the zip
  30. for zip_info in zip_file.infolist(): # for each file in the zip
  31. if not zip_info.filename.endswith('/') and zip_info.filename.count('/') < 2: # if its not the first directory or files in sub directories
  32. data = zip_file.read(zip_info.filename) # read that file
  33. if not os.path.exists(os.path.dirname(song_name+os.path.basename(zip_info.filename))):
  34. os.makedirs(os.path.dirname(song_name+os.path.basename(zip_info.filename))) # create any directories that are needed if they dont exist
  35. try:
  36. with io.FileIO(song_name+os.path.basename(zip_info.filename), "w") as file:
  37. file.write(data) # write the file to disk
  38. except OSError as exc:
  39. if True:
  40. print(os.path.basename(zip_info.filename), 'FAIL')
  41.  
  42.  
  43. while processing:
  44. response = requests.get(api.format(offset)).json()
  45. offset += offset_inc
  46. if len(response) == 0:
  47. break
  48. for song in response:
  49. try:
  50. if song['id'] in downloaded_songs:
  51.  
  52. # We found a song we already downloaded
  53. # Assume we've done them all
  54.  
  55. processing = False
  56. break
  57. print("Downloading {}".format(html.unescape(song['beatname'])))
  58. this_session = this_session + 1
  59. response = requests.get(download.format(song['id']))
  60. with zipfile.ZipFile(io.BytesIO(response.content)) as \
  61. song_zip:
  62. extractZip(song_zip,
  63. 'CustomSongs/{}/'.format(escape(html.unescape(song['beatname'
  64. ]))))
  65.  
  66. # Write out all the files for the zip to a folder named after the songname with html escaped characters and escaping
  67. # Add to downloaded
  68.  
  69. downloaded_songs.append(song['id'])
  70. except:
  71. continue
  72.  
  73.  
  74.  
  75.  
  76. with open("songs.json", "w") as handle:
  77. handle.write(json.dumps(downloaded_songs))
  78. print("Downloaded {} song{} this session".format(this_session, 's' if this_session != 0 else ''))
  79. print("Downloaded {} song{} in total".format(len(downloaded_songs), 's' if len(downloaded_songs) != 0 else ''))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement