-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[EdfColorDay Bridge] add new bridge #4370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
floviolleau
wants to merge
6
commits into
RSS-Bridge:master
Choose a base branch
from
floviolleau:feature/edf-tempo-color-day-upstream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e47ea28
[EdfColorDay Bridge] add new bridge
floviolleau 934c5be
[EdfColorDay Bridge] lint
floviolleau 23834c9
[EdfColorDayBridge] PR review
floviolleau 4bacbfa
[EdfColorDayBridge] Set default cache to 2hr
floviolleau 9f867d2
[EdfColorDayBridge] Remove unused increment of variable $i
floviolleau 8a9809d
[EdfColorDayBridge] Remove unused variable + fix URI
floviolleau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php | ||
|
|
||
| class EdfColorDayBridge extends BridgeAbstract | ||
| { | ||
| const NAME = 'EDF tempo color'; | ||
| // pull info from this site for now because EDF do not provide correct opendata | ||
| const URI = 'https://www.services-rte.com/cms/open_data/v1/tempo'; | ||
| const DESCRIPTION = 'Get EDF color of today and tomorrow of tempo contract'; | ||
| const MAINTAINER = 'floviolleau'; | ||
| const PARAMETERS = [ | ||
| [ | ||
| 'contract' => [ | ||
| 'name' => 'Choisir un contrat', | ||
| 'type' => 'list', | ||
| // we can add later more option prices like EJP | ||
| 'values' => [ | ||
| 'Tempo' => 'tempo' | ||
| ], | ||
| ] | ||
| ] | ||
| ]; | ||
| const CACHE_TIMEOUT = 7200; // 2h | ||
|
|
||
| /** | ||
| * @param string $json | ||
| * @return void | ||
| */ | ||
| private function tempo(string $json): void | ||
| { | ||
| $jsonDecoded = Json::decode($json); | ||
|
|
||
| $values = [ | ||
| $this->formatFrenchDate('now') => date('Y-m-d'), | ||
| 'Demain ' . $this->formatFrenchDate('tomorrow') => date('Y-m-d', strtotime('+1 day')) | ||
| ]; | ||
|
|
||
| foreach ($values as $key => $value) { | ||
| $item = []; | ||
|
|
||
| $text = $key . ' : ' . $this->getDisplayableColor($jsonDecoded['values'][$value]); | ||
| $item['uri'] = self::URI . '?season=' . $this->getTempoYear(); | ||
| $item['title'] = $text; | ||
| $item['author'] = self::MAINTAINER; | ||
| $item['content'] = $text; | ||
| $item['uid'] = hash('sha256', $item['title']); | ||
|
|
||
| $this->items[] = $item; | ||
| } | ||
| } | ||
|
|
||
| private function formatFrenchDate(string $datetime): string | ||
| { | ||
| // Set the locale and date format | ||
| $locale = 'fr_FR'; | ||
| $formatter = new IntlDateFormatter( | ||
| $locale, | ||
| IntlDateFormatter::FULL, // Full date format | ||
| IntlDateFormatter::NONE, // No time | ||
| null, // Default timezone | ||
| IntlDateFormatter::GREGORIAN, // Gregorian calendar | ||
| 'EEEE dd MMMM yyyy' // Custom pattern | ||
| ); | ||
|
|
||
| // Create a DateTime object for the desired date | ||
| $now = new DateTime($datetime); | ||
|
|
||
| // Format the date | ||
| return $formatter->format($now); | ||
| } | ||
|
|
||
| private function getDisplayableColor(?string $color): string | ||
| { | ||
| $displayableColor = null; | ||
| switch ($color) { | ||
| case 'BLUE': | ||
| $displayableColor = '🟦 TEMPO_BLEU'; | ||
| break; | ||
| case 'WHITE': | ||
| $displayableColor = '⬜ TEMPO_BLANC'; | ||
| break; | ||
| case 'RED': | ||
| $displayableColor = '🟥 TEMPO_ROUGE'; | ||
| break; | ||
| default: | ||
| $displayableColor = '⬛ NON_DEFINI'; | ||
| break; | ||
| } | ||
|
|
||
| return $displayableColor; | ||
| } | ||
|
|
||
| private function getTempoYear(): string | ||
| { | ||
| $month = date('n'); // Current month as a number (1-12) | ||
| $year = date('Y'); // Current year | ||
|
|
||
| // Assuming the tempo year starts in September | ||
| if ($month >= 9) { | ||
| return $year . '-' . ($year + 1); // e.g., 2024-2025 | ||
| } | ||
|
|
||
| return ($year - 1) . '-' . $year; // e.g., 2023-2024 | ||
| } | ||
|
|
||
| public function collectData() | ||
| { | ||
| $contract = $this->getKey('contract'); | ||
|
|
||
| $header = [ | ||
| 'Accept: application/json', | ||
| ]; | ||
| $opts = [ | ||
| CURLOPT_HTTPGET => 1, | ||
| ]; | ||
|
|
||
| $json = getContents(self::URI . '?season=' . $this->getTempoYear(), $header, $opts); | ||
|
|
||
| if ($contract === 'Tempo') { | ||
| $this->tempo($json); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.