2 # -*- coding: utf-8 -*-
4 __author__
= ("Dylan Lloyd <dylan@psu.edu>")
8 'notifications' : 'true',
9 # NOTIFICATIONS must be a string due to issues noted here:
10 # http://bugs.python.org/issue974019
11 # ConfigParser.getboolean fails when falling back to the default value
12 # if the value type is bool.
13 'youtube-dl' : '/usr/bin/youtube-dl',
14 'default_icon' : '/usr/share/icons/gnome/48x48/mimetypes/gnome-mime-application-x-shockwave-flash.png',
15 'youtube-dl_options' : '--no-progress --ignore-errors --continue --max-quality=22 -o "%(stitle)s---%(id)s.%(ext)s"'
18 import ConfigParser
# This module has been renamed to configparser in python 3.0
22 CONFIG_FILE
= os
.path
.join(os
.path
.expanduser('~'), '.i_like_pandora.config')
23 config
= ConfigParser
.ConfigParser(default_options
)
24 loaded_files
= config
.read(CONFIG_FILE
) # config.read returns an empty array if it fails.
25 if len(loaded_files
) == 0:
26 print 'Can\'t find a configuration file at', CONFIG_FILE
29 USER
= config
.get('settings', 'username')
30 DIR
= os
.path
.expanduser(config
.get('settings', 'download_folder'))
31 NOTIFICATIONS
= config
.getboolean('settings', 'notifications')
32 YT_DL
= config
.get('settings', 'youtube-dl')
33 DEFAULT_ICON
= config
.get('settings', 'default_icon')
35 print 'There is a formatting error in the configuration file at', CONFIG_FILE
38 from BeautifulSoup
import BeautifulSoup
43 import shlex
, subprocess
51 def fetch_stations(user
):
52 """ This takes a pandora username and returns the a list of the station tokens that the user is subscribed to. """
54 page
= urllib
.urlopen('http://www.pandora.com/favorites/profile_tablerows_station.vm?webname=' + USER
)
55 page
= BeautifulSoup(page
)
56 table
= page
.findAll('div', attrs
={'class':'station_table_row'})
59 for attr
, value
in row
.find('a').attrs
:
61 stations
.append(value
[10:])
64 def fetch_tracks(stations
):
65 """ Takes a list of station tokens and returns a list of Title + Artist strings.
68 for station
in stations
:
69 page
= urllib
.urlopen('http://www.pandora.com/favorites/station_tablerows_thumb_up.vm?token=' + station
+ '&sort_col=thumbsUpDate')
70 page
= BeautifulSoup(page
)
73 for span
in page
.findAll('span', attrs
={'class':'track_title'}):
74 for attr
, value
in span
.attrs
:
75 if attr
== 'tracktitle':
77 for anchor
in page
.findAll('a'):
78 artists
.append(anchor
.string
)
79 if len(titles
) == len(artists
):
82 search_string
= title
+ ' ' + artists
[i
]
83 search_strings
.append(search_string
)
86 # This would mean something strange has happened: there
87 # aren't the same number of titles and artist names on a
92 def search_youtube(search_strings
):
93 """ This takes a list of search strings and tries to find the first result. It returns a list of the youtube video ids of those results.
96 for search_string
in search_strings
:
97 search_url
= 'http://youtube.com/results?search_query=' + urllib
.quote_plus(search_string
)
98 page
= urllib
.urlopen(search_url
)
99 page
= BeautifulSoup(page
)
100 result
= page
.find('div', attrs
={'class':'video-main-content'})
102 print 'odd feedback for search, could not find div at ', search_url
104 for attr
, value
in result
.attrs
:
105 if attr
== 'id' and len(value
[19:]) == 11:
106 video_list
.append(value
[19:])
108 print 'odd feedback for search', search_url
, " : ", value
[19:]
112 def check_for_existing(video_list
):
113 """ Checks the download-folder for existing videos with same id and removes from video_list. """
114 filelist
= os
.listdir(DIR
)
116 for video
in copy
.deepcopy(video_list
):
117 for files
in filelist
:
118 if re
.search(video
,files
):
124 def fetch_videos(video_list
):
125 """ Uses subprocess to trigger a download using youtube-dl of the list created earlier, and triggers notifications if enabled. """
127 args
= shlex
.split(YT_DL
+ ' ' + YT_OPT
)
128 if NOTIFICATIONS
: regex
= re
.compile("\[download\] Destination: (.+)")
129 for video
in video_list
:
131 thread
= subprocess
.Popen(args
+ ["http://youtube.com/watch?v=" + video
], stdout
=subprocess
.PIPE
)
132 output
= thread
.stdout
.read()
134 video_file
= regex
.findall(output
)
135 if len(video_file
) == 0:
137 thumbnail
= hashlib
.md5('file://' + DIR
+ video_file
[0]).hexdigest() + '.png'
138 # Two '/'s instead of three because the path is
139 # absolute; I'm not sure how this'd work on windows.
140 title
, sep
, vid_id
= video_file
[0].rpartition('---')
141 title
= string
.replace(title
, '_', ' ')
142 thumbnail
= os
.path
.join(os
.path
.expanduser('~/.thumbnails/normal'), thumbnail
)
143 if not os
.path
.isfile(thumbnail
):
144 opener
= urllib2
.build_opener()
146 page
= opener
.open('http://img.youtube.com/vi/' + video
+ '/1.jpg')
148 # The thumbnail really should be saved to
149 # ~/.thumbnails/normal (Thumbnail Managing
151 # [http://jens.triq.net/thumbnail-spec/]
152 # As others have had problems anyway
153 # (http://mail.gnome.org/archives/gnome-list/2010-October/msg00009.html)
154 # I decided not to bother at the moment.
155 temp
= tempfile
.NamedTemporaryFile(suffix
='.jpg')
158 note
= pynotify
.Notification(title
, 'video downloaded', temp
.name
)
160 note
= pynotify
.Notification(title
, 'video downloaded', DEFAULT_ICON
)
162 # Generally, this will never happen, because the
163 # video is a new file.
164 note
= pynotify
.Notification(title
, 'video downloaded', thumbnail
)
168 stations
= fetch_stations(USER
)
169 if len(stations
) == 0:
170 print 'Are you sure your pandora profile is public? Can\'t seem to find any stations listed with your account.'
171 search_strings
= fetch_tracks(stations
)
172 videos
= search_youtube(search_strings
)
173 videos
= check_for_existing(videos
)
176 if __name__
== "__main__":