CompuGlobalAPI

class compuglobal.CompuGlobalAPI(url, title)

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.

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 encodes it in base64 for use in the url.

Parameters

caption (str) – The caption to encode in base64.

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

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.

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.

get_nearby_frames(episode, timestamp)

Performs a GET request to the api/nearby?e={}&t={} endpoint and gets a list of seven nearby using the episode key e={} and timestamp t={} the episode.

Parameters
  • episode (str) – The episode key of the screencap.

  • timestamp (int) – The timestamp of the screencap.

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 seven frames in the “More from this scene:” frame selection screen with arrows.

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.

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 episode e={} and timestamp t={} or a frame

Parameters
  • episode (str, optional) – The episode key of the screencap.

  • timestamp (int, optional) – 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.

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

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 query q={} 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.

search_for_screencap(search_text)

Performs a GET request to the api/search?q= endpoint using search() 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

view_episode(episode, start, end)

Performs a GET request to the api/episode/{episode}/{start}/{end} endpoint and returns the json response containing episode information.

Parameters
  • episode (str) – The episode key of the screencap.

  • start (int) – The starting timestamp for the episode information.

  • end (int) – The ending timestamp for the episode information.

Returns

The json response containing the episode information and subtitles for the timestamps.

Return type

dict

Note

Used for displaying the rest of an episode when using the “View Episode” button next to each screencap.