<repo_name>amanda/lyrics_scraper<file_sep>/az_scraper.py
#!usr/bin/env python
# -*- coding: utf-8 -*-

'''a shitty little a-z lyrics scraper for texty fun.
returns a json file of all the lyrics by a given artist by song.
azlyrics doesn't like scraping (eep) so use with caution!'''

from pattern.web import URL, DOM, abs, plaintext
import re, argparse, json

BASE_URL = 'http://www.azlyrics.com/'

def all_lyrics(artist):
	clean = re.sub(r"\s+|'", '', artist)
	url = URL(BASE_URL + artist[0] + '/' + clean + '.html')
	dom = DOM(url.download())
	titles = [a.content for a in dom('div#listAlbum a')]
	ew_amazon = [abs(link.attributes.get('href', ''), base=url.redirect or
		url.string) for link in dom('div#listAlbum a')]
	songlinks = [l for l in ew_amazon if 'amazon' not in l]
	lyrics = []
	for link in songlinks:
		song_url = URL(link)
		song_dom = DOM(song_url.download())
		lyrics.append(plaintext(song_dom('div#main div')[4:5][0].content))
	zippy_lyrics = zip(titles, lyrics)
	return json.dumps(zippy_lyrics, sort_keys=True)

if __name__ == '__main__':
	parser = argparse.ArgumentParser()
	parser.add_argument('artist', type=str, help='artist to get lyrics from.')
	args = parser.parse_args()
	fname = args.artist + '-lyrics.json'
	with open(fname, 'w') as f:
		f.write(all_lyrics(args.artist))
<file_sep>/requirements.txt
Pattern==2.6
argparse==1.3.0
wsgiref==0.1.2
<file_sep>/README.md
# lyrics_scraper

a simple lyrics scraper made to play with the [pattern-web module](http://www.clips.ua.ac.be/pages/pattern-web).

## usage:

```
pip install -r requirements.txt
python az_scraper.py 'artistname'
```
returns a json file of lyrics by the specified artist.

## todo:
- learn that i need to stop trying to scrape websites without APIs, they don't have APIs for a reason you're just going to get your ip banned :'(
