Skip to content

Instantly share code, notes, and snippets.

@robjwells
Last active January 4, 2016 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robjwells/f26569278cd0d4bc49ed to your computer and use it in GitHub Desktop.
Save robjwells/f26569278cd0d4bc49ed to your computer and use it in GitHub Desktop.
Really terrible scripts to scrape Tunefind, get the tracks into Spotify and then out to iTunes.

These are to accompany a post on my blog. Please do not use them verbatim. Read the post for details and analysis of the mistakes and terrible decisions I made.

#!/usr/local/bin/python3
import re
import json
from bs4 import BeautifulSoup
from urllib.request import urlopen
base_url = 'http://www.tunefind.com'
seasons_index = '/show/entourage'
response = urlopen(base_url + seasons_index)
response_text = response.read().decode()
soup = BeautifulSoup(response_text)
seasons_div = soup.find('div', class_='lefttext sidebarIndent')
seasons_urls = [(base_url + a_tag['href'])
for a_tag in seasons_div.find_all('a')]
tracks_list = []
for s in seasons_urls:
season = BeautifulSoup(urlopen(s).read().decode())
episode_urls = [(base_url + a_tag['href']) for a_tag
in season.find_all('a',
{'name': re.compile(r'episode\d+')})]
for e in episode_urls:
episode = BeautifulSoup(urlopen(e).read().decode())
for raw in episode.find_all(class_='tf-songevent-text'):
match = re.search(r'(.+)\n\s+by (.+)', raw.text.strip())
if match:
tracks_list.append(match.groups())
for track in tracks_list:
# filter list for duplicates
if tracks_list.count(track) > 1:
tracks_list.remove(track)
with open('/Users/robjwells/Desktop/tracks.json', 'w') as tracks_json:
json.dump(tracks_list, tracks_json)
#!/usr/local/bin/python3
import os
import sys
import requests
from bs4 import BeautifulSoup as bs
spot_path = os.path.join(os.getcwd(), sys.argv[1])
itunes_search = 'https://itunes.apple.com/search?term={}&country=gb'
with open(spot_path) as spot_file:
spot_links = spot_file.read().splitlines()
def spotify_to_itunes(link):
spot_soup = bs((requests.get(link)).text)
title = spot_soup.h1.text
artist = spot_soup.h2.a.text
plussed = '+'.join([title, artist]).replace(' ', '+')
itunes_response = requests.get(itunes_search.format(plussed))
if itunes_response.json()['resultCount']:
return itunes_response.json()['results'][0]['trackViewUrl']
else:
return ' '.join([artist, title])
for line in spot_links:
print(spotify_to_itunes(line))
#!/usr/local/bin/python3
import json
import subprocess
position = open('/Users/robjwells/Desktop/position', 'r+')
reported = open('/Users/robjwells/Desktop/reported', 'a')
def asrun(script):
"Run the given AppleScript and return the standard output and error."
osa = subprocess.Popen(['osascript', '-'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
return osa.communicate(script.encode())[0]
with open('/Users/robjwells/Desktop/tracks.json') as tracks_json:
tracks_list = json.load(tracks_json)
ascript = '''
tell application "LaunchBar"
perform action "Open Location" with string "spotify:search:{0}"
end tell
tell application "Finder"
set dialog_result to display dialog "Ready for next track?" buttons {{"Report", "Stop", "Next"}} default button "Next"
return button returned of dialog_result
end tell
'''
def prep_track(t):
joint = ' '.join(t)
return joint.replace(' ', '+')
def prompt(t):
script = ascript.format(prep_track(t))
result = asrun(script).decode().strip()
if result == 'Stop':
return False
elif result == 'Report':
reported.write(json.dumps(t))
reported.write('\n')
position.seek(0)
position.write(str(tracks_list.index(t) + 1))
return True
raw_pos = position.read()
if raw_pos:
pos = int(raw_pos)
else:
pos = 0
for track in tracks_list[pos:]:
if not prompt(track):
break
position.close()
reported.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment