]>
gitweb.pimeys.fr Git - scripts-20-100.git/blob - download_picasa_album.py
2 # -*- encoding:utf-8 -*-
6 Pour télécharger un album Picasa sans avoir à utiliser l'application Picasa
8 Il faut lui fournir le l'url vers le flux RSS
17 """Récupère le xml décrivant l'album à partir de l'``url`` du flux RSS"""
18 page
= urllib
.urlopen(url
)
20 parsed
= lxml
.etree
.fromstring(content
)
23 def album_to_links(album
):
24 """Un peu de xpath pour récupérer les liens directs des éléments
26 medias
= album
.xpath("//media:group", namespaces
=album
.nsmap
)
29 contenturls
= media
.xpath("./media:thumbnail", namespaces
=media
.nsmap
)
30 contenturl
= contenturls
[-1]
31 contenturl
= [val
for (kw
, val
) in contenturl
.items() if kw
== "url"][0]
32 # On récupère la meilleure résolution de l'image
33 contenturl
= contenturl
.replace("s288", "s5000")
34 title
= media
.xpath("./media:title", namespaces
=media
.nsmap
)[0]
36 processed
.append([title
, contenturl
])
39 def download_file(url
, path
):
40 """Télécharge le fichier depuis l'``url`` et l'enregistre dans ``path``."""
41 page
= urllib
.urlopen(url
)
42 with
open(path
, "w") as f
:
43 bloc
= page
.read(4096)
46 bloc
= page
.read(4096)
48 def do_all(rss_url
, folder
):
49 """Télécharge tout l'album depuis ``rss_url`` et le stocke dans ``folder``"""
50 album
= get_album(rss_url
)
51 titles
= album
.xpath("//image/title", namespaces
=album
.nsmap
)
52 albumtitle
= titles
[0].text
53 os
.mkdir("%s/%s" % (folder
, albumtitle
))
54 linktitles
= album_to_links(album
)
55 for [title
, url
] in linktitles
:
56 print "Downloading %s : %s" % (title
, url
)
57 download_file(url
, "%s/%s/%s" % (folder
, albumtitle
, title
))
60 if __name__
== "__main__":
63 albumtitle
= do_all(rss_url
, ".")
64 print (u
"Téléchargé dans %s" % albumtitle
).encode("utf-8")