Skip to content

Instantly share code, notes, and snippets.

@taonico
Created July 29, 2012 12:38
Show Gist options
  • Select an option

  • Save taonico/3198443 to your computer and use it in GitHub Desktop.

Select an option

Save taonico/3198443 to your computer and use it in GitHub Desktop.
NicoRequest
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import urllib, urllib2
import cookielib
from xml.dom import minidom
class NicoRequest:
def __init__(self, mail, password):
self.mail = mail
self.password = password
self.loginCookie = None
self.postHeaders = {'Content-Type' : 'application/x-www-form-urlencoded'}
self.nicoHost = "secure.nicovideo.jp"
self.nicoPath = "/secure/login"
self.login = "?site=nicolive"
self.alertLogin = "?site=nicolive_antenna"
self.apiHost = "live.nicovideo.jp";
self.getalertstatus = "/api/getalertstatus"
self.getplayerstatus = "/api/getplayerstatus?v="
def loginLive(self):
url = 'https://' + self.nicoHost + self.nicoPath + self.login
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
query = urllib.urlencode({'mail':self.mail, 'password':self.password})
response = opener.open(url, query).read()
return opener
def loginAlert(self):
url = 'https://' + self.nicoHost + self.nicoPath + self.alertLogin
query = urllib.urlencode({'mail':self.mail, 'password':self.password})
response = urllib2.urlopen(urllib2.Request(url, query, self.postHeaders)).read()
ticket = self.getNodeValue(response, 'ticket')
url = 'http://' + self.apiHost + self.getalertstatus
query = urllib.urlencode({'ticket' : ticket })
response = urllib2.urlopen(urllib2.Request(url, query, self.postHeaders)).read()
return response
def getPlayerStatus(self, lvid, user_session):
if user_session == None:
opener = self.loginLive()
else:
self.loginCookie = user_session
cookie = self.getCookie2()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
url = 'http://' + self.apiHost + self.getplayerstatus + lvid
request = urllib2.Request(url)
# print request.get_full_url()
response = opener.open(request).read()
return response
def getNodeValue(self, document, name):
return minidom.parseString(document).getElementsByTagName(name)[0].childNodes[0].data
def getCookie(self):
cookieJar = cookielib.CookieJar()
cookie = cookielib.Cookie(version=0, name='user_session',
value=self.loginCookie,
port=None, port_specified=False, domain='.nicovideo.jp',
domain_specified=False, domain_initial_dot=False, path='/', path_specified=True,
secure=False, expires=None, discard=True,
comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
cookieJar.set_cookie(cookie)
return cookieJar
def getCookie2(self):
cookieJar = cookielib.CookieJar()
cookie = CookieTest('user_session', self.loginCookie, '.nicovideo.jp', '/')
cookieJar.set_cookie(cookie)
return cookieJar
class CookieTest:
def __init__(self, name, value, domain, path):
self.version=0
self.name=name
self.value=value
self.port=None
self.port_specified=False
self.domain=domain
self.domain_specified=False
self.domain_initial_dot=False
self.path=path
self.path_specified=True
self.secure=False
self.expires=None
self.discard=True
self.comment=None
self.comment_url=None
self.rfc2109=False
def has_nonstandard_attr(name):
if self.name == name:
return True
return False
def get_nonstandard_attr(name, default):
if self.name == name:
return self.value
return None
def set_nonstandard_attr(name, value):
self.name = name
self.value = value
def is_expired(self, now):
return False
class PlayerStatus:
def __init__(self, response):
self.addr = self.getNodeValue(response, "addr")
self.port = self.getNodeValue(response, "port")
self.thread = self.getNodeValue(response, "thread")
self.rtmp_url = self.getNodeValue(response, "url")
self.stream_id = self.getNodeValue(response, "id")
self.ticket = self.getNodeValue(response, "ticket")
self.contents = self.getNodeValue(response, "contents")
def getNodeValue(self, document, name):
childNodes = minidom.parseString(document).getElementsByTagName(name)[0].childNodes;
if childNodes.length == 0:
return "";
else:
return childNodes[0].data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment