Skip to content

Instantly share code, notes, and snippets.

@takscape
Created January 31, 2012 15:28
Show Gist options
  • Save takscape/1711078 to your computer and use it in GitHub Desktop.
Save takscape/1711078 to your computer and use it in GitHub Desktop.
Python: スカパーのチャンネル一覧取得
# -*- coding: utf-8 -*-
import requests
import re
from BeautifulSoup import BeautifulSoup
MASTER_URL = "http://www.skyperfectv.co.jp/search/sky/S5_1/InfoInput.html"
PROGRAM_URL = "http://www.skyperfectv.co.jp/search/sky/S9_1/Program.html"
NAME_EXTRACTOR = re.compile(r'name\s*:\s*"([^"]+)"')
CHANNEL_EXTRACTOR = re.compile(r'chAr\[(\d+)\]\s*=\s*"([^"]+)"')
def get_channels():
r = requests.post(MASTER_URL, {"getmaster": 1, "master_mode": "A"})
bs = BeautifulSoup(unicode(r.content, "Shift_JIS"))
scripts = bs.head.findAll("script")
master = filter(lambda x: not x.has_key("src"), scripts)[0]
lines = master.text.splitlines()
genres = {}
channels = []
cur_genre = None
for line in lines:
line = line.strip()
if line.startswith("genAr.push"):
m = NAME_EXTRACTOR.search(line)
if (cur_genre is not None) and len(channels) > 0:
genres[cur_genre] = channels
cur_genre = m.group(1)
channels = []
elif line.startswith("chAr"):
m = CHANNEL_EXTRACTOR.search(line)
chid = m.group(1)
chname = m.group(2)
chname = chname[chname.index(" ")+1:]
channels.append((chid, chname))
if (cur_genre is not None) and len(channels) > 0:
genres[cur_genre] = channels
return genres
if __name__ == '__main__':
genres = get_channels()
for g,cs in genres.items():
print g
for c in cs:
print (c[0] + ": " + c[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment