Skip to content

Instantly share code, notes, and snippets.

@veirus
Forked from dansku/dropboxOrganize.py
Last active February 27, 2016 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veirus/2db7f5126cf7241eafa4 to your computer and use it in GitHub Desktop.
Save veirus/2db7f5126cf7241eafa4 to your computer and use it in GitHub Desktop.
Script to organize pictures uploaded from Mobile Devices to Dropbox's Camera Upload folder
#!/usr/bin/env python34
# dror4 - datetime format edition | 2016.02.27
# Based on https://gist.github.com/dansku/9040240#file-dropboxorganize-py
import os.path
import glob
from datetime import date
def move(files, newname):
# Move files
try:
os.rename(files, newname)
except FileExistsError:
print('[!] File %s already exists, removing duplicate.' % newname)
os.remove(files)
else:
print('Movendo: {0} --> {1}'.format(files, newname).encode('utf-8'))
def main():
folder = os.getcwd()
print(folder)
fileFormats = ('ai', 'bmp', 'cdr', 'jpg', 'jpg-large', 'jpg-orig', 'jpeg', 'mov', 'png',
'png-large', 'png-orig', 'pdf', 'psd', 'mp4', 'gif', 'webm', 'webp',
'svg', 'flv')
twi = ('jpg-large', 'jpg-orig', 'png-large', 'png-orig')
months_ru = ('Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль',
'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь')
# Generate list of files
pp = (os.path.realpath(i) for f in fileFormats for i in glob.iglob("*.%s" % f))
for files in pp:
fixed = ''
# Retrieving creation date (getctime() is Windows only):
newsub = os.path.join(folder, '{:%Y-%m-%d}'.format(date.fromtimestamp(os.path.getctime(files))))
if files.endswith(twi):
fixed = os.path.splitext(files)[0] + os.path.splitext(files)[1][:4]
newname = os.path.join(newsub, os.path.basename(fixed or files))
# folder exists? if not, create them!
if not os.path.exists(newsub):
print('Making dir:', newsub)
os.makedirs(newsub)
move(files, newname)
print("Done :)")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment