Created
June 26, 2015 09:38
-
-
Save AwwCookies/839a3d38a7a119188411 to your computer and use it in GitHub Desktop.
Download Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
# dm.py | |
# This script cleans up the downloads folder | |
# For uncompress to work you need to install p7ip-full | |
import glob | |
import os | |
import time | |
import subprocess | |
# Path to downloads folder | |
downloads_folder = "/home/aww/Downloads/" | |
# Decompress files automagicly | |
AUTO_DECOMPRESS = False | |
# Log File | |
log_f = "/home/aww/scripts/logs/dm.py.log" | |
# file exts | |
compressed = ['zip', 'tar'] | |
pictures = ['jpg', 'jpeg', 'gif', 'png', 'svg'] | |
videos = ['ogv', 'mp4'] | |
music = ['mp3', 'ogg', ] | |
documents = ['pdf','txt'] | |
# DO NOT EDIT BELOW # | |
def log(text): | |
print("dm.py [%i]: %s" % (time.time(), text)) | |
with open(log_f, "a+") as f: | |
f.write("[%i]: %s\n" % (time.time(), text)) | |
def move(op, np): | |
o_filename = op.split("/")[-1] | |
n_filename = np.split("/")[-1] | |
if op.split("/")[-1] in [f.split("/")[-1] for f in glob.glob('/'.join(np.split("/")[:-1]) + '/*')]: | |
log("[%s == %s] Duplicate file names" % (op, np)) | |
num = 0 | |
while True: # Run until we find a unused file name | |
num += 1 | |
fn = str(num) + "_" + n_filename | |
# If this file doesn't exists move the file over to the correct directory | |
if not os.path.exists('/'.join(np.split("/")[:-1]) + fn): | |
os.rename(op, '/'.join(np.split("/")[:-1]) + '/' + fn) | |
return '/'.join(np.split("/")[:-1]) + '/' + fn | |
else: | |
os.rename(op, np) | |
return np | |
def uncompress(f_path): | |
try: | |
subprocess.call(["7z", 'e', f_path, 'temp']) | |
except Exception as e: | |
raise e | |
# Create Directories | |
for dir_name in ["compressed", "pictures", "music", "documents", "videos", "other", 'extracted']: | |
if not os.path.exists(downloads_folder + dir_name): | |
os.mkdir(downloads_folder + dir_name) | |
log("mkdir %s" % downloads_folder + dir_name) | |
# Get a list of all the files and folders in the downloads folder | |
files = glob.glob(downloads_folder + "*") | |
for f in files: | |
# Make sure the file is not a folder | |
if not os.path.isdir(f): | |
# Compressed Files | |
if f.split(".")[-1] in compressed: | |
# Move the file into the compressed folder | |
path = move(f, downloads_folder + "compressed/" + f.split("/")[-1]) | |
log("%s to compressed" % f.split("/")[-1]) | |
if AUTO_DECOMPRESS: | |
uncompress(path) | |
# Picture | |
elif f.split(".")[-1] in pictures: | |
# Move the file into the pictures folder | |
move(f, downloads_folder + "pictures/" + f.split("/")[-1]) | |
log("Moved %s to pictures" % f.split("/")[-1]) | |
# Music | |
elif f.split(".")[-1] in music: | |
# Move the file into the music folder | |
move(f, downloads_folder + "music/" + f.split("/")[-1]) | |
log("Moved %s to music" % f.split("/")[-1]) | |
# Documents | |
elif f.split(".")[-1] in documents: | |
# Move the file into the ducments folder | |
move(f, downloads_folder + "documents/" + f.split("/")[-1]) | |
log("Moved %s to documents" % f.split("/")[-1]) | |
# Videos | |
elif f.split(".")[-1] in videos: | |
# Move the file into the videos folder | |
move(f, downloads_folder + "videos/" + f.split("/")[-1]) | |
log("Moved %s to videos" % f.split("/")[-1]) | |
# Everything else | |
else: | |
move(f, downloads_folder + 'other/' + f.split("/")[-1]) | |
log("Moved %s to other" % f.split("/")[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment