@@ -20,21 +20,40 @@ def search(self, search_string, count=0):
20
20
data = json .loads (response .read ().decode ("utf-8" ))
21
21
return data
22
22
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
+
23
33
class MatrixModule (BotModule ):
24
34
def __init__ (self , name ):
25
35
super ().__init__ (name )
26
36
self .instance_url = 'https://sepiasearch.org/'
37
+ self .client = PeerTubeClient ()
27
38
28
39
def matrix_start (self , bot ):
29
40
super ().matrix_start (bot )
30
41
self .add_module_aliases (bot , ['ptall' ])
31
42
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
+
32
51
async def matrix_message (self , bot , room , event ):
33
52
args = event .body .split ()
34
53
if len (args ) == 3 :
35
54
if args [1 ] == "setinstance" :
36
55
bot .must_be_owner (event )
37
- self .instance_url = args [2 ]
56
+ self .client . instance_url = args [2 ]
38
57
bot .save_settings ()
39
58
await bot .send_text (room , event , 'Instance url set to ' + self .instance_url , bot_ignore = True )
40
59
return
@@ -43,22 +62,26 @@ async def matrix_message(self, bot, room, event):
43
62
if args [1 ] == "showinstance" :
44
63
await bot .send_text (room , event , 'Using instance at ' + self .instance_url , bot_ignore = True )
45
64
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
+
46
75
47
76
if len (args ) > 1 :
48
77
query = event .body [len (args [0 ])+ 1 :]
49
- p = PeerTubeClient ()
50
- p .instance_url = self .instance_url
51
78
count = 1
52
79
if args [0 ] == '!ptall' :
53
80
count = 0
54
- data = p .search (query , count )
81
+ data = self . client .search (query , count )
55
82
if len (data ['data' ]) > 0 :
56
83
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 )
62
85
await bot .send_html (room , event , html , text , bot_ignore = True )
63
86
else :
64
87
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):
68
91
69
92
def get_settings (self ):
70
93
data = super ().get_settings ()
71
- data ['instance_url' ] = self .instance_url
94
+ data ['instance_url' ] = self .client . instance_url
72
95
return data
73
96
74
97
def set_settings (self , data ):
75
98
super ().set_settings (data )
76
99
if data .get ("instance_url" ):
77
- self .instance_url = data ["instance_url" ]
100
+ self .client . instance_url = data ["instance_url" ]
78
101
79
102
def help (self ):
80
103
return ('PeerTube search' )
0 commit comments