-
Notifications
You must be signed in to change notification settings - Fork 0
/
niconico.py
63 lines (49 loc) · 1.94 KB
/
niconico.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /usr/bin/env python
#coding: utf8
import ConfigParser
import re,urllib,urllib2,cookielib
import json
from xml.etree.ElementTree import *
class niconico:
userid = ""
passwd = ""
mylistId = ""
def __init__(self):
iniFile = ConfigParser.SafeConfigParser()
iniFile.read("config.ini")
self.userid=iniFile.get("NICONICO","USERID")
self.passwd=iniFile.get("NICONICO","PASSWD")
# self.mylistId = iniFile.get("NICONICO","MYLISTID")
def getToken(self):
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
urllib2.install_opener(opener)
urllib2.urlopen("https://secure.nicovideo.jp/secure/login",
urllib.urlencode({"mail":self.userid,"password":self.passwd}))
html = urllib2.urlopen("http://www.nicovideo.jp/my/mylist").read()
for line in html.splitlines():
mo = re.match(r'^\s*(?P<test>NicoAPI\.token) = "(?P<token>[\d\w-]+)";\s*', line)
if mo:
token = mo.group("token")
return token
def addMyList(self, videoId, tgtMyListId=None):
token = self.getToken()
if tgtMyListId != None:
myListId = tgtMyListId
cmdurl = "http://www.nicovideo.jp/api/mylist/add"
q = {}
q["group_id"] = myListId
q["item_type"] = 0
q["item_id"] = videoId
q["description"] = u""
q["token"] = token
cmdurl += "?" + urllib.urlencode(q)
return json.load(urllib2.urlopen(cmdurl), encoding='utf8')
@classmethod
def getVideoInfo(self, videoId):
tgtUrl = 'http://ext.nicovideo.jp/api/getthumbinfo/' + videoId
tgtXml = urllib2.urlopen(tgtUrl).read()
elem = fromstring(tgtXml)
ret = {}
for e in elem.getiterator():
ret[e.tag] = elem.findtext('.//' + e.tag)
return ret