Skip to content

Instantly share code, notes, and snippets.

@redacted
Created May 4, 2012 10:16
Show Gist options
  • Save redacted/2593814 to your computer and use it in GitHub Desktop.
Save redacted/2593814 to your computer and use it in GitHub Desktop.
Opens a song from the iTunes Top 100 in youtube (chosen at random)
'''
1) Gets top 100 songs from iTunes chart
2) Plays a random song from this list in YouTube
license: public domain
Dependancies:
1) BeautifulSoup http://www.crummy.com/software/BeautifulSoup/
'''
from urllib2 import urlopen
from webbrowser import open_new_tab
from random import choice
from BeautifulSoup import BeautifulSoup
def get_song_list():
'''Return the top 100 songs from Apple's iTunes charts.'''
URL = 'http://www.apple.com/itunes/charts/songs/'
soup = BeautifulSoup(urlopen(URL).read())
titles = (sp.a.string.encode('ascii', 'ignore') for sp in soup('h3')[:99]
if len(sp('a')) == 1)
artists = (sp.a.string.encode('ascii', 'ignore') for sp in soup('h4')[:99]
if len(sp('a')) == 1)
return ['{0} {1}'.format(a, t) for a, t in zip(artists, titles)]
def play_song(search_term):
'''Play song in YouTube.'''
URL = ("https://www.google.com/search?btnI=I'm+Feeling+Lucky&ie=UTF-8"
"&oe=UTF-8&q=site:youtube.com%20inurl:http://www.youtube.com/"
"watch?v=%20{0}".format(search_term))
open_new_tab(URL)
def main():
songs = get_song_list()
play_song(choice(songs))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment