Plugin for NASA Astronomy Picture of the Day

Hi There,

as I stumbled upon NASA Astronomy Picture of the Day (http://apod.nasa.gov/) and wondered about them having a RSS feed, I subscribed to it in my tt-rss instance (which really works like a charm). As I realised, they just integrate a very small thumbnail in their feed, I created my first plugin to fetch the bigger image from the linked site, based on the jp_stuttman and the af_feedmod plugins. If someone is interested:

  1. create a folder large_apod in the plugins.local directory of your tt-rss instance

  2. create a file init.php and paste the following into it:

     <?php
     class large_apod extends Plugin {
    
         private $host;
    
         function about() {
             return array(1.0,
                     "Display large images in NASA APOD feed",
                     "t0t4");
         }
    
         function init($host) {
             $this->host = $host;
             $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
         }
    
         function hook_article_filter($article) {
             global $fetch_last_content_type;
    
             if (strpos($article['link'], 'apod.nasa.gov') === false) return $article; // skip other URLs
             if (isset($article['stored']['content'])) {
                     $article['content'] = $article['stored']['content'];
                     return $article; // skip already stored content
             }
    
             $doc = new DOMDocument();
             $link = trim($article['link']);
    
             $html = fetch_file_contents($link);
             $content_type = $fetch_last_content_type;
    
             $charset = false;
             if ($content_type) {
                     preg_match('/charset=(\S+)/', $content_type, $matches);
                     if (isset($matches[1]) && !empty($matches[1])) $charset = $matches[1];
             }
    
             if ($charset) {
                     $html = iconv($charset, 'utf-8', $html);
                     $charset = 'utf-8';
                     $html = '<?xml encoding="' . $charset . '">' . $html;
             }
    
             @$doc->loadHTML($html);
    
             if ($doc) {
                     $basenode = false;
                     $xpath = new DOMXPath($doc);
                     $entries = $xpath->query('(//img)');
    
                     if ($entries->length > 0) $basenode = $entries->item(0);
    
                     if ($basenode) {
                             $article['content'] = $doc->saveXML($basenode);
                     }
             }
    
             return $article;
         }
    
         function api_version() {
           return 2;
         }
     }
     ?>
    
  3. activate it in the preferences and resubscribe to the feed.

Please feel free to use it and please don’t slap me if it’s not working at your installation, suggestions to improve it are welcome.

Regards
T0t4

:relaxed: Many thanks indeed. I just tried it and it worked for me.

Is there a chance of you expending this to allow for a fixed size of the image & support the NASA image of the day? Their photos are generally huge in RSS.

t0t4:
Thanks for plugin! it work’s great!

Kierun: NASA image of the day look OK in my TTRSS ( NASA Image of the Day ) - in widescreen mode.

@mrflible

This is what I see:

Which is the full sized image.

I can see it this way:

Only disadvantage is image size to download - for this image 2,6MB

default CSS has images limited to maximum width, not sure why it doesn’t work for you @Kierun

I removed all the custom CSS I had just in case and that made no difference. Then used inspector to get the CSS of the image.

I am using git commit ecab435420438e355d45a4adea33aeee26b9ca0d.

ah the image is probably sourced from attachments, i guess the built-in css doesn’t cover those

you can try adding this to user css as a temporary workaround

div.cdmIntermediate img,
div.cdmIntermediate video {
	border-width : 0px;
	max-width : 98%;
	height : auto;
}

@fox … and your black voodoo CSS incantation worked like a charm. :grinning:

should be fixed in trunk now

@fox I pulled the latest git, removed the custom CSS, and it’s all fine. Many thanks indeed.

Nice plugin, thanks @t0t4! I put this up on github if anybody is interested.

I just switched to the Docker installation from the old host installation. I followed all the instructions for copying over themes and plugins and now I am getting this in the error log:

Uncaught Error: Call to undefined function iconv() in /var/www/html/tt-rss/plugins.local/large_apod/init.php:39 Stack trace: #0 /var/www/html/tt-rss/classes/rssutils.php(814): large_apod->hook_article_filter(Array) #1 /var/www/html/tt-rss/update.php(264): RSSUtils::update_rss_feed(‘573’, true) #2 {main} thrown

Not sure if this is an issue with the plugin or with my Docker setup. Thanks.

docker image is likely built without php-iconv. i’ll make a note to add it, maybe tomorrow.

this should be now fixed:

https://git.tt-rss.org/fox/ttrss-docker-compose/commit/c0443faf7fd01249c31d3bd7c7e493b6e050dd9e

https://git.tt-rss.org/fox/ttrss-docker-compose/commit/6cccc777d3e3d0e515b62d7170b65a98bf9b9cb6

if you’re using docker hub setup the plugin should start working next time image is updated. if you’re using dynamic setup you’ll have to pull scripts & rebuild the image.

thanks fox. searching led me to believe that was the case but I didn’t want to come in here assuming since i am still figuring docker out and this plugin is pretty old. i got my containers updated and everything is working. thanks again for the effort.