Skip to content

Commit

Permalink
[TwitchBridge] use helper function getGraphQLData
Browse files Browse the repository at this point in the history
  • Loading branch information
User123698745 committed Sep 6, 2023
1 parent 329f98f commit cc150e8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 60 deletions.
95 changes: 37 additions & 58 deletions bridges/TwitchBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,43 @@ class TwitchBridge extends BridgeAbstract

public function collectData()
{
$query = <<<'EOD'
query VODList($channel: String!, $types: [BroadcastType!]) {
user(login: $channel) {
displayName
videos(types: $types, sort: TIME) {
edges {
node {
id
title
publishedAt
lengthSeconds
viewCount
thumbnailURLs(width: 640, height: 360)
previewThumbnailURL(width: 640, height: 360)
description
tags
contentTags {
isLanguageTag
localizedName
}
game {
displayName
}
moments(momentRequestType: VIDEO_CHAPTER_MARKERS) {
edges {
node {
description
positionMilliseconds
}
$query = <<<'QUERY'
query VODList($channel: String!, $types: [BroadcastType!]) {
user(login: $channel) {
displayName
videos(types: $types, sort: TIME) {
edges {
node {
id
title
publishedAt
lengthSeconds
viewCount
thumbnailURLs(width: 640, height: 360)
previewThumbnailURL(width: 640, height: 360)
description
tags
contentTags {
isLanguageTag
localizedName
}
game {
displayName
}
moments(momentRequestType: VIDEO_CHAPTER_MARKERS) {
edges {
node {
description
positionMilliseconds
}
}
}
}
}
}
}
}
}
}
}
}
}
EOD;
QUERY;
$channel = $this->getInput('channel');
$type = $this->getInput('type');
$variables = [
Expand Down Expand Up @@ -205,37 +205,16 @@ private function formatQueryTime($seconds)
);
}

// GraphQL: https://graphql.org/
// Tool for developing/testing queries: https://github.com/skevy/graphiql-app
private function apiRequest($query, $variables)
{
$request = [
'query' => $query,
'variables' => $variables
];
/**
* Official instructions for obtaining your own client ID can be found here:
* https://dev.twitch.tv/docs/v5/#getting-a-client-id
*/
$header = [
$headers = [
'Client-ID: kimne78kx3ncx6brgo4mv6wki5h1ko'
];
$opts = [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($request)
];

Logger::debug("Sending GraphQL query:\n" . $query);
Logger::debug("Sending GraphQL variables:\n" . json_encode($variables, JSON_PRETTY_PRINT));
$response = json_decode(getContents('https://gql.twitch.tv/gql', $header, $opts));
Logger::debug("Got GraphQL response:\n" . json_encode($response, JSON_PRETTY_PRINT));

if (isset($response->errors)) {
$messages = array_column($response->errors, 'message');
throw new \Exception(sprintf('twitch api: `%s`', implode("\n", $messages)));
}

return $response->data;
return getGraphQLData('https://gql.twitch.tv/gql', $query, $variables, $headers);
}

public function getName()
Expand Down
2 changes: 2 additions & 0 deletions docs/06_Helper_functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ $data = getGraphQLData('your URI', $query, $variables);
return $data->items;
```

GraphQL: https://graphql.org

# returnError
**Notice:** Whenever possible make use of [`returnClientError`](#returnclienterror) or [`returnServerError`](#returnservererror)

Expand Down
8 changes: 6 additions & 2 deletions lib/contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ function getSimpleHTMLDOMCached(

/**
* Gets data from a GraphQL endpoint on the Internet using a GraphQL query string.
*
* * GraphQL: https://graphql.org
* @param string $url The GraphQL endpoint URL (usually ending with /graphql).
* @param string $query The GraphQL query string.
*
Expand Down Expand Up @@ -502,5 +502,9 @@ function getGraphQLData(string $url, string $query, array $variables = [], array
if (isset($response->errors)) {
throw new \GraphQLException(sprintf('result contains errors (%s):', substr($query, 0, strpos($query, '('))), $response->errors);
}
return $response->data;
$data = $response->data;
if (isset($response->extensions) && !isset($data->extensions)) {
$data->extensions = $response->extensions;
}
return $data;
}

0 comments on commit cc150e8

Please sign in to comment.