Now stores user data in ~/.i_like_pandora.config
[i_like_pandora.git] / likes_pandora.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 __author__ = ("Dylan Lloyd <dylan@psu.edu>")
5 __license__ = "BSD"
6
7 default_options = {
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"'
16 }
17
18 import ConfigParser # This module has been renamed to configparser in python 3.0
19 import sys
20 import os
21
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
27 sys.exit()
28 try:
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')
34 except:
35 print 'There is a formatting error in the configuration file at', CONFIG_FILE
36 sys.exit()
37
38 from BeautifulSoup import BeautifulSoup
39 import urllib
40 import urllib2
41 import re
42 import copy
43 import shlex, subprocess
44
45 if NOTIFICATIONS:
46 import pynotify
47 import hashlib
48 import tempfile
49 import string
50
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. """
53 stations = []
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'})
57 for row in table:
58 if row.find('a'):
59 for attr, value in row.find('a').attrs:
60 if attr == 'href':
61 stations.append(value[10:])
62 return stations
63
64 def fetch_tracks(stations):
65 """ Takes a list of station tokens and returns a list of Title + Artist strings.
66 """
67 search_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)
71 titles = []
72 artists = []
73 for span in page.findAll('span', attrs={'class':'track_title'}):
74 for attr, value in span.attrs:
75 if attr == 'tracktitle':
76 titles.append(value)
77 for anchor in page.findAll('a'):
78 artists.append(anchor.string)
79 if len(titles) == len(artists):
80 i = 0
81 for title in titles:
82 search_string = title + ' ' + artists[i]
83 search_strings.append(search_string)
84 i += 1
85 else:
86 # This would mean something strange has happened: there
87 # aren't the same number of titles and artist names on a
88 # station page.
89 pass
90 return search_strings
91
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.
94 """
95 video_list = []
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'})
101 if result == None:
102 print 'odd feedback for search, could not find div at ', search_url
103 continue
104 for attr, value in result.attrs:
105 if attr == 'id' and len(value[19:]) == 11:
106 video_list.append(value[19:])
107 elif attr == 'id':
108 print 'odd feedback for search', search_url, " : ", value[19:]
109 return video_list
110
111
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)
115 i = 0
116 for video in copy.deepcopy(video_list):
117 for files in filelist:
118 if re.search(video,files):
119 del video_list[i]
120 i -= 1
121 i += 1
122 return video_list
123
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. """
126 os.chdir(DIR)
127 args = shlex.split(YT_DL + ' ' + YT_OPT)
128 if NOTIFICATIONS: regex = re.compile("\[download\] Destination: (.+)")
129 for video in video_list:
130 if video:
131 thread = subprocess.Popen(args + ["http://youtube.com/watch?v=" + video], stdout=subprocess.PIPE)
132 output = thread.stdout.read()
133 if NOTIFICATIONS:
134 video_file = regex.findall(output)
135 if len(video_file) == 0:
136 break
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()
145 try:
146 page = opener.open('http://img.youtube.com/vi/' + video + '/1.jpg')
147 thumb = page.read()
148 # The thumbnail really should be saved to
149 # ~/.thumbnails/normal (Thumbnail Managing
150 # Standard)
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')
156 temp.write(thumb)
157 temp.flush()
158 note = pynotify.Notification(title, 'video downloaded', temp.name)
159 except:
160 note = pynotify.Notification(title, 'video downloaded', DEFAULT_ICON)
161 else:
162 # Generally, this will never happen, because the
163 # video is a new file.
164 note = pynotify.Notification(title, 'video downloaded', thumbnail)
165 note.show()
166
167 def main():
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)
174 fetch_videos(videos)
175
176 if __name__ == "__main__":
177 main()