Skip to content

Commit

Permalink
Niconico: workaround for TLS hang bug, fix #296
Browse files Browse the repository at this point in the history
  • Loading branch information
soimort committed Feb 14, 2014
1 parent 13f785e commit 869128c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/you_get/extractor/nicovideo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

def nicovideo_login(user, password):
data = "current_form=login&mail=" + user +"&password=" + password + "&login_submit=Log+In"
response = request.urlopen(request.Request("https://secure.nicovideo.jp/secure/login?site=niconico", headers = fake_headers, data = data.encode('utf-8')))
response = request.urlopen(request.Request("https://secure.nicovideo.jp/secure/login?site=niconico", headers=fake_headers, data=data.encode('utf-8')))
return response.headers

def nicovideo_download(url, output_dir = '.', merge = True, info_only = False):
request.install_opener(request.build_opener(request.HTTPCookieProcessor()))

def nicovideo_download(url, output_dir='.', merge=True, info_only=False):
import ssl
ssl_context = request.HTTPSHandler(
context=ssl.SSLContext(ssl.PROTOCOL_TLSv1))
cookie_handler = request.HTTPCookieProcessor()
opener = request.build_opener(ssl_context, cookie_handler)
request.install_opener(opener)

import netrc, getpass
info = netrc.netrc().authenticators('nicovideo')
if info is None:
Expand All @@ -21,15 +26,15 @@ def nicovideo_download(url, output_dir = '.', merge = True, info_only = False):
user, password = info[0], info[2]
print("Logging in...")
nicovideo_login(user, password)

html = get_html(url) # necessary!
title = unicodize(r1(r'<span class="videoHeaderTitle">([^<]+)</span>', html))

api_html = get_html('http://www.nicovideo.jp/api/getflv?v=%s' % url.split('/')[-1])
real_url = parse.unquote(r1(r'url=([^&]+)&', api_html))

type, ext, size = url_info(real_url)

print_info(site_info, title, type, size)
if not info_only:
download_urls([real_url], title, ext, size, output_dir, merge = merge)
Expand Down

0 comments on commit 869128c

Please sign in to comment.