Doge log

Abby CTO 雑賀 力王のオフィシャルサイトです

TwistedでPornoTubeのビデオをダウンロードする

YouTubeの次はもちPornoTube。
ここでしょ。

from twisted.internet import reactor
from twisted.web import client
import re

CONST_INDEX = 'http://pornotube.com/index.php?'
CONST_PLAYER = 'http://www.pornotube.com/player/player.php?%s'
CONST_VIDEO = 'http://%s.pornotube.com/%s/%s.flv'

const_video_params_re = re.compile(r'v\.swf\?v=([^"]+)"', re.M)
const_mediaId_re = re.compile(r'mediaId=([\d]+)&', re.M)
const_userId_re = re.compile(r'userId=([\d]+)&', re.M)
const_domain_re = re.compile(r'mediaDomain=([\w]+)&', re.M)

dummy_cookie = {
        'pornotube_last_update':'1183979234',
        'pornotube_splash_page':'MDEvMS8xOTUw'
        }

def downloadFlv(url, file):
    
    def getId(data):
        match = const_video_params_re.search(data)
        param = match.group(1)
        player_php = CONST_PLAYER % param
        client.getPage(player_php, cookies=dummy_cookie).addCallback(parse)
    
    def parse(data):
        def finish(data):
            reactor.stop()

        match1 = const_mediaId_re.search(data)
        mediaId = match1.group(1)
        match2 = const_userId_re.search(data)
        userId = match2.group(1)
        match3 = const_domain_re.search(data)
        domain = match3.group(1)
        download_url = CONST_VIDEO % (domain, userId, mediaId)
        client.downloadPage(download_url, file).addCallback(finish)

    def apply(data):
        client.getPage(url, cookies=dummy_cookie).addCallback(getId)
    
    post = 'bMonth=01&bDay=1&bYear=1950&verifyAge=true&submit=CONTINUE+%C2%BB'
    client.getPage(CONST_INDEX, method='POST', cookies=dummy_cookie, postdata=post).addCallback(apply)

if __name__ == '__main__':
    downloadFlv('http://pornotube.com/media.php?m=xxxxxxx', '/tmp/tmp.flv')
    reactor.run()

エロいアプリぐらい書けないといけないらしいからね。
ご利用は計画的に。
うくく。