Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Classifier class and separated code into modules #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 49 additions & 87 deletions classifier/classifier.py
Original file line number Diff line number Diff line change
@@ -1,105 +1,67 @@
#!/usr/bin/env python
import os
import sys
import argparse
import arrow
import format_specifications

"""
All format lists were taken from wikipedia, not all of them were added due to extensions
not being exclusive to one format such as webm, or raw
Audio - https://en.wikipedia.org/wiki/Audio_file_format
Images - https://en.wikipedia.org/wiki/Image_file_formats
Video - https://en.wikipedia.org/wiki/Video_file_format
Documents - https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions
"""
class Classifier(object):

formats = format_specifications.formats

def moveto(file, from_folder, to_folder):
from_file = os.path.join(from_folder, file)
to_file = os.path.join(to_folder, file)
def __init__(self, args):

# to move only files, not folders
if os.path.isfile(from_file):
if not os.path.exists(to_folder):
os.makedirs(to_folder)
os.rename(from_file, to_file)


def classify(formats, output):
print("Scanning Files")

directory = os.getcwd()

for file in os.listdir(directory):
filename, file_ext = os.path.splitext(file)
file_ext = file_ext.lower()

for folder, ext_list in list(formats.items()):
folder = os.path.join(output, folder)

if file_ext in ext_list:
moveto(file, directory, folder)

print("Done!")
if not self.validate_args(args):
sys.exit()

self.args = args
self.date_format = 'DD-MM-YYYY'
self.output = os.getcwd() if self.args.output is None else self.args.output
self.current_dir = os.getcwd()

def classify_by_date(date_format, output_dir):
print("Scanning Files")
# filter out paths that are not files.
self.files = filter(os.path.isfile, os.listdir(self.current_dir))

directory = os.getcwd()
files = os.listdir(directory)
creation_dates = map(lambda x: (x, arrow.get(os.path.getctime(x))), files)
if self.args.date:
self.classify_by_date()
else:
self.classify()

for file, creation_date in creation_dates:
folder = creation_date.format(date_format)
moveto(file, directory, folder)
def classify(self):
print ("Scanning Files")

print("Done!")
for file in self.files:
filename, file_ext = os.path.splitext(file)
file_ext = file_ext.lower()

for folder, ext_list in list(self.formats.items()):
folder = os.path.join(self.output, folder)
if file_ext in ext_list:
self.moveto(file, self.current_dir, folder)
print ("Done!")

def main():
description = "Organize files in your directory instantly,by classifying them into different folders"
parser = argparse.ArgumentParser(description=description)
def classify_by_date(self):
print ("Scanning Files")

parser.add_argument("-st", "--specific-types", type=str, nargs='+',
help="Move all file extensions, given in the args list, in the current directory into the Specific Folder")
creation_dates = \
map(lambda x: (x, arrow.get(os.path.getctime(x))), self.files)

parser.add_argument("-sf", "--specific-folder", type=str,
help="Folder to move Specific File Type")
for file, creation_date in creation_dates:
folder = creation_date.format(self.date_format)
self.moveto(file, self.current_dir, folder)
print ("Done!")

parser.add_argument("-o", "--output", type=str,
help="Main directory to put organized folders")

parser.add_argument("-dt", "--date", action='store_true',
help="Organize files by creation date")

args = parser.parse_args()

formats = {
'Music' : ['.mp3', '.aac', '.flac', '.ogg', '.wma', '.m4a', '.aiff'],
'Videos': ['.flv', '.ogv', '.avi', '.mp4', '.mpg', '.mpeg', '.3gp', '.mkv', '.ts'],
'Pictures': ['.png', '.jpeg', '.gif', '.jpg', '.bmp', '.svg', '.webp', '.psd'],
'Archives': ['.rar', '.zip', '.7z', '.gz', '.bz2', '.tar', '.dmg', '.tgz'],
'Documents': ['.txt', '.pdf', '.doc', '.docx', '.xls', '.xlsv', '.xlsx',
'.ppt', '.pptx', '.ppsx', '.odp', '.odt', '.ods', '.md', '.json', '.csv'],
'Books': ['.mobi', '.epub'],
'RPMPackages': ['.rpm']
}

if bool(args.specific_folder) ^ bool(args.specific_types):
print(
'Specific Folder and Specific Types need to be specified together')
sys.exit()

if args.specific_folder and args.specific_types:
formats = {args.specific_folder: args.specific_types}

if args.output is None:
args.output = os.getcwd()

if args.date:
classify_by_date('DD-MM-YYYY', args.output)
else:
classify(formats, args.output)
def moveto(self, file, from_folder, to_folder):
from_file = os.path.join(from_folder, file)
to_file = os.path.join(to_folder, file)
if not os.path.exists(to_folder):
os.makedirs(to_folder)
os.rename(from_file, to_file)

sys.exit()
def validate_args(self, args):
if bool(args.specific_folder) ^ bool(args.specific_types):
print('Specific Folder and Specific Types need to be specified together')
return False
elif args.specific_folder and args.specific_types:
self.formats = {args.specific_folder: args.specific_types}
return True
else:
return True
20 changes: 20 additions & 0 deletions classifier/format_specifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

"""
All format lists were taken from wikipedia, not all of them were added due to extensions
not being exclusive to one format such as webm, or raw
Audio - https://en.wikipedia.org/wiki/Audio_file_format
Images - https://en.wikipedia.org/wiki/Image_file_formats
Video - https://en.wikipedia.org/wiki/Video_file_format
Documents - https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions
"""

formats = {
'Music' : ['.mp3', '.aac', '.flac', '.ogg', '.wma', '.m4a', '.aiff'],
'Videos': ['.flv', '.ogv', '.avi', '.mp4', '.mpg', '.mpeg', '.3gp', '.mkv', '.ts'],
'Pictures': ['.png', '.jpeg', '.gif', '.jpg', '.bmp', '.svg', '.webp', '.psd'],
'Archives': ['.rar', '.zip', '.7z', '.gz', '.bz2', '.tar', '.dmg', '.tgz'],
'Documents': ['.txt', '.pdf', '.doc', '.docx', '.xls', '.xlsv', '.xlsx',
'.ppt', '.pptx', '.ppsx', '.odp', '.odt', '.ods', '.md', '.json', '.csv'],
'Books': ['.mobi', '.epub'],
'RPMPackages': ['.rpm']
}
29 changes: 29 additions & 0 deletions classifier/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
import sys
import argparse
from classifier import Classifier

def main():
description = "Organize files in your directory instantly,by classifying them into different folders"
parser = argparse.ArgumentParser(description=description)

parser.add_argument("-st", "--specific-types", type=str, nargs='+',
help="Move all file extensions, given in the args list, in the current directory into the Specific Folder")

parser.add_argument("-sf", "--specific-folder", type=str,
help="Folder to move Specific File Type")

parser.add_argument("-o", "--output", type=str,
help="Main directory to put organized folders")

parser.add_argument("-dt", "--date", action='store_true',
help="Organize files by creation date")

args = parser.parse_args()

Classifier(args)

sys.exit()

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
packages=["classifier"],
entry_points="""
[console_scripts]
classifier = classifier.classifier:main
classifier = classifier.main:main
""",
install_requires=[
'arrow',
Expand Down