Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import string
- import codecs
- import unicodedata
- from time import gmtime, strftime
- if (len(sys.argv) != 2):
- print("Usage: sort_vods.py <twitch_vods_info_file>")
- sys.exit(0)
- vods_info_file_path = sys.argv[1];
- if not (os.path.isfile(vods_info_file_path)):
- print("Error: file", vods_info_file_path, "not found")
- sys.exit(0)
- all_unicode = ''.join(chr(i) for i in range(65536))
- unicode_letters = ''.join(c for c in all_unicode
- if unicodedata.category(c)=='Lu' or unicodedata.category(c)=='Ll')
- valid_chars = ";-_.() %s%s" % (unicode_letters, string.digits)
- max_file_name_length = 260
- log_file = open('sort_vods.log', 'w')
- log_file_time_format = "[%Y-%m-%d %H:%M:%S]"
- vods_info_file = codecs.open(vods_info_file_path, 'r', encoding='utf8')
- folders_created = 0
- files_moved = 0
- first_line_text = "Video Id";
- current_line = 1
- # read each line from vods info file
- for line in vods_info_file:
- vod_info_array = line.split(';')
- vod_info_array_size = len(vod_info_array)
- # skip header
- if (vod_info_array[0] == first_line_text):
- continue
- # check if vod title\description contains semicolons
- vod_title = ''
- expected_vod_info_array_size = 12
- if (vod_info_array_size > expected_vod_info_array_size):
- redundant_semicolons = vod_info_array_size - expected_vod_info_array_size
- for i in range(2, 3 + redundant_semicolons):
- vod_title = vod_title + vod_info_array[i]
- if (i != (2 + redundant_semicolons)):
- vod_title = vod_title + ';'
- else:
- vod_title = vod_info_array[2]
- vod_date = vod_info_array[vod_info_array_size - 8][0:10]
- # get video files links
- vod_flv_files = vod_info_array[vod_info_array_size - 2].split(',')
- for vod_flv_file in vod_flv_files:
- vod_flv_file_splitted = vod_flv_file.split('/')
- vod_flv_file_name = ''
- try:
- if (len(vod_flv_file_splitted) == 6): #old link format?
- vod_flv_file_name = vod_flv_file_splitted[5]
- else:
- vod_flv_file_name = vod_flv_file_splitted[6]
- except:
- vod_file_name_read_error = "line " + str(current_line) + " " \
- ". ERROR: cannot get vod flv file name from " + vod_flv_file
- print(vod_file_name_read_error)
- log_file.write(strftime(log_file_time_format, gmtime()) + " " +
- vod_file_name_read_error + '\n')
- continue
- if (os.path.isfile(vod_flv_file_name)):
- vod_folder = vod_date + " - " + vod_title
- if (len(vod_folder) > max_file_name_length):
- vod_folder = vod_folder[0:max_file_name_length - 1]
- vod_folder = ''.join(c for c in vod_folder if c in valid_chars)
- vod_folder = vod_folder.rstrip()
- # create new directory for each stream
- if (os.path.isdir(vod_folder)):
- folder_exists_message = "line " + str(current_line) + " " \
- "Folder " + vod_date + " - ... already exists"
- print(folder_exists_message)
- log_file.write(strftime(log_file_time_format, gmtime()) + " " +
- folder_exists_message + '\n')
- else:
- try:
- os.mkdir(vod_folder)
- if (os.path.isdir(vod_folder)):
- folder_created_message = "line " + str(current_line) + " " \
- "Folder " + vod_date + " - ... created"
- print(folder_created_message)
- log_file.write(strftime(log_file_time_format, gmtime()) + " " +
- folder_created_message + '\n')
- folders_created = folders_created + 1
- except:
- folder_create_exception = "line " + str(current_line) + " " \
- "ERROR: cannot create folder " + vod_folder
- print(folder_create_exception)
- log_file.write(strftime(log_file_time_format, gmtime()) + " " +
- folder_create_exception + '\n')
- # move video file to corresponding stream directory
- new_vod_flv_file_location = vod_folder + "/" + vod_flv_file_name
- try:
- os.rename(vod_flv_file_name, new_vod_flv_file_location)
- file_rename_message = "line " + str(current_line) + " " \
- "File " + vod_flv_file_name + " moved to the new folder"
- if (os.path.isfile(new_vod_flv_file_location)):
- print(file_rename_message)
- log_file.write(strftime(log_file_time_format, gmtime()) + " " +
- file_rename_message + '\n')
- files_moved = files_moved + 1
- except:
- file_rename_exception = "line " + str(current_line) + " " \
- "ERROR: cannot move file " + vod_flv_file_name + " " \
- "to " + new_vod_flv_file_location
- print(file_rename_exception)
- log_file.write(strftime(log_file_time_format, gmtime()) + " " +
- file_rename_exception + '\n')
- current_line = current_line + 1
- print('')
- log_file.write('\n')
- vods_info_file.close()
- total_folders_created_mesage = "Total folders created: " + str(folders_created)
- print(total_folders_created_mesage)
- log_file.write(strftime(log_file_time_format, gmtime()) + " " + total_folders_created_mesage + '\n')
- total_files_moved_mesage = "Total files moved: " + str(files_moved)
- print(total_files_moved_mesage)
- log_file.write(strftime(log_file_time_format, gmtime()) + " " + total_files_moved_mesage + '\n')
- log_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement