10
10
class PeerTubeClient :
11
11
def __init__ (self ):
12
12
self .instance_url = 'https://sepiasearch.org/'
13
+ self .client = PeerTubeClient ()
13
14
14
15
def search (self , search_string , count = 0 ):
15
16
if count == 0 :
@@ -19,6 +20,15 @@ def search(self, search_string, count=0):
19
20
response = urllib .request .urlopen (search_url )
20
21
data = json .loads (response .read ().decode ("utf-8" ))
21
22
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 })
22
32
23
33
class MatrixModule (BotModule ):
24
34
def __init__ (self , name ):
@@ -29,14 +39,31 @@ def matrix_start(self, bot):
29
39
super ().matrix_start (bot )
30
40
self .add_module_aliases (bot , ['ptall' ])
31
41
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
+
32
50
async def matrix_message (self , bot , room , event ):
33
51
args = event .body .split ()
34
52
if len (args ) == 3 :
35
53
if args [1 ] == "setinstance" :
36
54
bot .must_be_owner (event )
37
- self .instance_url = args [2 ]
55
+ self .client . instance_url = args [2 ]
38
56
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 )
40
67
return
41
68
42
69
if len (args ) == 2 :
@@ -68,13 +95,13 @@ async def matrix_message(self, bot, room, event):
68
95
69
96
def get_settings (self ):
70
97
data = super ().get_settings ()
71
- data ['instance_url' ] = self .instance_url
98
+ data ['instance_url' ] = self .client . instance_url
72
99
return data
73
100
74
101
def set_settings (self , data ):
75
102
super ().set_settings (data )
76
103
if data .get ("instance_url" ):
77
- self .instance_url = data ["instance_url" ]
104
+ self .client . instance_url = data ["instance_url" ]
78
105
79
106
def help (self ):
80
107
return ('PeerTube search' )
0 commit comments