Last active
May 16, 2016 02:26
-
-
Save seveibar/696c5b8b9b51a3477b92a509652c1b57 to your computer and use it in GitHub Desktop.
Organizes downloads in your downloads directory by time. Puts files into "_hours", "_days", "_weeks"
This file contains hidden or 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/python | |
import os.path as path,os,json, time | |
# Get path to temporary files for scripts directory | |
tmpDir = path.join(path.dirname(path.realpath(__file__)), "tmp") | |
class File: | |
def __init__(self, name, path, creationTime): | |
self.name = name | |
self.path = path | |
self.time = creationTime | |
def exists(self): | |
return path.exists(self.path) | |
def serialize(self): | |
return { | |
"name": self.name, | |
"path": self.path, | |
"time": self.time | |
} | |
# Move file to new directory | |
def moveDir(self, new_dir_path): | |
fullPath = path.join(new_dir_path, self.name) | |
if self.path != fullPath: | |
os.rename(self.path, fullPath) | |
self.path = fullPath | |
# Returns age in hours | |
def age(self): | |
return (time.time() - self.time) / 3600 | |
# Parse file for storing date information | |
files = [] # [File] | |
downloadFilesPath = path.join(tmpDir, "downloads_files.json") | |
try: | |
jsonFileList = json.load(open(downloadFilesPath)) | |
for jsonFile in jsonFileList: | |
files.append(File(jsonFile['name'], jsonFile['path'], jsonFile['time'])) | |
except: | |
print("Creating new downloads_files") | |
def findFile(path): | |
for f in files: | |
if f.path == path: | |
return f | |
return None | |
# Let's see if there are any new files | |
currentTime = time.time() | |
downloadsPath = "~/downloads" | |
new_filenames = [p for p in os.listdir(downloadsPath) if p[0] != "_"] | |
new_filepaths = [path.join(downloadsPath, p) for p in new_filenames] | |
for name, fpath in zip(new_filenames, new_filepaths): | |
if findFile(fpath) is None: | |
files.append(File(name, fpath, currentTime)) | |
# Make sure that the downloads directories exist | |
_lastweek = path.join(downloadsPath, "_weeks") | |
_days = path.join(downloadsPath, "_days") | |
_hours = path.join(downloadsPath, "_hours") | |
# Make sure the directories exist | |
try: os.mkdir(_lastweek) | |
except: pass | |
try: os.mkdir(_yest) | |
except: pass | |
try: os.mkdir(_hours) | |
except: pass | |
# Now let's shuffle files around | |
def moveFiles(files): | |
for f in files: | |
if not f.exists(): | |
continue | |
else: | |
if f.age() > 24 * 7: | |
f.moveDir(_lastweek) | |
elif f.age() > 24: | |
f.moveDir(_days) | |
elif f.age() > 4: | |
f.moveDir(_hours) | |
yield f | |
# Serialize and write new files | |
newJsonFiles = [f.serialize() for f in list(moveFiles(files))] | |
json.dump(newJsonFiles, open(downloadFilesPath, 'w')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment