Update crawl script and FileSystem/Video classes to support proper naming and organizing

This commit is contained in:
Jaisen Mathai 2015-10-06 01:28:00 -07:00
parent 6232810f42
commit a03c9c1d42
3 changed files with 62 additions and 10 deletions

View File

@ -13,11 +13,21 @@ filesystem = FileSystem()
for video_file in filesystem.get_all_files('/Users/jaisenmathai/Pictures/Videos/', Video.get_valid_extensions()):
video = Video(video_file)
filesystem.set_date_from_path_video(video)
metadata = video.get_metadata()
directory_name = filesystem.get_folder_name_by_date(metadata['date_taken'])
dest_directory = '%s/%s' % (destination, directory_name)
# TODO remove the day prefix of the file that was there prior to the crawl
file_name = filesystem.get_file_name_for_video(video)
dest_path = '%s/%s' % (dest_directory, file_name)
filesystem.create_directory(dest_directory)
print '%s -> %s' % (video_file, dest_path)
shutil.copy2(video_file, dest_path)
print '%s *** %s' % (video_file, file_name)
#print '%s/%s' % (directory_name, file_name)

View File

@ -3,12 +3,22 @@ Author: Jaisen Mathai <jaisen@jmathai.com>
Video package that handles all video operations
"""
import os
import re
import time
"""
General file system methods
"""
class FileSystem:
"""
Create a directory if it does not already exist..
@param, directory_name, string, A fully qualified path of the directory to create.
"""
def create_directory(self, directory_path):
if not os.path.exists(directory_path):
os.makedirs(directory_path)
"""
Recursively get all files which match a path and extension.
@ -34,6 +44,9 @@ class FileSystem:
"""
Generate file name for a video using its metadata.
We use an ISO8601-like format for the file name prefix.
Instead of colons as the separator for hours, minutes and seconds we use a hyphen.
https://en.wikipedia.org/wiki/ISO_8601#General_principles
@param, video, Video, A Video instance
@returns, string or None for non-videos
@ -46,7 +59,7 @@ class FileSystem:
if(metadata == None):
return None
file_name = '%s-%s.%s' % (time.strftime('%Y-%m-%d-%H-%M', metadata['date_taken']), metadata['base_name'], metadata['extension'])
file_name = '%s-%s.%s' % (time.strftime('%Y-%m-%d_%H-%M-%S', metadata['date_taken']), metadata['base_name'], metadata['extension'])
return file_name
"""
@ -59,10 +72,31 @@ class FileSystem:
return time.strftime('%Y-%m-%b', time_obj)
"""
Create a directory if it does not already exist..
@param, directory_name, string, A fully qualified path of the directory to create.
Set the modification time on the file based on the file path.
Noop if the path doesn't match the format YYYY-MM/DD-IMG_0001.JPG.
"""
def create_directory(self, directory_path):
if not os.path.exists(directory_path):
os.makedirs(dest_directory)
def set_date_from_path_video(self, video):
date_taken = None
video_file_path = video.get_file_path()
# Initialize date taken to what's returned from the metadata function.
# If the folder and file name follow a time format of YYYY-MM/DD-IMG_0001.JPG then we override the date_taken
(year, month, day) = [None] * 3
directory = os.path.dirname(video_file_path)
# If the directory matches we get back a match with groups() = (year, month)
year_month_match = re.search('(\d{4})-(\d{2})', directory)
if(year_month_match is not None):
(year, month) = year_month_match.groups()
day_match = re.search('^(\d{2})', os.path.basename(video.get_file_path()))
if(day_match is not None):
day = day_match.group(1)
# check if the file system path indicated a date and if so we override the metadata value
if(year is not None and month is not None):
if(day is not None):
date_taken = time.strptime('{}-{}-{}'.format(year, month, day), '%Y-%m-%d')
else:
date_taken = time.strptime('{}-{}'.format(year, month), '%Y-%m')
os.utime(video_file_path, (time.time(), time.mktime(date_taken)))

View File

@ -17,7 +17,7 @@ Video class for general video operations
"""
class Video(object):
# class / static variable accessible through get_valid_extensions()
__valid_extensions = ('avi','m4v','mov','mp4')
__valid_extensions = ('avi','m4v','mov','mp4','3gp')
"""
@param, source, string, The fully qualified path to the video file
@ -46,6 +46,14 @@ class Video(object):
return metadata
"""
Get the full path to the video.
@returns string
"""
def get_file_path(self):
return self.source
"""
Check the file extension against valid file extensions as returned by get_valid_extensions()