Skip to content

Instantly share code, notes, and snippets.

@milothiesen
Created June 1, 2017 15:55
Show Gist options
  • Save milothiesen/d44dfffe113815fd5f9509cfa141d296 to your computer and use it in GitHub Desktop.
Save milothiesen/d44dfffe113815fd5f9509cfa141d296 to your computer and use it in GitHub Desktop.
### need to copy to DEST without overwriting and add a -1 if its the first one and -2 if its the second....is my script right? check with multiple files for one stop.
### need to write to a new csv that contains metadata for the headers listed below
import os
import os.path
import sys
import shutil
import csv
import subprocess
infile = open("all_media_20170601.csv", "rU")
outfile = open("/Users/thiesm/Development/audioguideaudit-media/all_media_20170601-UPDATED.csv", "wb")
src = '/Users/thiesm/Development/audioguideaudit-media/media-copy'
dest = '/Users/thiesm/Development/audioguideaudit-media/media-test/dest'
reader = csv.DictReader(infile)
writer = csv.writer(outfile)
new_filename = ''
file_ext = ''
stop_number = ''
count = 0
site_code = ''
cmd = []
writer.writerow([
"Original Path",
"Original Filename",
"Web File Name",
"Acoustiguide File Name",
"Object ID",
"Title",
"Stop Number",
"Original XML file",
])
for root, dirs, filenames in os.walk(src):
for row in reader:
for f in filenames:
media_filename = str(f)
file_ext = os.path.splitext(media_filename)[1]
if os.path.splitext(media_filename)[0] in row['Original Media File Name'] and (file_ext == '.mp3' or file_ext == '.mp4'):
if row['active'] == 'TRUE' and row['Restricted'] == 'FALSE':
#BUILD SITE CODE NAMES
if row['Site'] == 'Cloisters':
site_code = 'CLO'
elif row['Site'] == 'Main':
site_code = '5TH'
elif row['Site'] == 'Breuer':
site_code = 'BRE'
stop_number = row['Stop Number']
print 'processing.......' + media_filename
print 'the site code is .......' + site_code
#BUILD LANGUAGE
lang = '/' + row['Language'].upper()
#BUILD FILE EXTENSION FOLDER NAMES
file_ext_folder = '/' + file_ext.replace(".", "").upper()
#BUILD DESTINATION ROUTE PATH
route_path = dest + '/' + site_code + lang + file_ext_folder
#IF THE STOP NUMBER IS BLANK, DON'T RENAME AND MOVE TO FOLDER 'LOST', 'LOST' means the files did not exist in any of the xml files listed on the report.
if stop_number == '':
stop_number = os.path.splitext(media_filename)[0]
route_path = dest + '/' + site_code + lang + 'lost'
print 'stop number is.... ' + stop_number
#MAKE ROUTE PATH FOLDERS IF THEY DON'T ALREADY EXIST
if not os.path.exists(route_path):
os.makedirs(route_path)
print 'routing to ..... ' + route_path
shutil.copyfile(src + '/' + media_filename, route_path + '/' + media_filename)
print 'copying to ..... ' + route_path + '/' + media_filename
#WHILE NEW FILE NAME ALREADY EXISTS, INCREMENT THE FILENAME
filename_incrementer = 1
dest_path = route_path + '/' + stop_number + '.' + str(filename_incrementer) + file_ext
def file_exist(dest_path, filename_incrementer):
if os.path.isfile(route_path + '/' + stop_number + '.' + str(filename_incrementer) + file_ext):
filename_incrementer += 1
dest_path = os.path.isfile(route_path + '/' + stop_number + '.' + str(filename_incrementer) + file_ext)
file_exist(dest_path, filename_incrementer)
#IF THE NEW FILE NAME DOESN'T EXIST, RENAME THE FILE WITH APPROPRIATE INCREMENTER
else: os.rename((route_path + '/' + media_filename), (route_path + '/' + stop_number + '.' + str(filename_incrementer) + file_ext))
print site_code
print 'renaming file to .......' + route_path + '/' + stop_number + '.' + str(filename_incrementer) + file_ext
file_exist(dest_path, filename_incrementer)
# IF FILES ARE NOT ACTIVE AND THEY ARE RESTRICTED MOVE THEM TO THE DARK/INACTIVE AND RESTRICTED FOLDER
elif row['active'] == 'FALSE' and row['Restricted'] == 'TRUE':
print 'processing INACTIVE AND RESTRICTED...' + media_filename
if not os.path.exists(dest + '/DARK/INACTIVE AND RESTRICTED'):
os.makedirs(dest + '/DARK/INACTIVE AND RESTRICTED')
shutil.copyfile((src + '/' + media_filename), (dest + '/DARK/INACTIVE AND RESTRICTED/' + media_filename))
# IF FILES ARE NOT ACTIVE AND THEY ARE UNRESTRICTED, MOVE THEM TO THE DARK/INACTIVE ONLY FOLDER
elif row['active'] == 'FALSE' and row['Restricted'] == 'FALSE':
print 'processing INACTIVE ONLY...' + media_filename
if not os.path.exists(dest + '/DARK/INACTIVE ONLY/'):
os.makedirs(dest + '/DARK/INACTIVE ONLY/')
shutil.copyfile((src + '/' + media_filename), (dest + '/DARK/INACTIVE ONLY/' + media_filename))
# IF FILES ARE ACTIVE AND THEY ARE RESTRICTED, MOVE THEM TO THE DARK/RESTRICTED ONLY FOLDER
elif row['active'] == 'TRUE' and row['Restricted'] == 'TRUE':
print 'processing RESTRICTED ONLY...' + media_filename
if not os.path.exists(dest + '/DARK/RESTRCITED ONLY/'):
os.makedirs(dest + '/DARK/RESTRCITED ONLY/')
shutil.copyfile((src + '/' + media_filename), (dest + '/DARK/RESTRCITED ONLY/' + media_filename))
# if os.path.splitext(media_filename)[0] in row['image']:
# stop_number = row['Stop Number']
# file_ext = os.path.splitext(media_filename)[1]
# print 'processing.......' + media_filename
# site = '/' + row['Site']
# lang = '/' + row['Language'].upper()
# file_ext_folder = '/' + file_ext.replace(".", "").upper()
# route_path = dest + site + lang + file_ext_folder
# if stop_number == '':
# stop_number = os.path.splitext(media_filename)[0]
# print 'stop number is.... ' + stop_number
# if not os.path.exists(route_path):
# os.makedirs(route_path)
# print 'routing to ..... ' + route_path
# shutil.copyfile(src + '/' + media_filename, route_path + '/' + media_filename)
# print 'copying to ..... ' + route_path + '/' + media_filename
# os.rename(route_path + '/' + media_filename, route_path + '/' + stop_number + '.1' + file_ext)
# print 'renaming to .....' + stop_number + '.1' + file_ext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment