3
3
4
4
import isodate
5
5
import requests
6
+ from requests import Response
6
7
7
8
from cloudbot import hook
8
9
from cloudbot .bot import bot
23
24
24
25
25
26
class APIError (Exception ):
26
- def __init__ (self , message : str , response : Optional [str ] = None ) -> None :
27
+ def __init__ (
28
+ self , message : str , response : Optional [Union [str , Response ]] = None
29
+ ) -> None :
27
30
super ().__init__ (message )
28
31
self .message = message
29
32
self .response = response
@@ -35,8 +38,8 @@ def __init__(self) -> None:
35
38
36
39
37
40
class NoResultsError (APIError ):
38
- def __init__ (self ) -> None :
39
- super ().__init__ ("No results" )
41
+ def __init__ (self , response : Response ) -> None :
42
+ super ().__init__ ("No results" , response )
40
43
41
44
42
45
def raise_api_errors (response : requests .Response ) -> None :
@@ -115,7 +118,7 @@ def get_video_description(video_id: str) -> str:
115
118
116
119
data = json ["items" ]
117
120
if not data :
118
- raise NoResultsError ()
121
+ raise NoResultsError (request )
119
122
120
123
item = data [0 ]
121
124
snippet = item ["snippet" ]
@@ -131,20 +134,21 @@ def get_video_description(video_id: str) -> str:
131
134
out += " - length \x02 {}\x02 " .format (
132
135
timeformat .format_time (int (length .total_seconds ()), simple = True )
133
136
)
134
- try :
135
- total_votes = float (statistics ["likeCount" ]) + float (
136
- statistics ["dislikeCount" ]
137
- )
138
- except (LookupError , ValueError ):
139
- total_votes = 0
140
137
141
- if total_votes != 0 :
142
- # format
143
- likes = pluralize_suffix (int (statistics ["likeCount" ]), "like" )
144
- dislikes = pluralize_suffix (int (statistics ["dislikeCount" ]), "dislike" )
138
+ like_data = statistics .get ("likeCount" )
139
+ dislike_data = statistics .get ("dislikeCount" )
145
140
146
- percent = 100 * float (statistics ["likeCount" ]) / total_votes
147
- out += " - {}, {} (\x02 {:.1f}\x02 %)" .format (likes , dislikes , percent )
141
+ if like_data :
142
+ # format
143
+ likes = int (like_data )
144
+ out += " - {}" .format (pluralize_suffix (likes , "like" ))
145
+ if dislike_data :
146
+ dislikes = int (dislike_data )
147
+ total_votes = likes + dislikes
148
+ percent = 100 * likes / total_votes
149
+ out += ", {} (\x02 {:.1f}\x02 %)" .format (
150
+ pluralize_suffix (dislikes , "dislike" ), percent
151
+ )
148
152
149
153
if "viewCount" in statistics :
150
154
views = int (statistics ["viewCount" ])
@@ -178,7 +182,7 @@ def get_video_id(text: str) -> str:
178
182
json = request .json ()
179
183
180
184
if not json .get ("items" ):
181
- raise NoResultsError ()
185
+ raise NoResultsError (request )
182
186
183
187
video_id = json ["items" ][0 ]["id" ]["videoId" ] # type: str
184
188
return video_id
0 commit comments