-
Notifications
You must be signed in to change notification settings - Fork 3
/
picasa_downloader.py
68 lines (62 loc) · 2.35 KB
/
picasa_downloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Author: Anoop Jacob Thomas<anoopjt@gmail.com>
#Email: anoopjt@gmail.com
#Web: http://anoop.caremedia.org
#License: GPLv3 or later
from urllib2 import urlopen
from os.path import basename, join, exists
from os import mkdir
from urlparse import urlsplit
from BeautifulSoup import BeautifulStoneSoup
import sys
import time
# Based on http://stackoverflow.com/a/3160819
class ProgressBar(object):
def __init__(self, items):
self.display_width = 40
self.items = items
def update(self, n):
complete_frac = (float(n+1)/self.items)
complete = int(complete_frac * self.display_width)
remaining = self.display_width-complete
info = '%4.0f%% Done' %(complete_frac * 100,)
sys.stdout.write('[%s%s]%s' %('-' * complete, ' ' * remaining, info))
sys.stdout.flush()
if complete < self.display_width:
# add 2 to account for brackets [ ]
sys.stdout.write('\b' * (self.display_width+len(info)+2))
else:
sys.stdout.write('\n')
def get_feed_url(url):
# Code for getting address of picasa web
if urlsplit(url).path.startswith('/data/feed/'):
#if the link given is a feed - picasaweb
return url
elif urlsplit(url).netloc == "picasaweb.google.com":
y = urlopen(url).read()
soup = BeautifulStoneSoup(y,selfClosingTags=['meta','link','base'])
for link in soup.findAll('link'):
if link.has_key('rel'):
if link['rel']=="alternate":
url = link['href']
return url
def download_photos(url, location):
if not exists(location):
mkdir(location)
feed_content = urlopen(url).read()
tags = BeautifulStoneSoup(feed_content).findAll('media:content')
count = len(tags)
print "%d pictures found!" % count
progress_bar = ProgressBar(count)
for i, each in enumerate(tags):
if each.has_key('url'):
img = urlopen(each['url']).read()
fname = join(location, basename(urlsplit(each['url'])[2]))
output = open(fname,'wb')
output.write(img)
output.close()
progress_bar.update(i)
if __name__ == '__main__':
url = raw_input('Enter the Picasa web album/feed url : ')
url = get_feed_url(url)
name_album = raw_input('Enter name of Album : ')
download_photos(url, name_album)