Skip to content

Commit

Permalink
Merge pull request #70 from racacax/improve-integration
Browse files Browse the repository at this point in the history
improve integration and fix providers
  • Loading branch information
racacax authored Aug 21, 2024
2 parents 59e3ba0 + 124b77e commit a26fe41
Show file tree
Hide file tree
Showing 34 changed files with 561 additions and 393 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress
#- name: Run cs fixer
# run: bin/php-cs-fixer fix --dry-run --diff
- name: Run cs fixer
run: bin/php-cs-fixer fix --dry-run --diff
- name: Run phpstan
run: bin/phpstan
- name: Run test suite
run: bin/phpunit --exclude-group integration
run: bin/phpunit --group validity

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"bin-dir": "bin"
},
"require": {
"php": ">=7.3",
"php": ">=7.4",
"ext-json": "*",
"ext-zlib": "*",
"ext-curl": "*",
Expand Down
1 change: 1 addition & 0 deletions integrity.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d9e7b299599ce84cfed845ef3900c121ebb93497903004a787da3ed8df8f55f8
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
failOnRisky="true"
failOnWarning="false"
verbose="true">

<extensions>
<extension class="racacax\XmlTvTest\AllTestsPassedHook" />
</extensions>
<testsuites>
<testsuite name="default">
<directory>tests</directory>
Expand Down
6 changes: 3 additions & 3 deletions src/Component/CacheFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function store(string $key, string $content)
}
$this->createdKeys[$key] = true;
$this->listFile[$key] = [
'file'=> $fileName,
'file' => $fileName,
'key' => $key
];
}
Expand All @@ -59,7 +59,7 @@ public function has(string $key): bool
}
if (file_exists($fileName)) {
$this->listFile[$key] = [
'file'=> $fileName,
'file' => $fileName,
'key' => $key
];

Expand All @@ -83,7 +83,7 @@ public function clearCache(int $maxCacheDay): void
$files = glob($this->basePath.DIRECTORY_SEPARATOR.'*');

foreach ($files as $file) {
if (time()-filemtime($file) >= 86400 * $maxCacheDay) {
if (time() - filemtime($file) >= 86400 * $maxCacheDay) {
unlink($file);
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/Component/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function (ProviderInterface $provider) use ($list) {
return
in_array(Utils::extractProviderName($provider), $list, true) ||
in_array(get_class($provider), $list, true)
;
;
}
);
}
Expand All @@ -86,7 +86,7 @@ public function generateEpg()
Logger::log(sprintf("\e[95m[EPG GRAB] \e[39mRécupération du guide des programmes (%s - %d chaines)\n", $guide['channels'], count($channels)));


$logs = ['channels'=>[], 'xml'=>[],'failed_providers'=>[]];
$logs = ['channels' => [], 'xml' => [],'failed_providers' => []];
$countChannel = 0;
foreach ($channels as $channelKey => $channelInfo) {
$countChannel++;
Expand All @@ -97,21 +97,23 @@ public function generateEpg()
$logs['channels'][$date][$channelKey] = [
'success' => false,
'provider' => null,
'cache'=> false,
'cache' => false,
'failed_providers' => [],
];
}
Logger::log(sprintf("\e[95m[EPG GRAB] \e[39m%s (%d/%d) : %s", $channelKey, $countChannel, count($channels), $date));

if ($this->cache->has($cacheKey)) {
Logger::log(" | \e[33mOK \e[39m- From Cache ".chr(10));
$logs['channels'][$date][$channelKey]["success"] = true;
$logs['channels'][$date][$channelKey]["cache"] = true;
$logs['channels'][$date][$channelKey]['success'] = true;
$logs['channels'][$date][$channelKey]['cache'] = true;

continue;
}
$channelFound = false;
foreach ($providers as $provider) {
$old_zone = date_default_timezone_get();

try {
$channel = @$provider->constructEPG($channelKey, $date);
} catch(\Throwable $e) {
Expand All @@ -129,7 +131,7 @@ public function generateEpg()
$logs['channels'][$date][$channelKey] = [
'success' => true,
'provider' => get_class($provider),
'cache'=> false,
'cache' => false,
];
$this->cache->store($cacheKey, $this->formatter->formatChannel($channel, $provider));
Logger::log(" | \e[32mOK\e[39m - ".Utils::extractProviderName($provider).chr(10));
Expand All @@ -146,7 +148,7 @@ public function generateEpg()
}
}
Logger::log("\e[95m[EPG GRAB] \e[39mRécupération du guide des programmes terminée...\n");
$logsFinal[$guide["channels"]] = $logs;
$logsFinal[$guide['channels']] = $logs;
}
Logger::debug(json_encode($logsFinal));
}
Expand Down Expand Up @@ -180,7 +182,7 @@ function (string $date) use ($channelKey) {
continue;
}
$cache = $this->cache->get($keyCache);
$channelId = explode("_", $keyCache)[0];
$channelId = explode('_', $keyCache)[0];
if(array_key_exists($channelId, $listAliases)) {
$cache = str_replace('channel="'.$channelId.'"', 'channel="'.$listAliases[$channelId].'"', $cache);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function getLastLog(): string
public static function log(string $log): void
{
self::$lastLog = $log;
if (self::$level==='none') {
if (self::$level === 'none') {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Component/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ protected function getContentFromURL(string $url, array $headers = []): string
$response = $this->client->get(
$url,
[
'headers'=> $headers,
'headers' => $headers,
'connect_timeout' => 1,
"timeout" => 20
'timeout' => 20
]
);
} catch (\Throwable $e) {
Expand Down
21 changes: 16 additions & 5 deletions src/Component/Provider/Bouygues.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ public function constructEPG(string $channel, string $date)
public function generateUrl(Channel $channel, \DateTimeImmutable $date): string
{
$param = [
'profile'=>'detailed',
'epgChannelNumber'=> $this->channelsList[$channel->getId()],
'eventCount'=>999,
'startTime'=>$date->format('Y-m-d\T04:00:00\Z'),
'endTime'=>$date->modify('+1 days')->format('Y-m-d\T03:59:59\Z')
'profile' => 'detailed',
'epgChannelNumber' => $this->channelsList[$channel->getId()],
'eventCount' => 999,
'startTime' => $date->format('Y-m-d\T04:00:00\Z'),
'endTime' => $date->modify('+1 days')->format('Y-m-d\T03:59:59\Z')
];

return 'https://epg.cms.pfs.bouyguesbox.fr/cms/sne/live/epg/events.json?' . http_build_query($param);
}

Expand All @@ -109,34 +110,44 @@ private function getCreditType(string $type): string
switch ($type) {
case 'Acteur':
$type = 'actor';

break;
case 'Réalisateur':
$type = 'director';

break;
case 'Scénariste':
$type = 'writer';

break;
case 'Producteur':
$type = 'producer';

break;
case 'Musique':
$type = 'composer';

break;
case 'Créateur':
$type = 'editor';

break;
case 'Présentateur vedette':
case 'Autre présentateur':
$type = 'presenter';

break;
case 'Commentateur':
$type = 'commentator';

break;
case 'Origine Scénario':
case 'Scénario':
$type = 'adapter';

break;
}

return $type;
}
}
69 changes: 37 additions & 32 deletions src/Component/Provider/DAZN.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ public function __construct(Client $client, ?float $priority = null)

private function getProgramTitle(array $match)
{
$title = $match["home"]["clubIdentity"]["officialName"] . " - " . $match["away"]["clubIdentity"]["officialName"];
$title = $match['home']['clubIdentity']['officialName'] . ' - ' . $match['away']['clubIdentity']['officialName'];
$isDAZN = false;
$name = ";";
foreach ($match["broadcasters"]["local"] as $broadcaster) {
$name = $broadcaster["name"]["fr-FR"];
$name = ';';
foreach ($match['broadcasters']['local'] as $broadcaster) {
$name = $broadcaster['name']['fr-FR'];

if ($broadcaster["code"] == "DAZ") {
if ($broadcaster['code'] == 'DAZ') {
$isDAZN = true;

break;
}
}
if (!$isDAZN) {
$title .= " (A suivre sur $name)";
}

return $title;
}

Expand All @@ -55,28 +57,28 @@ public function constructEPG(string $channel, string $date)
$this->jsonPerDay[$url] = json_decode($this->getContentFromURL($url), true);
}
$json = $this->jsonPerDay[$url];
if (count($json["matches"]) == 0) {
if (count($json['matches']) == 0) {
return false;
}
$week = $this->getWeek( new \DateTimeImmutable($date));
$week = $this->getWeek(new \DateTimeImmutable($date));

if ($channelId == "multiplex") {
if ($channelId == 'multiplex') {
$this->generateMultiplex($channelObj, $json['matches'], $startCurrentDate, $endCurrentDate, $week);
} else {
$match = $json['matches'][$channelId - 1];
$start = strtotime($match["date"]);
$end = strtotime($match["date"]) + self::$MATCH_DURATION;
$entries = [["startTime" => $startCurrentDate, "endTime" => min($start, $endCurrentDate), "prefix" => "Prochain match : "],
["startTime" => $start, "endTime" => $end, "prefix" => ""],
["startTime" => max($start, $startCurrentDate), "endTime" => $endCurrentDate, "prefix" => "Match précédent : "]];
$start = strtotime($match['date']);
$end = strtotime($match['date']) + self::$MATCH_DURATION;
$entries = [['startTime' => $startCurrentDate, 'endTime' => min($start, $endCurrentDate), 'prefix' => 'Prochain match : '],
['startTime' => $start, 'endTime' => $end, 'prefix' => ''],
['startTime' => max($start, $startCurrentDate), 'endTime' => $endCurrentDate, 'prefix' => 'Match précédent : ']];
foreach ($entries as $entry) {
if($entry["startTime"] > $endCurrentDate || $entry["endTime"] < $startCurrentDate) {
if($entry['startTime'] > $endCurrentDate || $entry['endTime'] < $startCurrentDate) {
continue;
}
$program = new Program($entry["startTime"], $entry["endTime"]);
$program->addCategory("Football");
$program = new Program($entry['startTime'], $entry['endTime']);
$program->addCategory('Football');
$program->addDesc("Ligue 1 - Semaine ${week}");
$program->addTitle($entry["prefix"] . $this->getProgramTitle($match));
$program->addTitle($entry['prefix'] . $this->getProgramTitle($match));
$channelObj->addProgram($program);
}
}
Expand All @@ -91,58 +93,61 @@ private function getWeek(\DateTimeImmutable $date)
foreach (array_reverse(array_flip(self::$WEEKS_TIMESTAMP), true) as $weekTimestamp => $week) {
if ($timestamp >= $weekTimestamp) {
$value = $week;

break;
}
}

return $value + 1;
}

public function generateUrl(Channel $channel, \DateTimeImmutable $date): string
{
$week = $this->getWeek($date);

return "https://ma-api.ligue1.fr/championship-matches/championship/1/game-week/$week?season=2024";
}

private function generateMultiplex(Channel $channel, array $matches, int $startCurrentDate, int $endCurrentDate,int $week)
private function generateMultiplex(Channel $channel, array $matches, int $startCurrentDate, int $endCurrentDate, int $week)
{
$groupedMatches = [];
$lastMatch = null;
$lastEndTime = $startCurrentDate;
foreach ($matches as $match) {
$startTime = strtotime($match["date"]);
$startTime = strtotime($match['date']);
$endTime = $startTime + self::$MATCH_DURATION;
if(is_null($lastMatch) || $lastMatch["endTime"] <= $startTime) {
$lastMatch = ["startTime" => $startTime, "endTime" => $endTime, "matches" => [["startTime" => $startTime, "title" => $this->getProgramTitle($match)]]];
if(is_null($lastMatch) || $lastMatch['endTime'] <= $startTime) {
$lastMatch = ['startTime' => $startTime, 'endTime' => $endTime, 'matches' => [['startTime' => $startTime, 'title' => $this->getProgramTitle($match)]]];
} else {
$lastMatch['endTime'] = $endTime;
$lastMatch["matches"][] = ["startTime" => $startTime, "title" => $this->getProgramTitle($match)];
$lastMatch['matches'][] = ['startTime' => $startTime, 'title' => $this->getProgramTitle($match)];
array_pop($groupedMatches);
}
$groupedMatches[] = $lastMatch;
}
foreach($groupedMatches as $groupedMatch) {
$start = $groupedMatch["startTime"];
$end = $groupedMatch["endTime"];
$start = $groupedMatch['startTime'];
$end = $groupedMatch['endTime'];
if($end < $startCurrentDate) {
continue;
}
$titles = array_map(function($match) { return $match["title"]; }, $groupedMatch["matches"]);
$titles = array_map(function ($match) { return $match['title']; }, $groupedMatch['matches']);
if(count($titles) == 1) {
$title = $titles[0];
} else {
$title = "Multiplex";
$title = 'Multiplex';
}
$endTime = min($start, $endCurrentDate);
$entries = [["startTime" => $lastEndTime, "endTime" => $endTime, "value" => "Match à venir"],
["startTime" => $start, "endTime" => $end, "value" => $title]];
$entries = [['startTime' => $lastEndTime, 'endTime' => $endTime, 'value' => 'Match à venir'],
['startTime' => $start, 'endTime' => $end, 'value' => $title]];

foreach ($entries as $entry) {
if($entry["startTime"] > $endCurrentDate || $entry["endTime"] < $startCurrentDate || $entry["startTime"] === $entry["endTime"]) {
if($entry['startTime'] > $endCurrentDate || $entry['endTime'] < $startCurrentDate || $entry['startTime'] === $entry['endTime']) {
continue;
}
$lastEndTime = $entry["endTime"];
$program = new Program($entry["startTime"], $entry["endTime"]);
$program->addCategory("Football");
$lastEndTime = $entry['endTime'];
$program = new Program($entry['startTime'], $entry['endTime']);
$program->addCategory('Football');
$program->addDesc("Ligue 1 - Semaine $week :\n".implode("\n", $titles));
$program->addTitle($entry['value']);
$channel->addProgram($program);
Expand Down
Loading

0 comments on commit a26fe41

Please sign in to comment.