support multi video in one page

add matchall api

matchall: almost same as match1, but it will return a list
of all matches.
This commit is contained in:
Zhang Ning 2015-09-03 15:46:29 +08:00
parent b50cc2338f
commit c73a636d43
2 changed files with 33 additions and 8 deletions

View File

@ -79,6 +79,24 @@ def match1(text, *patterns):
ret.append(match.group(1)) ret.append(match.group(1))
return ret return ret
def matchall(text, patterns):
"""Scans through a string for substrings matched some patterns.
Args:
text: A string to be scanned.
patterns: a list of regex pattern.
Returns:
a list if matched. empty if not.
"""
ret = []
for pattern in patterns:
match = re.findall(pattern, text)
ret += match
return ret
def launch_player(player, urls): def launch_player(player, urls):
import subprocess import subprocess
import shlex import shlex

View File

@ -7,35 +7,42 @@ from .qq import qq_download_by_vid
from .sina import sina_download_by_vid from .sina import sina_download_by_vid
from .tudou import tudou_download_by_id from .tudou import tudou_download_by_id
from .youku import youku_download_by_vid from .youku import youku_download_by_vid
from .youku import Youku
""" """
refer to http://open.youku.com/tools refer to http://open.youku.com/tools
""" """
youku_api_pattern = 'YKU.Player\(\'[a-zA-Z0-9]+\',{ client_id: \'[a-zA-Z0-9]+\', vid: \'([a-zA-Z0-9]+)\'' youku_embed_patterns = [ 'youku\.com/v_show/id_([a-zA-Z0-9=]+)',
'player\.youku\.com/player\.php/sid/([a-zA-Z0-9=]+)/v\.swf',
'loader\.swf\?VideoIDS=([a-zA-Z0-9=]+)',
'player\.youku\.com/embed/([a-zA-Z0-9=]+)',
'YKU.Player\(\'[a-zA-Z0-9]+\',{ client_id: \'[a-zA-Z0-9]+\', vid: \'([a-zA-Z0-9]+)\''
]
""" """
http://www.tudou.com/programs/view/html5embed.action?type=0&code=3LS_URGvl54&lcode=&resourceId=0_06_05_99 http://www.tudou.com/programs/view/html5embed.action?type=0&code=3LS_URGvl54&lcode=&resourceId=0_06_05_99
""" """
tudou_embed_pattern = 'tudou\.com[a-zA-Z0-9\/\?=\&\.\;]+code=([[a-zA-Z0-9_]+)\&' tudou_embed_patterns = [ 'tudou\.com[a-zA-Z0-9\/\?=\&\.\;]+code=([[a-zA-Z0-9_]+)\&'
]
""" """
refer to http://open.tudou.com/wiki/video/info refer to http://open.tudou.com/wiki/video/info
""" """
tudou_api_patterns = [ ]
def embed_download(url, output_dir = '.', merge = True, info_only = False ,**kwargs): def embed_download(url, output_dir = '.', merge = True, info_only = False ,**kwargs):
content = get_content(url) content = get_content(url)
found = False found = False
title = match1(content, '<title>([^<>]+)</title>') title = match1(content, '<title>([^<>]+)</title>')
vid = Youku.get_vid_from_url(content) or \ vids = matchall(content, youku_embed_patterns)
match1(content, youku_api_pattern) for vid in vids:
if vid is not None:
found = True found = True
youku_download_by_vid(vid, title=title, output_dir=output_dir, merge=merge, info_only=info_only) youku_download_by_vid(vid, title=title, output_dir=output_dir, merge=merge, info_only=info_only)
vid = match1(content, tudou_embed_pattern) vids = matchall(content, tudou_embed_patterns)
if vid is not None: for vid in vids:
found = True found = True
tudou_download_by_id(vid, title=title, output_dir=output_dir, merge=merge, info_only=info_only) tudou_download_by_id(vid, title=title, output_dir=output_dir, merge=merge, info_only=info_only)
if not found: if not found:
raise NotImplementedError(url) raise NotImplementedError(url)