mirror of
https://github.com/soimort/you-get.git
synced 2025-02-03 16:53:56 +03:00
icouses: Code clean up
This commit is contained in:
parent
4bbafeb9e4
commit
5351121186
@ -13,8 +13,9 @@ __all__ = ['icourses_download']
|
||||
|
||||
|
||||
def icourses_download(url, info_only, merge=False, output_dir='.', **kwargs):
|
||||
title, real_url = icourses_cn_url_parser(
|
||||
url, info_only=info_only, **kwargs)
|
||||
icourses_parser = ICousesExactor(url=url)
|
||||
real_url = icourses_parser.icourses_cn_url_parser(**kwargs)
|
||||
title = icourses_parser.title
|
||||
if real_url is not None:
|
||||
for tries in range(0, 3):
|
||||
try:
|
||||
@ -22,14 +23,23 @@ def icourses_download(url, info_only, merge=False, output_dir='.', **kwargs):
|
||||
break
|
||||
except error.HTTPError:
|
||||
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)
|
||||
if not info_only:
|
||||
download_urls([real_url], title, 'flv',
|
||||
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
|
||||
from time import sleep
|
||||
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_args[0], video_args[1], fs_status or '1')
|
||||
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(url, **kwargs):
|
||||
def icourses_cn_url_parser(self, **kwargs):
|
||||
PLAYER_BASE_VER = '150606-1'
|
||||
ENCRYPT_MOD_VER = '151020'
|
||||
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):
|
||||
logging.warning('Switching to playlist mode!')
|
||||
return icourses_playlist_download(url, **kwargs)
|
||||
return self.icourses_playlist_download(**kwargs)
|
||||
flashvars_patt = r'var\ flashvars\=((.|\n)*)};'
|
||||
server_time_patt = r'MPlayer.swf\?v\=(\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})
|
||||
final_url = '{}?{}'.format(
|
||||
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
|
||||
# time offset (?) value from the server
|
||||
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_callback[1], "%b %d %H:%M:%S %Y").timestamp() + int(ssl_callback[0]))
|
||||
sign_this = ENCRYPT_SALT + \
|
||||
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...
|
||||
arg_h = arg_h.decode('utf-8').strip('=').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})
|
||||
final_url = '{}?{}'.format(
|
||||
media_url, parse.urlencode(url_args))
|
||||
logging.debug('Concat`ed URL: {}'.format(final_url))
|
||||
return title, final_url
|
||||
logging.debug('Crafted URL: {}'.format(final_url))
|
||||
self.title = title
|
||||
return final_url
|
||||
|
||||
|
||||
site_info = 'icourses.cn'
|
||||
download = icourses_download
|
||||
download_playlist = icourses_playlist_download
|
||||
# download_playlist = icourses_playlist_download
|
||||
|
Loading…
Reference in New Issue
Block a user