CompuGlobalAPI¶
- class compuglobal.aio.CompuGlobalAPI(url, title, timeout)
Represents an API Wrapper used for accessing the cghmc API endpoints.
- Parameters
url (str) – The url of the API.
title (str) – The title of the tv show/movie/skit that the url leads to.
timeout (float) – The timeout for websocket read.
- Variables
random_url (str) – Endpoint used for getting a random screencap.
caption_url (str) – Endpoint for getting caption info using episode and timestamp
e = episode & t = timestamp
.search_url (str) – Endpoint for getting screencaps using a search query
q = search query
.frames_url (str) – Endpoint for getting all valid frames before & after an episode and timestamp
episode/timestamp/before/after
.nearby_url (str) – Endpoint for getting all valid frames nearby an episode and timestamp
e = episode & t = timestamp
.episode_url (str) – Endpoint for getting episode info and subtitles from start to end for episode
episode/start/end
.
- static encode_caption(caption)
Loops through the caption and formats it using max_lines and max_chars and finally encodes it in base64 for use in the url.
- Parameters
caption (str) – The caption to format and encode.
- Returns
The caption encoded in base64.
- Return type
str
- format_caption(caption, max_lines=4, max_chars=24, shorten=True)
Loops through the caption and formats it using max_lines and max_chars and returns the formatted outcome.
- Parameters
caption (str) – The caption to format.
max_lines (int, optional) – The maximum number of lines of captions allowed.
max_chars (int, optional) – The maximum number of characters allowed per line.
shorten (bool, optional) – Whether to shorten the caption at its latest sentence ending.
- Returns
The formatted caption.
- Return type
str
- coroutine generate_gif(gif_url)
Performs a GET request using gif_url and returns the direct url for the gif once it has been generated.
- Parameters
gif_url (str) – The url of the gif to generate.
- Returns
The direct url for the generated gif
- Return type
str
- Raises
APIPageStatusError – Raises an exception if the status code of the request is not 200.
- coroutine get_frames(episode, timestamp, before, after)
Performs a GET request to the
api/frames/{episode}/{timestamp}/{before}/{after}
endpoint and gets a list of all valid frames before and after the timestamp of the episode.- Parameters
episode (str) – The episode key of the screencap.
timestamp (int) – The timestamp of the screencap.
before (int) – The number of milliseconds before the timestamp.
after (int) – The number of milliseconds after the timestamp.
- Returns
A list of valid frames before and after the timestamp of the episode, containing the id, episode and timestamp for each frame.
- Return type
list
- Raises
APIPageStatusError – Raises an exception if the status code of the request is not 200.
Note
Used for displaying the valid frames available for the gifmaker.
- coroutine get_random_screencap()
Performs a GET request to the
api/random
endpoint and gets a random TV Show screencap.- Returns
A random screencap object.
- Return type
compuglobal.Screencap
- Raises
APIPageStatusError – Raises an exception if the status code of the request is not 200.
Note
Used for getting a random screencap when clicking the “RANDOM” button.
- coroutine get_screencap(episode=None, timestamp=None, frame=None)
Performs a GET request to the
api/caption?e={}&t={}
endpoint and gets a TV Show screencap using episodee={}
and timestampt={}
- Parameters
episode (str) – The episode key of the screencap.
timestamp (int) – The timestamp of the screencap.
frame (compuglobal.Frame) – The frame of the screencap.
- Returns
A Screencap objecct for the episode and timestamp.
- Return type
compuglobal.Screencap
- Raises
APIPageStatusError – Raises an exception if the status code of the request is not 200.
TypeError – Raises an exception if the constructor does not receive episode and timestamp, or compuglobal.Frame
Note
Used for getting the episode info and caption shown below each screencap.
- static json_to_caption(subtitles_json)
Loops through the subtitles of the json response, concatenates all lines and returns all subtitles combined as a complete caption.
- Parameters
subtitles_json (dict) – The json response containing the subtitles of the screencap.
- Returns
caption – The subtitles combined as a complete caption.
- Return type
str
- coroutine search(search_text)
Performs a GET request to the
api/search?q=
endpoint and gets a list of search results using the search text as the search queryq={}
for the request.- Parameters
search_text (str) – The text/quote to search for.
- Returns
search_results – A list of search results containing the id, episode and timestamp for each result.
- Return type
list
- Raises
APIPageStatusError – Raises an exception if the status code of the request is not 200.
NoSearchResultsFound – Raises an exception if there are no search results found using search_text.
Note
Used for displaying all the search results and their screencaps.
- coroutine search_for_screencap(search_text)
Performs a GET request to the
api/search?q=
endpoint usingsearch()
to get a list of search results using search_text and gets a screencap using the episode and timestamp of the first search result.- Parameters
search_text (str) – The text/quote to search for.
- Returns
A screencap object of the first search result found using search_text.
- Return type
compuglobal.Screencap
- Raises
APIPageStatusError – Raises an exception if the status code of the request is not 200.
NoSearchResultsFound – Raises an exception if there are no search results found using search_text.
- static shorten_caption(caption)
Loops through the caption and trims it at its latest sentence ending (., !, ? or ♪).
- Parameters
caption (str) – The caption to shorten/trim.
- Returns
caption – The shortened caption, ending at its latest sentence ending.
- Return type
str