Created
September 24, 2012 19:26
-
-
Save asdrubalivan/3777796 to your computer and use it in GitHub Desktop.
Tunein's simple parser
This file contains 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
# -*- coding: utf-8 -*- | |
''' | |
Simple Tunein's Streaming's URL parser | |
@author: Asdrúbal Suárez | |
@license: Public Domain | |
@version: 0.1 | |
@contact: twitter: @asdrubalivan | |
@usage: tunein_finder.py -url http://tunein.com/radio/EnlaRed-Radio-s89954/ (Don't forget to use the http:// at the beginning) | |
''' | |
import argparse | |
import urllib2 | |
import re | |
import sys | |
def read_url(url): | |
usock = urllib2.urlopen(url) | |
data = usock.read() | |
usock.close() | |
return data | |
def parse_streamid(url): | |
ret_val = [] | |
regex_radio_url = r'"StreamUrl":"(?P<url>.*)"' | |
regex_streamings_url = r"""http://[^"]*""" | |
pattern_radio_url = re.compile(regex_radio_url) | |
data = read_url(url) | |
raw_streamings_url = pattern_radio_url.search(data) | |
url_streamings = raw_streamings_url.group("url") | |
data_url_streamings = read_url(url_streamings) | |
pattern_streamings_url = re.compile(regex_streamings_url) | |
for match in pattern_streamings_url.finditer(data_url_streamings): | |
ret_val.append(match.group(0)) | |
return ret_val | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Process the URL and find the streamings") | |
parser.add_argument('-url', help='The Tunein radio URL that should be parsed') | |
args = parser.parse_args() | |
if not len(sys.argv[1:]) > 0: | |
parser.print_help() | |
else: | |
try: | |
results = parse_streamid(args.url) | |
except: | |
print "Error" | |
sys.exit(1) | |
for result in results: | |
print result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment