start to add socialter

This commit is contained in:
quenousimporte 2024-07-07 15:12:56 +02:00
parent 3133a4dd24
commit 44017ffd35
1 changed files with 119 additions and 2 deletions

121
epub.php
View File

@ -97,6 +97,118 @@
date_default_timezone_set('Europe/Paris'); date_default_timezone_set('Europe/Paris');
$date = (new DateTime('today'))->format('Ymd'); $date = (new DateTime('today'))->format('Ymd');
// Socialter
if (isset($_POST['socialter']) && $_POST['socialter'])
{
// parameters hardcoded for now
$includeimages = false;
$includepages = true;
$imagesonly = false;
$imagesize = $imagesonly ? "XLARGE" : "MEDIUM";
$url = $_POST['socurl'];
// extract url from curl command
if (str_starts_with($url, 'curl '))
{
$url = explode("'", $url)[1];
echo '<p>extracted url from curl command:</p><p>' . $url . '</p>';
}
$epub = new TPEpubCreator();
$epub->temp_folder = 'temp/';
$epub->epub_file = 'epub/socialter.epub';
$epub->title = 'Socialter ' . $date ;
// cache json in case url expires
$tempjsonpath = 'temp/' . hash('md5', $url) . '.json';
$json = '';
if (file_exists($tempjsonpath))
{
$json = file_get_contents($tempjsonpath);
}
else
{
$json = file_get_contents($url);
file_put_contents($tempjsonpath, $json);
}
$publication = json_decode($json);
$content = array_filter($publication->Content, function($item) { return $item->Category == 'Le Monde'; });
usort($content, function ($a, $b) { return $a->PageNumber - $b->PageNumber; });
if ($includepages)
{
$pageindex = json_decode(file_get_contents(str_replace('GetPublicationContentItems', 'GetPublicationPages', $url)));
}
$page = 0;
foreach ($content as $article)
{
if ($includepages && $article->PageNumber > $page)
{
$page = $article->PageNumber;
$pageobj = array_values(array_filter($pageindex->Page, function($p)
{
global $page;
return $page == $p->PageNumber;
}))[0];
$pageid = $pageobj->PublicationPageID;
$path = lm_download_image($url, 'Preview-' . $imagesize . '-', $pageid);
if ($page == 1)
{
// cover
$epub->AddImage($path, 'image/jpeg', true);
}
else
{
$epub->AddPage('<img style="width: 100%" src="' . $path . '">', false, 'Page ' . $page, true);
}
}
$articlebody = array_filter($article->ContentItem, function($item) { return $item->ContentType == 'text/xml'; });
$articlebody = array_values($articlebody)[0];
if ($articlebody->Title && $articlebody->HtmlText)
{
$pagecontent = '<h1>'. strip_tags($articlebody->Title) . '</h1>';
$pagecontent .= '<p>Page ' . $article->PageNumber . '</p>';
$author = array_filter($article->ContentItem, function($item) { return $item->ContentType == 'author/xml'; });
$author = array_values($author)[0];
if ($author->Author)
{
$pagecontent .= $author->Author;
}
if ($articlebody->Introduction)
{
$pagecontent .= '<b>' . $articlebody->Introduction . '</b>';
}
if ($includeimages)
{
$images = array_values(array_filter($article->ContentItem, function($item) { return $item->ContentType == 'graphic/jpeg' || $item->ContentType == 'image/jpeg'; }));
foreach ($images as $image)
{
$path = lm_download_image($url, 'Image-MEDIUM-', $image->ContentItemId);
$pagecontent .= '<p><img style="width: 100%" src="' . $path . '"></p>';
}
}
$pagecontent .= $articlebody->HtmlText;
if (!$imagesonly)
{
$epub->AddPage($pagecontent, false, strip_tags($articlebody->Title), true);
}
}
}
write_epub($epub);
}
// Le Monde // Le Monde
if (isset($_POST['lemonde']) && $_POST['lemonde']) if (isset($_POST['lemonde']) && $_POST['lemonde'])
{ {
@ -464,8 +576,13 @@
<input id="lemonde" name="lemonde" type="checkbox"> <input id="lemonde" name="lemonde" type="checkbox">
<label for="lemonde">From json url: </label> <label for="lemonde">From json url: </label>
<input name="lmurl"><br> <input name="lmurl"><br>
<!--Update MPSESSID: <input name="mpsessid"><br> <br>
Update NYT-S: <input name="nyts"><br>-->
<b>Socialter</b>
<br>
<input id="socialter" name="socialter" type="checkbox">
<label for="socialter">From json url: </label>
<input name="socurl"><br>
<br> <br>
<b>Mediapart</b> <b>Mediapart</b>