Skip to content

Instantly share code, notes, and snippets.

@pmow
Created December 15, 2018 16:35
Show Gist options
  • Save pmow/4bbd74df02315913429378ffb5cb4a96 to your computer and use it in GitHub Desktop.
Save pmow/4bbd74df02315913429378ffb5cb4a96 to your computer and use it in GitHub Desktop.
flickr_flatten
import os
import fnmatch
import json
from functions import *
verbose = 0
jsonpath= '/mnt/media2/Flickr.Dump/json/'
mediapath= '/mnt/media2/Flickr.Dump/media/'
outputpath= '/mnt/media2/Flickr.Dump/output/'
for root, dirs, files in os.walk(jsonpath):
for filename in fnmatch.filter(files,'photo_*.json'):
if verbose: print('Reading '+ os.path.join(root, filename))
fullfilenamepath = os.path.join(root, filename)
j_obj = json.load(open(fullfilenamepath,'r'))
if len(j_obj['albums']) == 0:
print "Id: ", j_obj['id'], 'is without album'
copyphoto(j_obj['id'],mediapath,outputpath,'noAlbum')
elif len(j_obj['albums']) == 1:
print "Id: ", j_obj['id'], ' goes in album ', j_obj['albums'][0]['title']
copyphoto(j_obj['id'],mediapath,outputpath,j_obj['albums'][0]['title'])
elif len(j_obj['albums']) == 2:
print "Id: ", j_obj['id'], ' goes in album ', j_obj['albums'][0]['title']
print "Id: ", j_obj['id'], ' goes in album ', j_obj['albums'][1]['title']
copyphoto(j_obj['id'],mediapath,outputpath,j_obj['albums'][0]['title'])
copyphoto(j_obj['id'],mediapath,outputpath,j_obj['albums'][1]['title'])
else:
print "Id: ", j_obj['id'], ' has ', len(j_obj['albums']),' albums! Error!'
import os
import fnmatch
from shutil import copyfile
def copyphoto(flickrid,frompath,topath,album,negated = 0):
print "flickrid is "+flickrid,' frompath is '+frompath,' topath is '+topath,' album is '+album
if not os.path.exists(topath+'/'+album):
os.makedirs(topath+'/'+album)
for root, dirs, files in os.walk(frompath):
for filename in fnmatch.filter(files,'*_'+flickrid+'_o.*'):
print "Copying "+filename+" to "+topath
if negated == 0:
copyfile(os.path.join(root,filename),topath+'/'+album+'/'+filename)
@pmow
Copy link
Author

pmow commented Dec 15, 2018

Will take dir full of JSON and MEDIA, and arrange media in folders according to the JSON albums.
This is the Flickr format when you hit the "download all" button.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment