download.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. import wget
  3. import json
  4. import os
  5. import re
  6. from pathlib import Path
  7. import ssl
  8. ssl._create_default_https_context = ssl._create_unverified_context
  9. # The File downloaded from digital ocean is xml
  10. ## Convert XML to JSON using any online converter
  11. ## Save the Converted Json to /digitalocean_space_data
  12. ## This Script can be used also with s3 bucket xml .
  13. ## am using digital ocean droplet for downloading and uploading the data to another server
  14. ## There is another paid services like transfer in videos website , but for full hd it cost's a lot for service I need .
  15. def folder_manager(path):
  16. path_to_folder_array = path.split('/')
  17. if path_to_folder_array[-1] == '':
  18. path_to_folder_array.pop()
  19. create_if_not_exist(path_to_folder_array)
  20. def pass_folder_type(filename):
  21. print(Path(filename).suffix)
  22. if Path(filename).suffix == '.mp4' or Path(filename).suffix == '.txt':
  23. return False
  24. else:
  25. return True
  26. # replacing slashes with dashes for downloaded folder naming
  27. def url_to_name(download_url):
  28. return download_url.replace('/' , '-')
  29. def download_file(folder , local_location):
  30. path = local_location[10:]
  31. url = 'https://example.fra1.digitaloceanspaces.com/' + path
  32. download_url = url + folder
  33. video_folder_name = url_to_name(path) + folder
  34. try:
  35. wget.download(download_url , local_location + video_folder_name )
  36. except:
  37. print('File Failed to download')
  38. ## Looping throw folder order , if exist create the subfolder
  39. def create_if_not_exist(folder_arrays):
  40. path = './folders/'
  41. for folder in folder_arrays:
  42. if pass_folder_type(folder) is True:
  43. path = path + folder
  44. try:
  45. ## if folder order 0 and created
  46. os.mkdir(path)
  47. print("Directory " , folder , " Created ")
  48. except FileExistsError:
  49. print("Directory " , folder , " already exists")
  50. path = path + '/'
  51. else:
  52. download_file(folder , path)
  53. with open('./digitalocean_space_data/data.json') as json_file:
  54. print('Beginning file download with wget module')
  55. space_data = json.loads(json_file.read())
  56. space_data_content = space_data['ListBucketResult']['Contents']
  57. for item in space_data_content:
  58. folder_manager(item['Key'])
  59. # url = 'http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg'
  60. # wget.download(url, './cat4.jpg')