2019-09-07 19:27:44 +03:00
|
|
|
<?php
|
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
class TwitchBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const MAINTAINER = 'Roliga';
|
|
|
|
const NAME = 'Twitch Bridge';
|
|
|
|
const URI = 'https://twitch.tv/';
|
|
|
|
const CACHE_TIMEOUT = 300; // 5min
|
|
|
|
const DESCRIPTION = 'Twitch channel videos';
|
|
|
|
const PARAMETERS = [ [
|
|
|
|
'channel' => [
|
|
|
|
'name' => 'Channel',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'criticalrole',
|
|
|
|
'title' => 'Lowercase channel name as seen in channel URL'
|
|
|
|
],
|
|
|
|
'type' => [
|
|
|
|
'name' => 'Type',
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => [
|
|
|
|
'All' => 'all',
|
|
|
|
'Archive' => 'archive',
|
|
|
|
'Highlights' => 'highlight',
|
|
|
|
'Uploads' => 'upload',
|
|
|
|
'Past Premieres' => 'past_premiere',
|
|
|
|
'Premiere Uploads' => 'premiere_upload'
|
|
|
|
],
|
|
|
|
'defaultValue' => 'archive'
|
|
|
|
]
|
|
|
|
]];
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
const BROADCAST_TYPES = [
|
|
|
|
'all' => [
|
|
|
|
'ARCHIVE',
|
|
|
|
'HIGHLIGHT',
|
|
|
|
'UPLOAD',
|
|
|
|
'PAST_PREMIERE',
|
|
|
|
'PREMIERE_UPLOAD'
|
|
|
|
],
|
|
|
|
'archive' => 'ARCHIVE',
|
|
|
|
'highlight' => 'HIGHLIGHT',
|
|
|
|
'upload' => 'UPLOAD',
|
|
|
|
'past_premiere' => 'PAST_PREMIERE',
|
|
|
|
'premiere_upload' => 'PREMIERE_UPLOAD'
|
|
|
|
];
|
2020-10-30 16:50:36 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
$query = <<<'EOD'
|
2020-10-30 16:50:36 +03:00
|
|
|
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;
|
2022-11-08 23:17:32 +03:00
|
|
|
$channel = $this->getInput('channel');
|
|
|
|
$type = $this->getInput('type');
|
2022-07-01 16:10:30 +03:00
|
|
|
$variables = [
|
2022-11-08 23:17:32 +03:00
|
|
|
'channel' => $channel,
|
|
|
|
'types' => self::BROADCAST_TYPES[$type]
|
2022-07-01 16:10:30 +03:00
|
|
|
];
|
2023-09-20 04:15:15 +03:00
|
|
|
$response = $this->apiRequest($query, $variables);
|
|
|
|
$data = $response->data;
|
2022-11-08 23:17:32 +03:00
|
|
|
if ($data->user === null) {
|
|
|
|
throw new \Exception(sprintf('Unable to find channel `%s`', $channel));
|
|
|
|
}
|
2024-03-31 22:32:27 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
$user = $data->user;
|
2023-09-22 21:41:39 +03:00
|
|
|
if ($user->videos === null) {
|
2024-03-31 22:32:27 +03:00
|
|
|
// twitch regularly does this for unknown reasons
|
2024-04-06 19:07:45 +03:00
|
|
|
//$this->logger->info('Twitch returned empty set of videos', ['data' => $data]);
|
2024-03-31 22:32:27 +03:00
|
|
|
return;
|
2023-09-22 21:41:39 +03:00
|
|
|
}
|
2024-03-31 22:32:27 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
foreach ($user->videos->edges as $edge) {
|
|
|
|
$video = $edge->node;
|
2020-10-30 16:50:36 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
$url = 'https://www.twitch.tv/videos/' . $video->id;
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
$item = [
|
|
|
|
'uri' => $url,
|
|
|
|
'title' => $video->title,
|
|
|
|
'timestamp' => $video->publishedAt,
|
|
|
|
'author' => $user->displayName,
|
|
|
|
];
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// Add categories for tags and played game
|
|
|
|
$item['categories'] = $video->tags;
|
|
|
|
if (!is_null($video->game)) {
|
|
|
|
$item['categories'][] = $video->game->displayName;
|
|
|
|
}
|
2023-08-22 20:47:32 +03:00
|
|
|
|
|
|
|
$contentTags = $video->contentTags ?? [];
|
|
|
|
foreach ($contentTags as $tag) {
|
2022-07-01 16:10:30 +03:00
|
|
|
if (!$tag->isLanguageTag) {
|
|
|
|
$item['categories'][] = $tag->localizedName;
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// Add enclosures for thumbnails from a few points in the video
|
|
|
|
// Thumbnail list has duplicate entries sometimes so remove those
|
|
|
|
$item['enclosures'] = array_unique($video->thumbnailURLs);
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
/*
|
|
|
|
* Content format example:
|
|
|
|
*
|
|
|
|
* [Preview Image]
|
|
|
|
*
|
|
|
|
* Some optional video description.
|
|
|
|
*
|
|
|
|
* Duration: 1:23:45
|
|
|
|
* Views: 123
|
|
|
|
*
|
|
|
|
* Played games:
|
|
|
|
* * 00:00:00 Game 1
|
|
|
|
* * 00:12:34 Game 2
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
$item['content'] = '<p><a href="'
|
|
|
|
. $url
|
|
|
|
. '"><img src="'
|
|
|
|
. $video->previewThumbnailURL
|
|
|
|
. '" /></a></p><p>'
|
|
|
|
. $video->description // in markdown format
|
|
|
|
. '</p><p><b>Duration:</b> '
|
|
|
|
. $this->formatTimestampTime($video->lengthSeconds)
|
|
|
|
. '<br/><b>Views:</b> '
|
|
|
|
. $video->viewCount
|
|
|
|
. '</p>';
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// Add played games list to content
|
|
|
|
$item['content'] .= '<p><b>Played games:</b><ul>';
|
2023-09-22 21:59:45 +03:00
|
|
|
|
|
|
|
$momentEdges = $video->moments->edges ?? [];
|
|
|
|
if (count($momentEdges) > 0) {
|
|
|
|
foreach ($momentEdges as $momentEdge) {
|
|
|
|
$moment = $momentEdge->node;
|
2020-10-30 16:50:36 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
$item['categories'][] = $moment->description;
|
|
|
|
$item['content'] .= '<li><a href="'
|
|
|
|
. $url
|
|
|
|
. '?t='
|
|
|
|
. $this->formatQueryTime($moment->positionMilliseconds / 1000)
|
|
|
|
. '">'
|
|
|
|
. $this->formatTimestampTime($moment->positionMilliseconds / 1000)
|
|
|
|
. '</a> - '
|
|
|
|
. $moment->description
|
|
|
|
. '</li>';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$item['content'] .= '<li><a href="'
|
|
|
|
. $url
|
|
|
|
. '">00:00:00</a> - '
|
|
|
|
. ($video->game ? $video->game->displayName : 'No Game')
|
|
|
|
. '</li>';
|
|
|
|
}
|
|
|
|
$item['content'] .= '</ul></p>';
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
$item['categories'] = array_unique($item['categories']);
|
2020-10-30 16:50:36 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// e.g. 01:53:27
|
|
|
|
private function formatTimestampTime($seconds)
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
'%02d:%02d:%02d',
|
|
|
|
floor($seconds / 3600),
|
|
|
|
($seconds / 60) % 60,
|
|
|
|
$seconds % 60
|
|
|
|
);
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// e.g. 01h53m27s
|
|
|
|
private function formatQueryTime($seconds)
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
'%02dh%02dm%02ds',
|
|
|
|
floor($seconds / 3600),
|
|
|
|
($seconds / 60) % 60,
|
|
|
|
$seconds % 60
|
|
|
|
);
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2023-09-20 04:15:15 +03:00
|
|
|
/**
|
|
|
|
* GraphQL: https://graphql.org/
|
|
|
|
* Tool for developing/testing queries: https://github.com/skevy/graphiql-app
|
|
|
|
*
|
|
|
|
* Official instructions for obtaining your own client ID can be found here:
|
|
|
|
* https://dev.twitch.tv/docs/v5/#getting-a-client-id
|
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
private function apiRequest($query, $variables)
|
|
|
|
{
|
|
|
|
$request = [
|
2023-09-20 04:15:15 +03:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
2022-07-01 16:10:30 +03:00
|
|
|
];
|
2023-09-20 04:15:15 +03:00
|
|
|
$headers = [
|
|
|
|
'Client-ID: kimne78kx3ncx6brgo4mv6wki5h1ko',
|
2022-07-01 16:10:30 +03:00
|
|
|
];
|
|
|
|
$opts = [
|
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST',
|
2023-09-20 04:15:15 +03:00
|
|
|
CURLOPT_POSTFIELDS => json_encode($request),
|
2022-07-01 16:10:30 +03:00
|
|
|
];
|
2023-09-20 04:15:15 +03:00
|
|
|
$json = getContents('https://gql.twitch.tv/gql', $headers, $opts);
|
|
|
|
$result = Json::decode($json, false);
|
|
|
|
return $result;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
if (!is_null($this->getInput('channel'))) {
|
|
|
|
return $this->getInput('channel') . ' twitch videos';
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
return parent::getName();
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
if (!is_null($this->getInput('channel'))) {
|
|
|
|
return self::URI . $this->getInput('channel');
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
return parent::getURI();
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
public function detectParameters($url)
|
|
|
|
{
|
|
|
|
$params = [];
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// Matches e.g. https://www.twitch.tv/someuser/videos?filter=archives
|
|
|
|
$regex = '/^(https?:\/\/)?
|
2019-09-07 19:27:44 +03:00
|
|
|
(www\.)?
|
|
|
|
twitch\.tv\/
|
|
|
|
([^\/&?\n]+)
|
|
|
|
\/videos\?.*filter=
|
|
|
|
(all|archive|highlight|upload)/x';
|
2022-07-01 16:10:30 +03:00
|
|
|
if (preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['channel'] = urldecode($matches[3]);
|
|
|
|
$params['type'] = $matches[4];
|
|
|
|
return $params;
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
return null;
|
|
|
|
}
|
2019-09-07 19:27:44 +03:00
|
|
|
}
|