-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
194 lines (179 loc) · 6.99 KB
/
client.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
""" nicovideo2discord client program """
from __future__ import annotations
import datetime
import json
import time
import urllib.request
import pypresence
import xmltodict
genres = { #Genres list
"entertainment": "エンターテインメント",
"radio": "ラジオ",
"music_sound": "音楽・サウンド",
"dance": "ダンス",
"animal": "動物",
"nature": "自然",
"cooking": "料理",
"traveling_outdoor": "旅行・アウトドア",
"vehicle": "乗り物",
"sports": "スポーツ",
"society_politics_news": "社会・政治・時事",
"technology_craft": "技術・工作",
"解説・講座": "commentary_lecture",
"anime": "アニメ",
"game": "ゲーム",
"other": "その他"
}
def debug(message: str, content_type: str = "text/plain") -> urllib.response:
""" Send debug message to server """
headers = {"Content-Type": content_type}
req = urllib.request.Request("http://localhost:5000/debug", message.encode(), headers)
return urllib.request.urlopen(req)
with open("config.json", encoding="UTF-8") as configfile:
config = json.load(configfile)
CLIENT_ID = config["client_id"]
RPC = pypresence.Presence(CLIENT_ID)
RPC.connect()
beforevideodata = {'status': 'closed'}
while True:
videodata = json.loads(urllib.request.urlopen("http://localhost:5000/video").read().decode())
if videodata != beforevideodata:
debug("Data changed.")
if videodata["status"] == "closed": # Page closed
RPC.clear()
beforevideodata = videodata
continue
elif videodata["status"] == "toppage": # Top page
RPC.update(
state="トップページ",
large_image="niconico_tvchan",
large_text="ニコニコテレビちゃん",
buttons=[
{
"label": "ニコニコ動画トップページ",
"url": "https://www.nicovideo.jp/video_top"
}
]
)
beforevideodata = videodata
continue
elif videodata["status"] == "ranking": # Ranking Page
RPC.update(
state="ランキングを閲覧中",
large_image="niconico_tvchan",
large_text="ニコニコテレビちゃん",
buttons=[
{
"label": "ニコニコ動画トップページ",
"url": "https://www.nicovideo.jp/video_top"
},
{
"label": "ニコニコ動画総合ランキング",
"url": "https://www.nicovideo.jp/ranking"
}
]
)
beforevideodata = videodata
continue
elif videodata["status"] == "genretoppages": # Genre-separated top pages
RPC.update(
state=f'ジャンル: {genres[videodata["genre"]]}',
details="ジャンル別トップページを閲覧中",
large_image="niconico_tvchan",
large_text="ニコニコテレビちゃん",
buttons=[
{
"label": "ニコニコ動画トップページ",
"url": "https://www.nicovideo.jp/video_top"
},
{
"label": "該当ジャンルトップページ",
"url": f'https://www.nicovideo.jp/video_top/genre/{videodata["genre"]}'
}
]
)
beforevideodata = videodata
continue
# Watching video
video = xmltodict.parse(
urllib.request.urlopen(
f'http://localhost:5000/videoinfo?vid={videodata["id"]}'
).read().decode()
)
# Video metadata
video = video["nicovideo_thumb_response"]["thumb"]
title = video["title"]
vid = video["video_id"]
thumbnail_url = video["thumbnail_url"]
url = video["watch_url"]
videolength = video["length"].split(":")
# Video length
if len(videolength) == 1:
videolength = datetime.timedelta(
seconds=int(videolength[0])
)
elif len(videolength) == 2:
videolength = datetime.timedelta(
minutes=int(videolength[0]),
seconds=int(videolength[1])
)
elif len(videolength) == 3:
videolength = datetime.timedelta(
hours=int(videolength[0]),
minutes=int(videolength[1]),
seconds=int(videolength[2])
)
# Video ended
if videodata["ended"] is False:
playingtime = datetime.timedelta(
hours=int(videodata["hour"]),
minutes=int(videodata["min"]),
seconds=int(videodata["sec"])
)
startedtime = datetime.datetime.now().replace(microsecond=0) - playingtime
# Get author name
if "user_nickname" in video:
author = video["user_nickname"]
else:
author = video["ch_name"]
# Set state message
detailmsg = f'{video["title"]}'
if videodata["playing"] is False and videodata["ended"] is False:
detailmsg = f'{detailmsg} (一時停止中)'
elif videodata["ended"] is True:
detailmsg = f'{detailmsg} (再生終了)'
if "speed" in videodata and videodata["speed"] != 1:
detailmsg = f'{detailmsg} ({videodata["speed"]}倍速)'
# Set details message
statemsg = f'投稿者: {author}, 動画長: {video["length"]}'
# RICKROLL
if video["video_id"] == "sm5904539":
detailmsg="リックロールされています"
statemsg="釣られた"
# Post those informations into discord
if videodata["playing"] is True:
RPC.update(
details=detailmsg,
state=statemsg,
large_image="niconico_tvchan",
large_text="ニコニコテレビちゃん",
start=startedtime.timestamp(),
buttons=[
{"label": f'動画を視聴する [{videodata["id"]}]', "url": url},
{"label": "ニコニコ動画トップページ", "url": "https://www.nicovideo.jp/video_top/"}
],
instance=True
)
else:
RPC.update(
details=detailmsg,
state=statemsg,
large_image="niconico_tvchan",
large_text="ニコニコテレビちゃん",
buttons=[
{"label": f'動画を視聴する [{videodata["id"]}]', "url": url},
{"label": "ニコニコ動画トップページ", "url": "https://www.nicovideo.jp/video_top/"}
]
)
beforevideodata = videodata
time.sleep(1)