Last active
December 30, 2015 02:59
-
-
Save judotens/7766841 to your computer and use it in GitHub Desktop.
Find Indonesian song and lyrics from KapanLagi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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