Created
August 22, 2025 20:03
-
-
Save El3k0n/65fd8e8a4449acd46cfe69d6e16a3b33 to your computer and use it in GitHub Desktop.
An old script I made in 2013 to download all images from a Tumblr. Works with Python 2.7
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 | |
| # -*- coding: utf-8 -*- | |
| #Copyleft 2013 Diego Pignattini <El3k0n> | |
| import os, sys | |
| from shutil import copyfileobj | |
| from urllib import urlopen | |
| from xml.etree import ElementTree as ET | |
| if len(sys.argv) < 2 or len(sys.argv) > 3: | |
| print >> sys.stderr, "Pass tumblr name as argument, followed by a tag (optional)" | |
| sys.exit() | |
| if len(sys.argv) == 3: | |
| tag = sys.argv[2] | |
| else: | |
| tag = "" | |
| tumblr_name = sys.argv[1] | |
| api_endpoint = 'http://%s.tumblr.com/api/read' % tumblr_name | |
| start = 0 #Foto da cui cominciare, a partire dall'ultima postata | |
| num = 50 #Numero di foto da visualizzare per ogni richiesta | |
| post_count = 1 | |
| while post_count: | |
| resp = urlopen("%s?type=photo&tagged=%s&start=%s&num=%s" % (api_endpoint, tag, start, num)) | |
| #Le variabili start e num si riferiscono rispettivamente alla foto | |
| #(contata a partire dall'ultima foto postata) da cui cominciare e | |
| #al numero di foto da visualizzare per ogni richiesta. | |
| content = resp.read() #Legge il contenuto della pagina | |
| tree = ET.fromstring(content) #Crea l'albero dei tag | |
| post_tags = tree.findall(".//post") #Trova tutti i tag post | |
| post_count = len(post_tags) | |
| for post_tag in post_tags: | |
| post_id = post_tag.attrib['id'] | |
| post_date = post_tag.attrib['date-gmt'].split(" ")[0] | |
| #Per ogni post prende l'ID e la data in cui è stato postato | |
| outname = "%s-%s-%s" % (tumblr_name, post_date, post_id) | |
| #Il nome della foto una volta scaricata contiene l'ID e la data | |
| if os.path.exists(outname + '.gif') or os.path.exists(outname + '.jpeg'): | |
| #Controlla se esiste il file outname con estensione .gif o .jpeg | |
| print "%s already downloaded" % outname | |
| continue | |
| for photo_tag in post_tag.findall(".//photo-url"): | |
| if photo_tag.attrib['max-width'] == "1280": | |
| #Ora procede con il trovare tutti gli URL delle immagini | |
| #E salvando quelle a maggior risoluzione | |
| photo_url = photo_tag.text | |
| if '.gif' in photo_url: | |
| outname += '.gif' | |
| else: | |
| outname += '.jpeg' | |
| resp = urlopen(photo_url) #Apre l'URL con l'immagine | |
| with open(outname, 'wb') as w: | |
| w.write(resp.read()) | |
| w.close() | |
| print "Downloaded %s to %s" % (photo_url, outname) | |
| start += num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment