Created
May 18, 2010 13:42
-
-
Save mizchi/405010 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/python | |
#-*- encoding:utf-8 -*- | |
#setting | |
username = "" | |
password = "" | |
import cgi | |
import cookielib | |
import urllib | |
import urllib2 | |
import re | |
import sys | |
from time import sleep | |
def __login(username, password): | |
cj = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
req = urllib2.Request("https://secure.nicovideo.jp/secure/login?site=niconico"); | |
account = {"mail": username, "password": password} | |
req.add_data(urllib.urlencode(account.items())) | |
opener.open(req) | |
return opener | |
def get_comments(sm): | |
opener = __login(username,password) | |
res = opener.open("http://www.nicovideo.jp/api/getflv.php?v=%s" % sm) | |
doc = res.read() | |
#POSTするXMLを作る | |
thread_id = re.findall("(?<=thread_id=)[0-9]+",doc)[0] | |
send_xml = '<thread res_from="-300" version="20061206" thread="%s" />'% thread_id | |
#コメントダウンロード用のurl (エスケープされている) | |
_tmp =re.findall("((?<=ms=)http.*?api)",doc) | |
ms_num = re.findall(r"(?<=2F)[0-9]+",_tmp[0]) | |
url="http://msg.nicovideo.jp/%s/api/" % ms_num[0] | |
res = opener.open(url,data=send_xml ) | |
doc =res.read() | |
comments = [] | |
for i in re.findall("<chat.*?>.*?</chat>",doc): | |
raw_comment = re.sub("<.*?>","",i) | |
comments.append(raw_comment) | |
return comments | |
# comments = get_comments("sm1966768") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment