parse optional guid from EpisodeAction request

This commit is contained in:
thrillfall 2021-08-23 02:45:38 +02:00
parent 20a647906a
commit ba0d4f4357
4 changed files with 63 additions and 28 deletions

View File

@ -11,6 +11,7 @@ class EpisodeAction {
private int $started;
private int $position;
private int $total;
private ?string $guid;
public function __construct(
string $podcast,
@ -19,7 +20,8 @@ class EpisodeAction {
string $timestamp,
int $started,
int $position,
int $total
int $total,
?string $guid
) {
$this->podcast = $podcast;
$this->episode = $episode;
@ -28,6 +30,7 @@ class EpisodeAction {
$this->started = $started;
$this->position = $position;
$this->total = $total;
$this->guid = $guid;
}
/**
@ -75,9 +78,15 @@ class EpisodeAction {
/**
* @return int
*/
public function getTotal(): int {
public function getTotal(): int
{
return $this->total;
}
public function getGuid() : ?string
{
return $this->guid;
}
}

View File

@ -3,41 +3,53 @@ declare(strict_types=1);
namespace OCA\GPodderSync\Core\EpisodeAction;
class EpisodeActionReader {
class EpisodeActionReader
{
const EPISODEACTION_IDENTIFIER = 'EpisodeAction{';
const EPISODEACTION_IDENTIFIER = 'EpisodeAction{';
/**
* @param string $episodeActionsString
* @return EpisodeAction[]
*/
public function fromString(string $episodeActionsString): array {
public function fromString(string $episodeActionsString): array
{
$patterns = [
'/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', guid=\')(?<guid>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/',
'/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/',
];
$episodeActions = [];
$episodeActionStrings = explode(self::EPISODEACTION_IDENTIFIER, $episodeActionsString);
array_shift($episodeActionStrings);
foreach($episodeActionStrings as $episodeActionString) {
$episodeActionStrings = explode(self::EPISODEACTION_IDENTIFIER, $episodeActionsString);
array_shift($episodeActionStrings);
preg_match(
'/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/',
self::EPISODEACTION_IDENTIFIER . $episodeActionString,
$matches
);
foreach ($episodeActionStrings as $episodeActionString) {
foreach ($patterns as $pattern) {
preg_match(
$pattern,
self::EPISODEACTION_IDENTIFIER . $episodeActionString,
$matches
);
$episodeActions[] = new EpisodeAction(
$matches["podcast"],
$matches["episode"],
$matches["action"],
$matches["timestamp"],
(int)$matches["started"],
(int)$matches["position"],
(int)$matches["total"],
);
}
if ($matches["action"] !== null) {
$episodeActions[] = new EpisodeAction(
$matches["podcast"],
$matches["episode"],
$matches["action"],
$matches["timestamp"],
(int)$matches["started"],
(int)$matches["position"],
(int)$matches["total"],
$matches["guid"] ?? null,
);
break;
}
}
}
return $episodeActions;
}
}

View File

@ -15,7 +15,7 @@ class AppTest extends TestCase {
private $container;
public function setUp() : void {
public function setUp(): void {
parent::setUp();
$app = new App('gpoddersync');
$this->container = $app->getContainer();

View File

@ -15,16 +15,30 @@ class EpisodeActionReaderTest extends TestCase {
$this->assertSame("https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ", $episodeAction->getEpisode());
$this->assertSame("PLAY", $episodeAction->getAction());
$this->assertSame("Tue May 18 23:45:11 GMT+02:00 2021", $episodeAction->getTimestamp());
$this->assertNull($episodeAction->getGuid());
$this->assertSame(31, $episodeAction->getStarted());
$this->assertSame(36, $episodeAction->getPosition());
$this->assertSame(2474, $episodeAction->getTotal());
}
public function testCreateFromStringWithGuid(): void {
$reader = new EpisodeActionReader();
$episodeActions = $reader->fromString("[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='https://dts.podtrac.com/redirect.mp3/chrt.fm/track/9EE2G/pdst.fm/e/rss.art19.com/episodes/db0e3500', guid='gid://art19-episode-locator/V0/KtdfKfLndyWetCZ6t2WhUEFOFArDXRExVIb4Z1fh-TU', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]");
$episodeAction = $episodeActions[0];
$this->assertSame("https://rss.art19.com/dr-death-s3-miracle-man", $episodeAction->getPodcast());
$this->assertSame("https://dts.podtrac.com/redirect.mp3/chrt.fm/track/9EE2G/pdst.fm/e/rss.art19.com/episodes/db0e3500", $episodeAction->getEpisode());
$this->assertSame("PLAY", $episodeAction->getAction());
$this->assertSame("Mon Aug 23 01:58:56 GMT+02:00 2021", $episodeAction->getTimestamp());
$this->assertSame("gid://art19-episode-locator/V0/KtdfKfLndyWetCZ6t2WhUEFOFArDXRExVIb4Z1fh-TU", $episodeAction->getGuid());
$this->assertSame(47, $episodeAction->getStarted());
$this->assertSame(54, $episodeAction->getPosition());
$this->assertSame(2252, $episodeAction->getTotal());
}
public function testCreateFromMultipleEpisodesString(): void {
$reader = new EpisodeActionReader();
$episodeActions = $reader->fromString('[EpisodeAction{podcast=\'https://example.com/feed.xml\', episode=\'https://example.com/episode1.mp3\', action=PLAY, timestamp=Tue May 18 23:45:11 GMT+02:00 2021, started=31, position=36, total=2474},EpisodeAction{podcast=\'https://example.com/feed.xml\', episode=\'https://example.com/episode2.mp3\', action=DOWNLOAD, timestamp=Tue May 18 23:46:42 GMT+02:00 2021, started=31, position=36, total=2474},EpisodeAction{podcast=\'https://example.org/feed.xml\', episode=\'https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ\', action=PLAY, timestamp=Tue May 18 23:45:14 GMT+02:00 2021, started=0, position=211, total=3121}]');
$this->assertSame("https://example.com/feed.xml", $episodeActions[0]->getPodcast());
$this->assertSame("https://example.com/episode1.mp3", $episodeActions[0]->getEpisode());
$this->assertSame("PLAY", $episodeActions[0]->getAction());