Created
November 26, 2016 21:00
-
-
Save djsmiley2k/e7f0fb27b2d8e937b1357afc15a120e5 to your computer and use it in GitHub Desktop.
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/python | |
import sys | |
import re | |
import os | |
import urllib | |
import urllib2 | |
import gdata.photos | |
import gdata.photos.service | |
pws = gdata.photos.service.PhotosService() | |
def main(): | |
username = sys.argv[1] | |
resume = '' | |
l_username = False | |
l_password = False | |
force = False | |
for arg in sys.argv: | |
if arg.startswith('--resume='): | |
resume = arg.split('--resume=')[1] | |
if arg.startswith('--username='): | |
l_username = arg.split('--username=')[1] | |
if arg.startswith('--password='): | |
l_password = arg.split('--password=')[1] | |
if arg == '--force': | |
force = True | |
if l_username and l_password: | |
pws.ClientLogin(l_username, l_password) | |
print 'Logged in as %s.' % (username) | |
else: | |
print 'Logged in as anonymous.' | |
print 'Grabbing %s\'s photos' % username | |
albums = pws.GetUserFeed(user=username) | |
for album in albums.entry: | |
process_album(album, username) | |
def process_album(album, username): | |
print 'Album named %s found, containing %s photos and ID of %s' %(album.name.text, album.numphotos.text, album.gphoto_id.text) | |
album_name = album.name.text | |
create_dir(album_name) | |
photos = pws.GetFeed('/data/feed/api/user/%s/albumid/%s?kind=photo&imgmax=d' %(username, album.gphoto_id.text)) | |
for photo in photos.entry: | |
#link = str(photo.GetHtmlLink()) # Returns XML | |
#print "Link is ", link | |
res = re.search("href=\"(.*?)\"", str(photo.GetHtmlLink()), 0) # Pull URL of photo display page from XML | |
if res.group(1) is not None: | |
print res.group(1) | |
else: | |
print 'Could not extract link' | |
return 0 | |
#print 'Photo title:', photo.title.text | |
#print photo.GetHtmlLink | |
try: | |
photo = photos[0] | |
print "Found some photos, continuing for %s" % (album_name) | |
except IndexError: | |
print 'No photos in %s' % (album_name) | |
return | |
gallery_link = str(photo.GetHtmlLink().href) | |
text = grab_url(gallery_link) | |
print "Text to match against is %s" % (text) | |
# found = re.findall('"width":"(.*?)","height":"(.*?)","size":"(.*?)","commentingEnabled":"(.*?)","allowNameTags":"(.*?)","media":\{"content":\[\{"url":"https:\/\/(.*?)",', text) | |
found = re.findall('https://get.google.com(.*?)', text) | |
print "Matched %s" % (found) | |
# url_list = [] | |
for object in found: | |
split = object[5].split('/') | |
split.append(split[5]) # extra item | |
if int(object[0]) > 2048: | |
size = '2048' | |
else: | |
size = object[0] | |
split[5] = 's' + size | |
url = 'https://' + '/'.join(split) | |
filename = split[6] | |
if os.path.isfile(filename) and (not force): | |
continue | |
print "found %s" % (url) | |
try: | |
download_image(url, album_name.lower(), filename) | |
except KeyboardInterrupt: | |
sys.exit(1) | |
except: | |
print 'Skipping: Error on %s' % (url) | |
print 'Successfully grabbed %s' % (split[6]) | |
# url_list.append(url) | |
print 'Finished %s' % (album_name) | |
def download_image(url, album, filename): | |
img = grab_url(url) | |
file = open(album + '/' + filename, 'w') | |
file.write(img) | |
file.close() | |
print "Downloaded %s" % (filename) | |
def grab_url(link): | |
obj = urllib.urlopen(link) | |
text = obj.read() | |
obj.close() | |
return text | |
def create_dir(name): | |
try: | |
os.mkdir(name.lower()) | |
except OSError: | |
pass | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment