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

View File

@ -3,7 +3,8 @@ declare(strict_types=1);
namespace OCA\GPodderSync\Core\EpisodeAction; namespace OCA\GPodderSync\Core\EpisodeAction;
class EpisodeActionReader { class EpisodeActionReader
{
const EPISODEACTION_IDENTIFIER = 'EpisodeAction{'; const EPISODEACTION_IDENTIFIER = 'EpisodeAction{';
@ -11,21 +12,29 @@ class EpisodeActionReader {
* @param string $episodeActionsString * @param string $episodeActionsString
* @return EpisodeAction[] * @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 = []; $episodeActions = [];
$episodeActionStrings = explode(self::EPISODEACTION_IDENTIFIER, $episodeActionsString); $episodeActionStrings = explode(self::EPISODEACTION_IDENTIFIER, $episodeActionsString);
array_shift($episodeActionStrings); array_shift($episodeActionStrings);
foreach($episodeActionStrings as $episodeActionString) { foreach ($episodeActionStrings as $episodeActionString) {
foreach ($patterns as $pattern) {
preg_match( preg_match(
'/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/', $pattern,
self::EPISODEACTION_IDENTIFIER . $episodeActionString, self::EPISODEACTION_IDENTIFIER . $episodeActionString,
$matches $matches
); );
if ($matches["action"] !== null) {
$episodeActions[] = new EpisodeAction( $episodeActions[] = new EpisodeAction(
$matches["podcast"], $matches["podcast"],
$matches["episode"], $matches["episode"],
@ -34,10 +43,13 @@ class EpisodeActionReader {
(int)$matches["started"], (int)$matches["started"],
(int)$matches["position"], (int)$matches["position"],
(int)$matches["total"], (int)$matches["total"],
$matches["guid"] ?? null,
); );
break;
}
} }
}
return $episodeActions; return $episodeActions;
} }
} }

View File

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

View File

@ -15,10 +15,24 @@ 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("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("PLAY", $episodeAction->getAction());
$this->assertSame("Tue May 18 23:45:11 GMT+02:00 2021", $episodeAction->getTimestamp()); $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(31, $episodeAction->getStarted());
$this->assertSame(36, $episodeAction->getPosition()); $this->assertSame(36, $episodeAction->getPosition());
$this->assertSame(2474, $episodeAction->getTotal()); $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 { public function testCreateFromMultipleEpisodesString(): void {