Skip to content

Instantly share code, notes, and snippets.

@staybuzz
Created February 12, 2016 16:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staybuzz/ca935bc22350019d9911 to your computer and use it in GitHub Desktop.
Save staybuzz/ca935bc22350019d9911 to your computer and use it in GitHub Desktop.
まあやおねえさんをお迎えするスクリプト
#coding: utf-8
#!/usr/bin/env python2
import re
import requests
import lxml.html
import urllib
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
""" スマホ用UserAgentにすることでプレイリストが取得しやすくなる """
ua = "Mozilla/5.0 (Linux; Android 4.4.2; LGL22 Build/KOT49I.LGL2220f) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.95 Mobile Safari/537.36"
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ua
driver = webdriver.PhantomJS(desired_capabilities=dcap)
for i in range(216):
num = "{0:03d}".format(i+1)
target_url = 'http://www.tv-tokyo.co.jp/pirameki/gallery/maya/'+num+'.html'
driver.get(target_url)
src = driver.page_source
root = lxml.html.fromstring(src)
""" 動画が見つからない場合は次の動画へ移る """
try:
pre_m3u_url = root.cssselect('.g480 a')[0].values()[0]
except IndexError:
continue
print pre_m3u_url
pre_m3u = urllib.urlopen(pre_m3u_url).read() // ビットレートごとに読み込むプレイリストが書いてある
m3u_url = pre_m3u.split("\n")[2] // 一番いいビットレートのプレイリストを選択
print m3u_url
m3u = urllib.urlopen(m3u_url).read() // 一番いいビットレートのプレイリストを取得
print m3u
url_header = "/".join(m3u_url.split("/")[:-1])+"/" // 動画ファイルは相対パスで書かれているので、その起点を変数に入れる
print url_header
ts_urls = re.compile(r".*ts").findall(m3u) // 動画ファイルは分割されている。すべてのリンクをリストに格納
print ts_urls
print url_header+ts_urls[0]
""" 保存名は「ループ回数」-「連番」。ex) '001-2.ts', '121-3.ts' """
for j in range(len(ts_urls)):
fname = ts_urls[j].split("/")[1]
urllib.urlretrieve(url_header+ts_urls[j], "{0:03d}".format(i+1)+"-"+str(j+1)+".ts")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment