Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find Indonesian song and lyrics from KapanLagi
from StringIO import StringIO
import gzip, BeautifulSoup, sys, urllib2, urllib
main_url = "http://lirik.kapanlagi.com"
def buka(url):
request = urllib2.Request(url)
request.add_header('Accept-encoding', 'gzip')
response = urllib2.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO( response.read())
f = gzip.GzipFile(fileobj=buf)
data = f.read()
else: data = response.read()
return data
def get_lagu(artis):
url = main_url + "/artis/" + artis
isi = buka(url)
bs = BeautifulSoup.BeautifulSoup(isi)
#bs.findAll('div', attrs={'class': 'lyrics-body'})
out = bs.findAll('a', attrs={'style': 'font:16px arial, sans-serif; font-weight:bold;'})
keluar = []
for o in out:
if o['href']:
jdl = urllib.unquote(urllib.unquote(o['href'].replace("/artis/" + artis + "/", "").replace("_", " ")))
keluar.append(jdl)
return keluar
def get_lirik(artis, judul):
url = "%s/artis/%s/%s" % (str(main_url), str(artis), str(judul))
isi = buka(url)
bs = BeautifulSoup.BeautifulSoup(isi)
#bs.findAll('div', attrs={'class': 'lyrics-body'})
out = bs.findAll('span', attrs={'class': 'lirik_line'})
lirik = ""
for o in out:
lirik += o.text + "\n"
return lirik
if __name__ == "__main__":
import sys
perintah = sys.argv[0] + " <daftar|cari> <artis> <judul>"
try:
cmd = sys.argv[1].lower()
except:
cmd = ""
print perintah
sys.exit()
artis = sys.argv[2].lower().replace(" ", "_")
if cmd == "daftar": print "\n".join(get_lagu(artis))
elif cmd == "cari":
judul = sys.argv[3].lower().replace(" ", "_")
print get_lirik(artis, judul)
else: print perintah
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment