Skip to content

Commit

Permalink
mo dry
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv committed Oct 27, 2023
1 parent abfb4a9 commit dd8d104
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 401 deletions.
30 changes: 12 additions & 18 deletions src/FetchMeditation/EnglishJFT.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@

class EnglishJFT extends JFT
{
public function fetch(): JFTEntry
{
$data = $this->getData();
$entry = new JFTEntry(
$data['date'],
$data['title'],
$data['page'],
$data['quote'],
$data['source'],
$data['content'],
$data['thought'],
$data['copyright']
);
return $entry;
}

public function getLanguage(): JFTLanguage
{
return $this->settings->language;
}

private function getData(): array
public function fetch(): JFTEntry
{
libxml_use_internal_errors(true);
$data = HttpUtility::httpGet('https://www.jftna.org/jft/');
Expand All @@ -51,6 +35,16 @@ private function getData(): array
}
}
$result["copyright"] = preg_replace('/\s+/', ' ', str_replace("\n", "", $result["copyright"]));
return $result;

return new JFTEntry(
$result['date'],
$result['title'],
$result['page'],
$result['quote'],
$result['source'],
$result['content'],
$result['thought'],
$result['copyright']
);
}
}
30 changes: 12 additions & 18 deletions src/FetchMeditation/EnglishSPAD.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@

class EnglishSPAD extends SPAD
{
public function fetch()
{
$data = $this->getData();
$entry = new SPADEntry(
$data['date'],
$data['title'],
$data['page'],
$data['quote'],
$data['source'],
$data['content'],
$data['thought'],
$data['copyright']
);
return $entry;
}

public function getLanguage(): SPADLanguage
{
return $this->settings->language;
}

private function getData(): array
public function fetch(): SPADEntry
{
libxml_use_internal_errors(true);
$data = HttpUtility::httpGet('https://spadna.org');
Expand All @@ -50,6 +34,16 @@ private function getData(): array
}
}
$result["copyright"] = preg_replace('/\s+|\n/', ' ', $result["copyright"]);
return $result;

return new SPADEntry(
$result['date'],
$result['title'],
$result['page'],
$result['quote'],
$result['source'],
$result['content'],
$result['thought'],
$result['copyright']
);
}
}
46 changes: 20 additions & 26 deletions src/FetchMeditation/FrenchJFT.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@

class FrenchJFT extends JFT
{
public function fetch()
{
$data = $this->getData();
$entry = new JFTEntry(
$data['date'],
$data['title'],
$data['page'],
$data['quote'],
$data['source'],
$data['content'],
$data['thought'],
$data['copyright']
);
return $entry;
}

public function getLanguage(): JFTLanguage
{
return $this->settings->language;
}

private function getData(): array
public function fetch(): JFTEntry
{
libxml_use_internal_errors(true);
$data = HttpUtility::httpGet('https://jpa.narcotiquesanonymes.org/');
Expand All @@ -37,7 +21,7 @@ private function getData(): array
libxml_use_internal_errors(false);
$xpath = new \DOMXpath($doc);

$content = [
$result = [
'date' => '',
'quote' => '',
'source' => '',
Expand All @@ -51,38 +35,48 @@ private function getData(): array
// Extract the title
$titleElement = $xpath->query('//div[@class="cartouche"]/h1');
if ($titleElement->length > 0) {
$content['title'] = trim($titleElement->item(0)->textContent);
$result['title'] = trim($titleElement->item(0)->textContent);
}

// Extract the date
$dateElement = $xpath->query('//p[@class="info-publi"]');
if ($dateElement->length > 0) {
$content['date'] = trim($dateElement->item(0)->textContent);
$result['date'] = trim($dateElement->item(0)->textContent);
}

// Extract the quote and source
$quoteDiv = $xpath->query('//div[@class="chapo"]')->item(0);
$quotePTags = $quoteDiv->getElementsByTagName('p');
if ($quotePTags->length >= 2) {
$content['quote'] = trim($quotePTags->item(0)->textContent);
$content['source'] = trim($quotePTags->item(1)->textContent);
$result['quote'] = trim($quotePTags->item(0)->textContent);
$result['source'] = trim($quotePTags->item(1)->textContent);
}

// Extract the thought
$thoughtElement = $xpath->query('//h3[@class="h3 spip"]');
if ($thoughtElement->length > 0) {
$content['thought'] = trim($thoughtElement->item(0)->textContent);
$result['thought'] = trim($thoughtElement->item(0)->textContent);
}

// Extract the content
$textDiv = $xpath->query('//div[@class="texte"]');
if ($textDiv->length > 0) {
$content['content'] = [];
$result['content'] = [];
$pTags = $textDiv->item(0)->getElementsByTagName('p');
foreach ($pTags as $pTag) {
$content['content'][] = trim($pTag->textContent);
$result['content'][] = trim($pTag->textContent);
}
}
return $content;

return new JFTEntry(
$result['date'],
$result['title'],
$result['page'],
$result['quote'],
$result['source'],
$result['content'],
$result['thought'],
$result['copyright']
);
}
}
62 changes: 28 additions & 34 deletions src/FetchMeditation/ItalianJFT.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@

class ItalianJFT extends JFT
{
public function fetch()
{
$data = $this->getData();
$entry = new JFTEntry(
$data['date'],
$data['title'],
$data['page'],
$data['quote'],
$data['source'],
$data['content'],
$data['thought'],
$data['copyright']
);
return $entry;
}

public function getLanguage(): JFTLanguage
{
return $this->settings->language;
}

private function getData(): array
public function fetch(): JFTEntry
{
libxml_use_internal_errors(true);
$data = HttpUtility::httpGet('https://na-italia.org/get-jft');
Expand All @@ -40,7 +24,7 @@ private function getData(): array

$paragraphs = $xpath->query('//p');

$result = [
$initialResult = [
'quote' => '',
'source' => '',
'thought' => '',
Expand All @@ -50,16 +34,16 @@ private function getData(): array

// Split the title and date parts
$titleParts = explode(',', $data['title']);
$result['title'] = trim($titleParts[0]);
$result['date'] = trim(end($titleParts));
$initialResult['title'] = trim($titleParts[0]);
$initialResult['date'] = trim(end($titleParts));

// Populate the result array with paragraph content
foreach ($paragraphs as $index => $paragraph) {
$key = ($index === 0) ? 'quote' : ($index + 1);
$result[$key] = $paragraph->textContent;
$initialResult[$key] = $paragraph->textContent;
}

$resultArray = [
$result = [
'content' => [],
'thought' => '',
'source' => '',
Expand All @@ -69,27 +53,37 @@ private function getData(): array
$firstNumericKeyWithSource = null;

// Iterate through the result array and categorize content
foreach ($result as $key => $value) {
foreach ($initialResult as $key => $value) {
if (str_starts_with($value, '--')) {
$firstNumericKeyWithSource = $key;
$resultArray['source'] = substr($value, 2);
$result['source'] = substr($value, 2);
}
if (is_numeric($key)) {
$lastNumericKey = $key;
$resultArray['content'][$key] = $value;
$resultArray['thought'] = $value;
$result['content'][$key] = $value;
$result['thought'] = $value;
} else {
$resultArray[$key] = $value;
$result[$key] = $value;
}
}

// Remove unnecessary content entries
unset($resultArray['content'][$firstNumericKeyWithSource]);
unset($resultArray['content'][$lastNumericKey]);
$resultArray['content'] = array_values($resultArray['content']);
$quoteParts = explode('--', $resultArray['quote']);
$resultArray['quote'] = $quoteParts[0] ?? '';
$resultArray['source'] = $quoteParts[1] ?? $resultArray['source'];
return $resultArray;
unset($result['content'][$firstNumericKeyWithSource]);
unset($result['content'][$lastNumericKey]);
$result['content'] = array_values($result['content']);
$quoteParts = explode('--', $result['quote']);
$result['quote'] = $quoteParts[0] ?? '';
$result['source'] = $quoteParts[1] ?? $result['source'];

return new JFTEntry(
$result['date'],
$result['title'],
$result['page'],
$result['quote'],
$result['source'],
$result['content'],
$result['thought'],
$result['copyright']
);
}
}
80 changes: 1 addition & 79 deletions src/FetchMeditation/JFTEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getJson(): string
return json_encode((array)$this);
}

public function stripTags(): array
public function withoutTags(): array
{
return array_map(function ($item) {
if (is_array($item)) {
Expand All @@ -48,82 +48,4 @@ public function stripTags(): array
}
}, (array)$this);
}

public function getHtml(): string
{
$thought = str_replace('Just for today: ', '', $this->thought);
$paragraphs = "";

foreach ($this->content as $c) {
$paragraphs .= "<p>{$c}</p><br>";
}

return <<<HTML
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Just for Today Meditation</title>
<meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=yes">
<meta http-equiv="expires" content="-1">
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta charset="UTF-8" />
</head>
<body>
<table align="center">
<tr>
<td align="left"><h2>{$this->date}</h2></td>
</tr>
<tr>
<td align="center"><h1>{$this->title}</h1></td>
</tr>
<tr>
<td align="center">{$this->page}<br><br></td>
</tr>
<tr>
<td align="left"><i>{$this->quote}</i><br><br></td>
</tr>
<tr>
<td align="center">{$this->source}<br><br></td>
</tr>
<tr>
<td align="left">{$paragraphs}</td>
</tr>
<tr>
<td align="left">{$thought}<br><br></td>
</tr>
<tr>
<td align="center">{$this->copyright}</td>
</tr>
</table>
</body>
</html>
HTML;
}

public function getCss(): string
{
$paragraphs = "";
$count = 1;
foreach ($this->content as $c) {
$paragraphs .= "<p id=\"jft-content-$count\" class=\"jft-rendered-element\">$c</p>";
$count++;
}

return <<<CSS
<div id="jft-container" class="jft-rendered-element">
<div id="jft-date" class="jft-rendered-element">{$this->date}</div>
<div id="jft-title" class="jft-rendered-element">{$this->title}</div>
<div id="jft-page" class="jft-rendered-element">{$this->page}</div>
<div id="jft-quote" class="jft-rendered-element">{$this->quote}</div>
<div id="jft-quote-source" class="jft-rendered-element">{$this->source}</div>
<div id="jft-content" class="jft-rendered-element">
{$paragraphs}
</div>
<div id="jft-thought" class="jft-rendered-element">{$this->thought}</div>
<div id="jft-copyright" class="jft-rendered-element">{$this->copyright}</div>
</div>
CSS;
}
}
Loading

0 comments on commit dd8d104

Please sign in to comment.