Skip to content

Instantly share code, notes, and snippets.

@rinsuki
Created December 20, 2018 22:03
Show Gist options
  • Save rinsuki/99588711a42c7626448b0ce02163d103 to your computer and use it in GitHub Desktop.
Save rinsuki/99588711a42c7626448b0ce02163d103 to your computer and use it in GitHub Desktop.
iOS版ニコニコアプリのコメントレンダラをHTML5版の独自拡張に無理矢理対応したように見せかけるやつ
from mitmproxy import http
import xml.etree.ElementTree as ET
import re
# iOS版ニコニコはどうやら250件以上の投稿者コメントは処理しないよう
# def request(flow: http.HTTPFlow):
# if flow.request.host != "nmsg.nicovideo.jp":
# return
# print(flow.request.headers.get("content-type", ""))
# if "xml" not in flow.request.headers.get("content-type", ""):
# return
# tree = ET.fromstring(flow.request.content.decode("utf-8"))
# if tree.tag != "packet":
# return
# for child in tree:
# if child.tag == "thread":
# if child.attrib.get("fork") == "1":
# child.set("res_from", "-1000")
# flow.request.content = ET.tostring(tree, encoding="utf-8", method="xml")
def response(flow: http.HTTPFlow):
if flow.request.host != "nmsg.nicovideo.jp":
return
print(flow.request.headers.get("content-type", ""))
if "xml" not in flow.request.headers.get("content-type", ""):
return
tree = ET.fromstring(flow.response.content)
if tree.tag != "packet":
return
replacedir = []
for child in tree:
if child.tag != "chat":
continue
if child.text.startswith("@"):
# コマンドモード
if child.text.startswith("@置換"):
r = re.match("^@置換 \"(.+?)\" \"(.+)\"", child.text)
print(r.group(1), r.group(2))
replacedir.append([r.group(1), r.group(2).replace("\\n", "\n")])
else:
for r in replacedir:
print(r[0], r[1])
child.text = child.text.replace(r[0], r[1])
child.set("mail", re.sub('\.[0-9]+', '', re.sub('@0+\.[0-9]+', '@1', re.sub('^[0-9]+ ', '', child.attrib.get("mail", "")))))
flow.response.content = ET.tostring(tree, encoding="utf-8", method="xml")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment