icouses: Code clean up

This commit is contained in:
liushuyu 2016-10-25 12:52:30 -06:00
parent 4bbafeb9e4
commit 5351121186
No known key found for this signature in database
GPG Key ID: 23D1CE4534419437

View File

@ -13,8 +13,9 @@ __all__ = ['icourses_download']
def icourses_download(url, info_only, merge=False, output_dir='.', **kwargs): def icourses_download(url, info_only, merge=False, output_dir='.', **kwargs):
title, real_url = icourses_cn_url_parser( icourses_parser = ICousesExactor(url=url)
url, info_only=info_only, **kwargs) real_url = icourses_parser.icourses_cn_url_parser(**kwargs)
title = icourses_parser.title
if real_url is not None: if real_url is not None:
for tries in range(0, 3): for tries in range(0, 3):
try: try:
@ -22,14 +23,23 @@ def icourses_download(url, info_only, merge=False, output_dir='.', **kwargs):
break break
except error.HTTPError: except error.HTTPError:
logging.warning('Failed to fetch the video file! Retrying...') logging.warning('Failed to fetch the video file! Retrying...')
title, real_url = icourses_cn_url_parser(url) real_url = icourses_parser.icourses_cn_url_parser()
title = icourses_parser.title
print_info(site_info, title, type_, size) print_info(site_info, title, type_, size)
if not info_only: if not info_only:
download_urls([real_url], title, 'flv', download_urls([real_url], title, 'flv',
total_size=size, output_dir=output_dir, refer=url, merge=merge, faker=True) total_size=size, output_dir=output_dir, refer=url, merge=merge, faker=True)
def icourses_playlist_download(url, **kwargs): # Why not using VideoExtractor: This site needs specical download method
class ICousesExactor(object):
def __init__(self, url):
self.url = url
self.title = ''
return
def icourses_playlist_download(self, **kwargs):
import random import random
from time import sleep from time import sleep
html = get_content(url) html = get_content(url)
@ -47,17 +57,16 @@ def icourses_playlist_download(url, **kwargs):
video_url = 'http://www.icourses.cn/jpk/changeforVideo.action?resId={}&courseId={}&firstShowFlag={}'.format( video_url = 'http://www.icourses.cn/jpk/changeforVideo.action?resId={}&courseId={}&firstShowFlag={}'.format(
video_args[0], video_args[1], fs_status or '1') video_args[0], video_args[1], fs_status or '1')
sleep(random.Random().randint(0, 5)) # Prevent from blockage sleep(random.Random().randint(0, 5)) # Prevent from blockage
icourses_download(url=video_url, **kwargs) icourses_download(video_url, **kwargs)
def icourses_cn_url_parser(self, **kwargs):
def icourses_cn_url_parser(url, **kwargs):
PLAYER_BASE_VER = '150606-1' PLAYER_BASE_VER = '150606-1'
ENCRYPT_MOD_VER = '151020' ENCRYPT_MOD_VER = '151020'
ENCRYPT_SALT = '3DAPmXsZ4o' # It took really long time to find this... ENCRYPT_SALT = '3DAPmXsZ4o' # It took really long time to find this...
html = get_content(url) html = get_content(self.url)
if re.search(pattern=r'showSectionNode\(.*\)', string=html): if re.search(pattern=r'showSectionNode\(.*\)', string=html):
logging.warning('Switching to playlist mode!') logging.warning('Switching to playlist mode!')
return icourses_playlist_download(url, **kwargs) return self.icourses_playlist_download(**kwargs)
flashvars_patt = r'var\ flashvars\=((.|\n)*)};' flashvars_patt = r'var\ flashvars\=((.|\n)*)};'
server_time_patt = r'MPlayer.swf\?v\=(\d+)' server_time_patt = r'MPlayer.swf\?v\=(\d+)'
uuid_patt = r'uuid:(\d+)' uuid_patt = r'uuid:(\d+)'
@ -102,16 +111,19 @@ def icourses_cn_url_parser(url, **kwargs):
url_args.update({'h': arg_h, 'r': arg_r}) url_args.update({'h': arg_h, 'r': arg_r})
final_url = '{}?{}'.format( final_url = '{}?{}'.format(
media_url, parse.urlencode(url_args)) media_url, parse.urlencode(url_args))
return title, final_url self.title = title
return final_url
# when the `SSLMode` is activated, we need to receive the timestamp and the # when the `SSLMode` is activated, we need to receive the timestamp and the
# time offset (?) value from the server # time offset (?) value from the server
logging.debug('The encryption mode is in effect') logging.debug('The encryption mode is in effect')
ssl_callback = get_html('{}/ssl/ssl.shtml'.format(media_host)).split(',') ssl_callback = get_html(
'{}/ssl/ssl.shtml'.format(media_host)).split(',')
ssl_timestamp = int(datetime.datetime.strptime( ssl_timestamp = int(datetime.datetime.strptime(
ssl_callback[1], "%b %d %H:%M:%S %Y").timestamp() + int(ssl_callback[0])) ssl_callback[1], "%b %d %H:%M:%S %Y").timestamp() + int(ssl_callback[0]))
sign_this = ENCRYPT_SALT + \ sign_this = ENCRYPT_SALT + \
parse.urlparse(media_url).path + str(ssl_timestamp) parse.urlparse(media_url).path + str(ssl_timestamp)
arg_h = base64.b64encode(hashlib.md5(bytes(sign_this, 'utf-8')).digest()) arg_h = base64.b64encode(hashlib.md5(
bytes(sign_this, 'utf-8')).digest())
# Post-processing, may subject to change, so leaving this alone... # Post-processing, may subject to change, so leaving this alone...
arg_h = arg_h.decode('utf-8').strip('=').replace('+', arg_h = arg_h.decode('utf-8').strip('=').replace('+',
'-').replace('/', '_') '-').replace('/', '_')
@ -120,10 +132,11 @@ def icourses_cn_url_parser(url, **kwargs):
url_args.update({'h': arg_h, 'r': arg_r, 'p': ENCRYPT_MOD_VER}) url_args.update({'h': arg_h, 'r': arg_r, 'p': ENCRYPT_MOD_VER})
final_url = '{}?{}'.format( final_url = '{}?{}'.format(
media_url, parse.urlencode(url_args)) media_url, parse.urlencode(url_args))
logging.debug('Concat`ed URL: {}'.format(final_url)) logging.debug('Crafted URL: {}'.format(final_url))
return title, final_url self.title = title
return final_url
site_info = 'icourses.cn' site_info = 'icourses.cn'
download = icourses_download download = icourses_download
download_playlist = icourses_playlist_download # download_playlist = icourses_playlist_download