From 0489caabcad2c62b5e144eb1b2821bbc324ae013 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 29 Jan 2019 02:00:55 -0700 Subject: [PATCH] Add image thumbnail cache, do lots of adjustments --- .gitignore | 3 +- cache/.htaccess | 11 +++++++ cache/handle_404.php | 31 ++++++++++++++++++ cache/thumb/README.md | 3 ++ langs/en/categories.json | 3 +- langs/en/titles.json | 5 +-- lib/Base64.lib.php | 53 ++++++++++++++++++++++++++++++ lib/NewsCategory.lib.php | 11 ++++++- lib/NewsSource_NewsAPI.lib.php | 2 +- lib/NewsSource_Reddit.lib.php | 6 ++-- lib/Thumbnail.lib.php | 30 ++++++++++------- lib/Weather.lib.php | 2 +- lib/Weather_DarkSky.lib.php | 11 +++++++ pages/form.php | 26 --------------- pages/home.php | 59 ++++++++++++++++------------------ pages/news.php | 38 ++++++++++++++++------ settings.template.php | 6 +++- static/js/news.js | 4 ++- 18 files changed, 215 insertions(+), 89 deletions(-) create mode 100644 cache/.htaccess create mode 100644 cache/handle_404.php create mode 100644 cache/thumb/README.md create mode 100644 lib/Base64.lib.php create mode 100644 lib/Weather_DarkSky.lib.php delete mode 100644 pages/form.php diff --git a/.gitignore b/.gitignore index 207052b..98d0289 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ vendor settings.php nbproject/private *.sync-conflict* -/database.mwb.bak \ No newline at end of file +/database.mwb.bak +/cache/thumb/*.jpg \ No newline at end of file diff --git a/cache/.htaccess b/cache/.htaccess new file mode 100644 index 0000000..9603d64 --- /dev/null +++ b/cache/.htaccess @@ -0,0 +1,11 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# Redirect 404'd thumbnail requests to a script that will generate them. + +RewriteEngine on + +RewriteCond %{REQUEST_FILENAME} !-d +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^(.*)/?$ handle_404.php?file=$1 [QSA,L] diff --git a/cache/handle_404.php b/cache/handle_404.php new file mode 100644 index 0000000..ae93f14 --- /dev/null +++ b/cache/handle_404.php @@ -0,0 +1,31 @@ + "Business", @@ -26,7 +27,8 @@ class NewsCategory { self::HEALTH => "Health", self::SCIENCE => "Science", self::SPORTS => "Sports", - self::TECHNOLOGY => "Technology" + self::TECHNOLOGY => "Technology", + self::SOCIAL => "Social" ]; public function __construct(int $category) { @@ -61,6 +63,8 @@ class NewsCategory { return "sports"; case self::TECHNOLOGY: return "technology"; + case self::SOCIAL: + return "social"; default: return ""; } @@ -91,6 +95,9 @@ class NewsCategory { case "tech": $cat = self::TECHNOLOGY; break; + case "social": + $cat = self::SOCIAL; + break; } return new NewsCategory($cat); } @@ -115,6 +122,8 @@ class NewsCategory { return "fas fa-futbol"; case self::TECHNOLOGY: return "fas fa-laptop"; + case self::SOCIAL: + return "fas fa-share-alt"; default: return "fas fa-newspaper"; } diff --git a/lib/NewsSource_NewsAPI.lib.php b/lib/NewsSource_NewsAPI.lib.php index b8ae4d0..9e954aa 100644 --- a/lib/NewsSource_NewsAPI.lib.php +++ b/lib/NewsSource_NewsAPI.lib.php @@ -41,7 +41,7 @@ class NewsSource_NewsAPI extends NewsSource { $params['category'] = $category->toString(); } $items = []; - $json = ApiFetcher::get($url, $params, ["apiKey" => $apikey]); + $json = ApiFetcher::get($url, $params, ["apiKey" => $apikey], "+1 hour"); $data = json_decode($json, TRUE); if ($data['status'] != "ok") { return []; diff --git a/lib/NewsSource_Reddit.lib.php b/lib/NewsSource_Reddit.lib.php index e6d4a59..5df23d7 100644 --- a/lib/NewsSource_Reddit.lib.php +++ b/lib/NewsSource_Reddit.lib.php @@ -15,13 +15,13 @@ class NewsSource_Reddit extends NewsSource { } function loadItems() { - $this->loadSubreddit("news+worldnews+UpliftingNews", new NewsCategory(NewsCategory::GENERAL)); + $this->loadSubreddit("popular/top", new NewsCategory(NewsCategory::SOCIAL)); //$this->loadSubreddit("technology", new NewsCategory(NewsCategory::TECHNOLOGY)); } private function loadSubreddit(string $subreddit, NewsCategory $category) { $items = []; - $json = ApiFetcher::get("https://www.reddit.com/r/$subreddit.json", ["limit" => "50"]); + $json = ApiFetcher::get("https://www.reddit.com/r/$subreddit.json", ["limit" => "50", "sort" => "top", "t" => "day"]); $news = json_decode($json, TRUE)['data']['children']; foreach ($news as $d) { $n = $d['data']; @@ -58,6 +58,8 @@ class NewsSource_Reddit extends NewsSource { $source = $n['domain']; } + $source .= " via " . $n['subreddit_name_prefixed'] . ""; + $timestamp = time(); if (!empty($n['created'])) { $timestamp = $n['created']; diff --git a/lib/Thumbnail.lib.php b/lib/Thumbnail.lib.php index 2f0addf..12bb2a6 100644 --- a/lib/Thumbnail.lib.php +++ b/lib/Thumbnail.lib.php @@ -6,6 +6,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use \SKleeschulte\Base32; + class Thumbnail { /** @@ -29,7 +31,6 @@ class Thumbnail { $output = imagecreatetruecolor($width, $height); imagecopyresampled($output, $image, 0, 0, 0, 0, $width, $height, ImageSX($image), ImageSY($image)); - ob_end_flush(); ob_start(); imagejpeg($output, null, 75); $imagedata = ob_get_contents(); @@ -40,21 +41,26 @@ class Thumbnail { } /** - * Encodes some data to base64. - * @param type $img + * Make a thumbnail, save it to the disk cache, and return a url relative to + * the app root. + * @param string $url + * @param int $width + * @param int,bool $height * @return string */ - static function imgToBase64($img): string { - return base64_encode($img); + static function getThumbnailCacheURL(string $url, int $width = 150, $height = true): string { + $encodedfilename = Base64::encode($url); + $path = "cache/thumb/$encodedfilename.$width.jpg"; + + return $path; } - /** - * Get the base64 data: URI for a jpeg image - * @param type $img - * @return string - */ - static function jpegToBase64URI($img): string { - return "data:image/jpeg;base64," . self::imgToBase64($img); + static function addThumbnailToCache(string $url, int $width = 150, $height = true) { + $encodedfilename = Base64::encode($url); + $path = "cache/thumb/$encodedfilename.$width.jpg"; + $image = self::getThumbnailFromUrl($url, $width, $height); + file_put_contents(__DIR__ . "/../$path", $image); + return $image; } } diff --git a/lib/Weather.lib.php b/lib/Weather.lib.php index 3516f2c..c000ecf 100644 --- a/lib/Weather.lib.php +++ b/lib/Weather.lib.php @@ -7,5 +7,5 @@ */ class Weather { - + } \ No newline at end of file diff --git a/lib/Weather_DarkSky.lib.php b/lib/Weather_DarkSky.lib.php new file mode 100644 index 0000000..707fa92 --- /dev/null +++ b/lib/Weather_DarkSky.lib.php @@ -0,0 +1,11 @@ +setID("sampleform"); - -$form->addHiddenInput("page", "form"); - -$form->addInput("name", "John", "text", true, null, null, "Your name", "fas fa-user", 6, 5, 20, "John(ny)?|Steve", "Invalid name, please enter John, Johnny, or Steve."); -$form->addInput("location", "", "select", true, null, ["1" => "Here", "2" => "There"], "Location", "fas fa-map-marker"); -$form->addInput("textbox", "Hello world", "textarea", true, null, null, "Text area", "fas fa-font"); -$form->addInput("box", "1", "checkbox", true, null, null, "I agree to the terms of service"); - -$form->addButton("Submit", "fas fa-save", null, "submit", "savebtn"); - -$form->generate(); \ No newline at end of file diff --git a/pages/home.php b/pages/home.php index d101aa9..4dee109 100644 --- a/pages/home.php +++ b/pages/home.php @@ -16,39 +16,36 @@ foreach ($newsitems as $item) { } ?> +

+ get("Headlines"); ?> +

$items) { - $cat = NewsCategory::fromString($category); - ?> -

- get($cat->toString()); ?> -

-
- = 5) { - break; - } - $count++; - ?> - - +
+ = 6) { + break; } + $count++; ?> -
- + +

+ getHeadline()); ?> +

+

getSource()); ?>

+
+ + getImage())) { ?> + + +
+ + + \ No newline at end of file diff --git a/pages/news.php b/pages/news.php index 7a71fa5..364b137 100644 --- a/pages/news.php +++ b/pages/news.php @@ -10,9 +10,19 @@ $newsitems = News::getItems();