Skip to content

Commit 234b8e3

Browse files
committed
Add `!pt live' to show the first found livestream
1 parent 337e223 commit 234b8e3

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

modules/pt.py

+34-11
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,40 @@ def search(self, search_string, count=0):
2020
data = json.loads(response.read().decode("utf-8"))
2121
return data
2222

23+
def videos(self, params):
24+
params = urlencode(params, quote_via=quote_plus)
25+
query_url = self.instance_url + 'api/v1/videos?' + params
26+
response = urllib.request.urlopen(query_url)
27+
data = json.loads(response.read().decode("utf-8"))
28+
return data
29+
30+
def getlive(self, count=1):
31+
return self.videos({'isLive': 1, 'count': count})
32+
2333
class MatrixModule(BotModule):
2434
def __init__(self, name):
2535
super().__init__(name)
2636
self.instance_url = 'https://sepiasearch.org/'
37+
self.client = PeerTubeClient()
2738

2839
def matrix_start(self, bot):
2940
super().matrix_start(bot)
3041
self.add_module_aliases(bot, ['ptall'])
3142

43+
def format_video(self, video):
44+
video_url = video.get("url") or self.instance_url + 'videos/watch/' + video["uuid"]
45+
duration = time.strftime('%H:%M:%S', time.gmtime(video["duration"]))
46+
instancedata = video["account"]["host"]
47+
html = f'<a href="{video_url}">{video["name"]}</a> {video["description"] or ""} [{duration}] @ {instancedata}'
48+
text = f'{video_url} : {video["name"]} {video.get("description") or ""} [{duration}]'
49+
return (html, text)
50+
3251
async def matrix_message(self, bot, room, event):
3352
args = event.body.split()
3453
if len(args) == 3:
3554
if args[1] == "setinstance":
3655
bot.must_be_owner(event)
37-
self.instance_url = args[2]
56+
self.client.instance_url = args[2]
3857
bot.save_settings()
3958
await bot.send_text(room, event, 'Instance url set to ' + self.instance_url, bot_ignore=True)
4059
return
@@ -43,22 +62,26 @@ async def matrix_message(self, bot, room, event):
4362
if args[1] == "showinstance":
4463
await bot.send_text(room, event, 'Using instance at ' + self.instance_url, bot_ignore=True)
4564
return
65+
if args[1] == "live":
66+
data = self.client.getlive()
67+
if len(data['data']):
68+
for video in data['data']:
69+
html, text = self.format_video(video)
70+
await bot.send_html(room, event, html, text, bot_ignore=True)
71+
else:
72+
await bot.send_text(room, event, 'Sorry, no livestreams found.', bot_ignore=True)
73+
return
74+
4675

4776
if len(args) > 1:
4877
query = event.body[len(args[0])+1:]
49-
p = PeerTubeClient()
50-
p.instance_url = self.instance_url
5178
count = 1
5279
if args[0] == '!ptall':
5380
count = 0
54-
data = p.search(query, count)
81+
data = self.client.search(query, count)
5582
if len(data['data']) > 0:
5683
for video in data['data']:
57-
video_url = video.get("url") or self.instance_url + 'videos/watch/' + video["uuid"]
58-
duration = time.strftime('%H:%M:%S', time.gmtime(video["duration"]))
59-
instancedata = video["account"]["host"]
60-
html = f'<a href="{video_url}">{video["name"]}</a> {video["description"] or ""} [{duration}] @ {instancedata}'
61-
text = f'{video_url} : {video["name"]} {video.get("description") or ""} [{duration}]'
84+
html, text = self.format_video(video)
6285
await bot.send_html(room, event, html, text, bot_ignore=True)
6386
else:
6487
await bot.send_text(room, event, 'Sorry, no videos found found.', bot_ignore=True)
@@ -68,13 +91,13 @@ async def matrix_message(self, bot, room, event):
6891

6992
def get_settings(self):
7093
data = super().get_settings()
71-
data['instance_url'] = self.instance_url
94+
data['instance_url'] = self.client.instance_url
7295
return data
7396

7497
def set_settings(self, data):
7598
super().set_settings(data)
7699
if data.get("instance_url"):
77-
self.instance_url = data["instance_url"]
100+
self.client.instance_url = data["instance_url"]
78101

79102
def help(self):
80103
return ('PeerTube search')

0 commit comments

Comments
 (0)