Skip to content

Commit 57c3291

Browse files
committed
!pt live support
1 parent 068ff95 commit 57c3291

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

modules/pt.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class PeerTubeClient:
1111
def __init__(self):
1212
self.instance_url = 'https://sepiasearch.org/'
13+
self.client = PeerTubeClient()
1314

1415
def search(self, search_string, count=0):
1516
if count == 0:
@@ -19,6 +20,15 @@ def search(self, search_string, count=0):
1920
response = urllib.request.urlopen(search_url)
2021
data = json.loads(response.read().decode("utf-8"))
2122
return data
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})
2232

2333
class MatrixModule(BotModule):
2434
def __init__(self, name):
@@ -29,14 +39,31 @@ def matrix_start(self, bot):
2939
super().matrix_start(bot)
3040
self.add_module_aliases(bot, ['ptall'])
3141

42+
def format_video(self, video):
43+
video_url = video.get("url") or self.instance_url + 'videos/watch/' + video["uuid"]
44+
duration = time.strftime('%H:%M:%S', time.gmtime(video["duration"]))
45+
instancedata = video["account"]["host"]
46+
html = f'<a href="{video_url}">{video["name"]}</a> {video["description"] or ""} [{duration}] @ {instancedata}'
47+
text = f'{video_url} : {video["name"]} {video.get("description") or ""} [{duration}]'
48+
return (html, text)
49+
3250
async def matrix_message(self, bot, room, event):
3351
args = event.body.split()
3452
if len(args) == 3:
3553
if args[1] == "setinstance":
3654
bot.must_be_owner(event)
37-
self.instance_url = args[2]
55+
self.client.instance_url = args[2]
3856
bot.save_settings()
39-
await bot.send_text(room, 'Instance url set to ' + self.instance_url, bot_ignore=True, event)
57+
await bot.send_text(room, 'Instance url set to ' + self.instance_url, bot_ignore=True, event=event)
58+
return
59+
if args[1] == "live":
60+
data = self.client.getlive()
61+
if len(data['data']):
62+
for video in data['data']:
63+
html, text = self.format_video(video)
64+
await bot.send_html(room, event, html, text, bot_ignore=True)
65+
else:
66+
await bot.send_text(room, 'Sorry, no livestreams found.', bot_ignore=True, event=event)
4067
return
4168

4269
if len(args) == 2:
@@ -68,13 +95,13 @@ async def matrix_message(self, bot, room, event):
6895

6996
def get_settings(self):
7097
data = super().get_settings()
71-
data['instance_url'] = self.instance_url
98+
data['instance_url'] = self.client.instance_url
7299
return data
73100

74101
def set_settings(self, data):
75102
super().set_settings(data)
76103
if data.get("instance_url"):
77-
self.instance_url = data["instance_url"]
104+
self.client.instance_url = data["instance_url"]
78105

79106
def help(self):
80107
return ('PeerTube search')

0 commit comments

Comments
 (0)