Advertisement
NavitaK

sort_vods

Aug 17th, 2014
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.99 KB | None | 0 0
  1. import os
  2. import sys
  3. import string
  4. import codecs
  5. import unicodedata
  6. from time import gmtime, strftime
  7.  
  8. if (len(sys.argv) != 2):
  9.     print("Usage: sort_vods.py <twitch_vods_info_file>")
  10.     sys.exit(0)
  11.  
  12. vods_info_file_path = sys.argv[1];
  13. if not (os.path.isfile(vods_info_file_path)):
  14.     print("Error: file", vods_info_file_path, "not found")
  15.     sys.exit(0)
  16.  
  17. all_unicode = ''.join(chr(i) for i in range(65536))
  18. unicode_letters = ''.join(c for c in all_unicode
  19.                           if unicodedata.category(c)=='Lu' or unicodedata.category(c)=='Ll')
  20.  
  21. valid_chars = ";-_.() %s%s" % (unicode_letters, string.digits)
  22. max_file_name_length = 260
  23.  
  24. log_file = open('sort_vods.log', 'w')
  25. log_file_time_format = "[%Y-%m-%d %H:%M:%S]"
  26.  
  27. vods_info_file = codecs.open(vods_info_file_path, 'r', encoding='utf8')
  28.  
  29. folders_created = 0
  30. files_moved = 0
  31. first_line_text = "Video Id";
  32. current_line = 1
  33.  
  34. # read each line from vods info file
  35. for line in vods_info_file:
  36.     vod_info_array = line.split(';')
  37.     vod_info_array_size = len(vod_info_array)
  38.  
  39.     # skip header
  40.     if (vod_info_array[0] == first_line_text):
  41.         continue
  42.  
  43.     # check if vod title\description contains semicolons
  44.     vod_title = ''
  45.     expected_vod_info_array_size = 12
  46.     if (vod_info_array_size > expected_vod_info_array_size):
  47.         redundant_semicolons = vod_info_array_size - expected_vod_info_array_size
  48.         for i in range(2, 3 + redundant_semicolons):
  49.             vod_title = vod_title + vod_info_array[i]
  50.             if (i != (2 + redundant_semicolons)):
  51.                 vod_title = vod_title + ';'
  52.     else:
  53.         vod_title = vod_info_array[2]
  54.  
  55.     vod_date = vod_info_array[vod_info_array_size - 8][0:10]
  56.     # get video files links
  57.     vod_flv_files = vod_info_array[vod_info_array_size - 2].split(',')
  58.  
  59.     for vod_flv_file in vod_flv_files:
  60.  
  61.         vod_flv_file_splitted = vod_flv_file.split('/')
  62.  
  63.         vod_flv_file_name = ''
  64.         try:
  65.             if (len(vod_flv_file_splitted) == 6): #old link format?
  66.                 vod_flv_file_name = vod_flv_file_splitted[5]
  67.             else:
  68.                 vod_flv_file_name = vod_flv_file_splitted[6]
  69.         except:
  70.             vod_file_name_read_error = "line " + str(current_line)  + " " \
  71.                                        ". ERROR: cannot get vod flv file name from " + vod_flv_file
  72.             print(vod_file_name_read_error)
  73.             log_file.write(strftime(log_file_time_format, gmtime()) + " " +
  74.                                vod_file_name_read_error + '\n')
  75.             continue
  76.  
  77.         if (os.path.isfile(vod_flv_file_name)):
  78.             vod_folder = vod_date + " - " + vod_title
  79.             if (len(vod_folder) > max_file_name_length):
  80.                 vod_folder = vod_folder[0:max_file_name_length - 1]
  81.             vod_folder = ''.join(c for c in vod_folder if c in valid_chars)
  82.             vod_folder = vod_folder.rstrip()
  83.  
  84.             # create new directory for each stream
  85.             if (os.path.isdir(vod_folder)):
  86.                 folder_exists_message = "line " + str(current_line) + " " \
  87.                                         "Folder " + vod_date + " - ... already exists"
  88.                 print(folder_exists_message)
  89.                 log_file.write(strftime(log_file_time_format, gmtime()) + " " +
  90.                                folder_exists_message + '\n')
  91.             else:
  92.                 try:
  93.                     os.mkdir(vod_folder)
  94.                     if (os.path.isdir(vod_folder)):
  95.                         folder_created_message = "line " + str(current_line) + " " \
  96.                                                  "Folder " + vod_date + " - ... created"
  97.                         print(folder_created_message)
  98.                         log_file.write(strftime(log_file_time_format, gmtime()) + " " +
  99.                                        folder_created_message + '\n')
  100.                         folders_created = folders_created + 1
  101.                 except:
  102.                     folder_create_exception = "line " + str(current_line) + " " \
  103.                                               "ERROR: cannot create folder " + vod_folder
  104.                     print(folder_create_exception)
  105.                     log_file.write(strftime(log_file_time_format, gmtime()) + " " +
  106.                                    folder_create_exception + '\n')
  107.  
  108.             # move video file to corresponding stream directory
  109.             new_vod_flv_file_location = vod_folder + "/" + vod_flv_file_name
  110.             try:
  111.                 os.rename(vod_flv_file_name, new_vod_flv_file_location)
  112.                 file_rename_message = "line " + str(current_line) + " " \
  113.                                       "File " + vod_flv_file_name + " moved to the new folder"
  114.                 if (os.path.isfile(new_vod_flv_file_location)):
  115.                     print(file_rename_message)
  116.                     log_file.write(strftime(log_file_time_format, gmtime()) + " " +
  117.                                    file_rename_message + '\n')
  118.                     files_moved = files_moved + 1
  119.             except:
  120.                 file_rename_exception = "line " + str(current_line) + " " \
  121.                                         "ERROR: cannot move file " + vod_flv_file_name + " " \
  122.                                         "to " + new_vod_flv_file_location
  123.                 print(file_rename_exception)
  124.                 log_file.write(strftime(log_file_time_format, gmtime()) + " " +
  125.                                file_rename_exception + '\n')
  126.  
  127.     current_line = current_line + 1
  128.  
  129. print('')
  130. log_file.write('\n')
  131.  
  132. vods_info_file.close()
  133.  
  134. total_folders_created_mesage = "Total folders created: " + str(folders_created)
  135. print(total_folders_created_mesage)
  136. log_file.write(strftime(log_file_time_format, gmtime()) + " " + total_folders_created_mesage + '\n')
  137.  
  138. total_files_moved_mesage = "Total files moved: " + str(files_moved)
  139. print(total_files_moved_mesage)
  140. log_file.write(strftime(log_file_time_format, gmtime()) + " " + total_files_moved_mesage + '\n')
  141.  
  142. log_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement