From 56689a3f16b638c7ab323dec7d69059da6ca84bd Mon Sep 17 00:00:00 2001 From: HOnza Koudelka Date: Thu, 24 Mar 2022 00:39:43 +0100 Subject: [PATCH] Updated to support multiple news feeds (currently AllSides.com and Perigon) AllSides Media Bias Ratings can now be shown for most articles obtaind from other feeds as well Improved error handling for IP geolocation --- helpukraine.php | 89 +++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/helpukraine.php b/helpukraine.php index cd7b805..46e8511 100644 --- a/helpukraine.php +++ b/helpukraine.php @@ -29,7 +29,13 @@ function get_url($url) if (ini_get('allow_url_fopen')) { // The easiest way - return file_get_contents($url); + $result = @file_get_contents($url); + if ($result == false) + { + $error = error_get_last(); + return json_encode(["status" => $error["message"]]); + } + return $result; } elseif (function_exists('curl_init')) { @@ -88,24 +94,18 @@ function helpukraine_get_news(&$news_list_en, &$news_list_ru, &$news_articles, & $news_json = get_url("https://24usw.com/getuanews"); $news_decoded = json_decode($news_json, true); $status = $news_decoded["status"] ?? "Invalid result: $news_json"; - $translations = [ - "en" => [ - "source" => "Source", - "no_news" => "No news found", - "error" => "Error fetching news, please try again later", - ], - "ru" => [ - "source" => "Источник", - "no_news" => "Новости не найдены", - "error" => "Ошибка при получении новостей, повторите попытку позже", - "Left" => "Левая", - "Lean Left" => "Наклон влево", - "Center" => "Центр", - "Lean Right" => "Наклон вправо", - "Right" => "Правая", - "Mixed" => "Смешанная", - "Bias" => "Предвзятость" - ] + $ru = [ + "source" => "Источник", + "no_news" => "Новости не найдены", + "error" => "Ошибка при получении новостей, повторите попытку позже", + "via" => "через", + "Left" => "Левая", + "Lean Left" => "Наклон влево", + "Center" => "Центр", + "Lean Right" => "Наклон вправо", + "Right" => "Правая", + "Mixed" => "Смешанная", + "Bias" => "Рейтинг предвзятости СМИ AllSides" ]; if ($status == "ok") { @@ -117,8 +117,8 @@ function helpukraine_get_news(&$news_list_en, &$news_list_ru, &$news_articles, & $id = 0; if (empty($articles)) { - $news_list_en = $translations["en"]["no_news"]; - $news_list_ru = $translations["ru"]["no_news"]; + $news_list_en = "No news found"; + $news_list_ru = $ru["no_news"]; return; } foreach ($articles as $article) @@ -127,28 +127,43 @@ function helpukraine_get_news(&$news_list_en, &$news_list_ru, &$news_articles, & $author = empty($article["author"] ?? "") ? "" : ($article["author"] . " | "); $link = $article["link"] ?? ""; $pubdate = $article["pubDate"] ?? ""; - $rating_en = empty($article["rating"] ?? "") ? "" : (" | Bias: " . $article["rating"]); - $rating_ru = empty($rating_en) ? "" : (" | " . $translations["ru"]["Bias"] . ": " . $translations["ru"][$article["rating"]]); - $source_en = $author . ($article["source_en"] ?? "") . $rating_en; - $source_ru = $author . ($article["source_ru"] ?? "") . $rating_ru; + $feed = $article["feed"] ?? ""; + + $rating_en = $article["rating"] ?? ""; + $rating_ru = empty($rating_en) ? "" : $ru[$article["rating"]]; + if (!empty($rating_en)) $rating_en = "AllSides Media Bias Rating: " . $rating_en; + if (!empty($rating_ru)) $rating_ru = $ru["Bias"] . ": " . $rating_ru; + + $rating_url = $article["rating_url"] ?? ""; + if (!empty($rating_url)) + { + if (!empty($rating_en)) $rating_en = "$rating_en"; + if (!empty($rating_ru)) $rating_ru = "$rating_ru"; + } + + $source_en = "$pubdate " . $author . ($article["source_en"] ?? "") . "" . + (empty($feed) ? "" : " via $feed") . (empty($rating_en) ? "" : (" | " . $rating_en)); + $source_ru = "$pubdate " . $author . ($article["source_ru"] ?? "") . "" . + (empty($feed) ? "" : " " . $ru["via"] . " $feed") . (empty($rating_ru) ? "" : (" | " . $rating_ru)); $title_en = $article["title_en"] ?? ""; $title_ru = $article["title_ru"] ?? ""; $summary_en = $article["summary_en"] ?? ""; $summary_ru = $article["summary_ru"] ?? ""; $content_en = $article["content_en"] ?? ""; $content_ru = $article["content_ru"] ?? ""; + if (!empty($title_en) and !empty($summary_en) and !empty($source_en)) { if (empty($content_en)) { - $news_list_en .= "

$title_en
$summary_en
" . - "$pubdate $source_en

\n"; + $news_list_en .= "

$title_en
$summary_en
" . + "$source_en

\n"; } else { $news_list_en .= "

$title_en
$summary_en " . "» Read More
" . - "$pubdate $source_en

\n"; + "$source_en

\n"; $news_articles .= << @@ -157,7 +172,7 @@ function helpukraine_get_news(&$news_list_en, &$news_list_ru, &$news_articles, &

$title_en

$content_en

-

$pubdate $source_en

+

$source_en

@@ -169,14 +184,14 @@ function helpukraine_get_news(&$news_list_en, &$news_list_ru, &$news_articles, & { if (empty($content_en)) { - $news_list_ru .= "

$title_ru
$summary_ru
" . - "$pubdate $source_ru

\n"; + $news_list_ru .= "

$title_ru
$summary_ru
" . + "$source_ru

\n"; } else { $news_list_ru .= "

$title_ru
$summary_ru " . "» Читать далее
" . - "$pubdate $source_ru

\n"; + "$source_ru

\n"; $news_articles .= << @@ -185,7 +200,7 @@ function helpukraine_get_news(&$news_list_en, &$news_list_ru, &$news_articles, &

$title_ru

$content_ru

-

$pubdate $source_ru

+

$source_ru

@@ -197,8 +212,8 @@ function helpukraine_get_news(&$news_list_en, &$news_list_ru, &$news_articles, & } else { - $news_list_en = $translations["en"]["error"] . " ($status)"; - $news_list_ru = $translations["ru"]["error"] . " ($status)"; + $news_list_en = "Error fetching news, please try again later ($status)"; + $news_list_ru = $ru["error"] . " ($status)"; $credits_en = ""; $credits_ru = ""; } @@ -247,7 +262,7 @@ function helpukraine_show_the_webpage()

Stop The War!

-

On the 24th of February 2022 Russian armed forces invaded Ukraine and started war against a sovereign country. The attack came from both Russia and Belarus. Thousands of Ukrainian and Russian soldiers and hundreds of Ukrainian civilians have already been killed.

+

On the 24th of February 2022 Russian armed forces invaded Ukraine and started war against a sovereign country. The attack came from both Russia and Belarus. Thousands of Ukrainian and Russian soldiers and hundreds of Ukrainian civilians, including women and children, have already been killed.

If you protest against the war, you may get arrested for up to 15 years. Even if you just say that attacking another country with weapons is a war, you may be prosecuted the same way.

But if you close your eyes and stay quiet, you will be an accomplice to the mass murderer who claims to be your president.

What do you choose?

@@ -262,7 +277,7 @@ function helpukraine_show_the_webpage()

Остановите войну!

-

24 февраля 2022 года российские вооруженные силы вторглись в Украину и начали войну против суверенной страны. Нападение произошло как со стороны России, так и со стороны Беларуси. Тысячи украинских и российских солдат и сотни украинских гражданских лиц уже убиты.

+

24 февраля 2022 года российские вооруженные силы вторглись в Украину и начали войну против суверенной страны. Нападение произошло как со стороны России, так и со стороны Беларуси. Тысячи украинских и российских солдат и сотни украинских гражданских лиц, включая женщин и детей, уже убиты.

Если вы протестуете против войны, вас могут арестовать на срок до 15 лет. Даже если вы просто скажете, что нападение на другую страну с оружием - это война, вас могут привлечь к ответственности таким же образом.

Но если вы закроете глаза и будете молчать, вы станете сообщником массового убийцы, который называет себя вашим президентом.

Что вы выберете?