news.tatooine.club/_plugins/fetch_posts.rb

138 lines
3.7 KiB
Ruby
Raw Normal View History

2023-06-06 09:13:12 +00:00
# frozen_string_literal: true
require 'sanitize'
require 'uri'
require 'net/http'
require 'set'
require 'date'
2023-06-08 13:20:34 +00:00
SANITIZE_CONFIG = {
:elements => ['a', 'span', 'p', 'i', 'br'],
:attributes => {
'a' => ['href', 'title']
},
:protocols => {
'a' => {'href' => ['http', 'https']}
}
}
2023-06-06 09:13:12 +00:00
class PageWithoutAFile < Jekyll::Page
def read_yaml(*)
@data ||= {}
end
end
class BeatrootNews < Jekyll::Generator
safe true
2023-06-06 09:48:37 +00:00
priority :highest
2023-06-06 09:13:12 +00:00
SOURCE_URL = "https://beatrootnews.com/api.php/article?page%5Blimit%5D=60&sort=-publishing_date"
# Make a request to SOURCE_URL, and return the parsed JSON
def get_content
uri = URI.parse(SOURCE_URL)
response = Net::HTTP.get_response(uri)
JSON.parse(response.body)['data']
end
# Main plugin action, called by Jekyll-core
def generate(site)
@site = site
topics = Set.new
get_content.each do |article|
page = make_page(article['attributes']['modules'])
if page
site.pages << page
page['topics'].each { |t| topics.add(t) }
end
end
topics.each do |topic|
@site.pages << make_topic_page(topic)
end
2023-06-06 09:26:25 +00:00
site.config['topics'] = topics.to_a.sort
2023-06-06 09:13:12 +00:00
end
private
def make_topic_page(topic)
PageWithoutAFile.new(@site, __dir__, topic, "index.html").tap do |file|
file.data.merge!(
'title' => topic.capitalize,
'layout' => 'topic',
'topic' => topic,
2023-06-09 08:20:12 +00:00
'permalink' => "/#{topic}/"
2023-06-06 09:13:12 +00:00
)
file.output
end
end
# Generates contents for a file
def timestamp(ts)
d = Time.at(ts.to_i).to_datetime
d.new_offset("+0530")
end
2023-06-12 09:14:43 +00:00
def syndicated?(article)
sources = article['sources'].map(&:downcase)
return !(sources & @site.config['syndication_sources']).empty?
end
2023-06-06 09:13:12 +00:00
def make_page(article)
return nil if article['topic'].nil?
n = DateTime.new
now = DateTime.new(n.year, n.month, n.day, 23, 59, 59, "+0530")
2023-06-12 09:14:43 +00:00
date = timestamp(article['updated_on'])
days_ago = (now - date).floor
return nil if days_ago > 2
2023-06-06 09:13:12 +00:00
PageWithoutAFile.new(@site, __dir__, article['id'], "index.html").tap do |file|
html = article['body_json']['blocks'].map{ |t| t['data']['text']}.join(" ")
2023-06-09 16:21:43 +00:00
html = Sanitize.fragment(html, SANITIZE_CONFIG)
2023-06-06 09:13:12 +00:00
topics = article['topic'].map { |topic| topic.split('-').first }
2023-06-26 09:33:33 +00:00
twt = nil
2023-06-12 09:14:43 +00:00
2023-06-06 09:13:12 +00:00
if article['trigger_warning']
2023-06-26 09:33:33 +00:00
twt = article['trigger_warning_text']
unless twt.downcase.include? 'trigger'
twt = 'Trigger Warning: ' + twt
end
html = "<b>#{twt}</b><br>" + html
2023-06-06 09:13:12 +00:00
end
2023-06-09 16:21:43 +00:00
file.content = html
2023-06-06 09:13:12 +00:00
file.data.merge!(
'sources' => article['sources'],
"date" => date,
2023-06-09 16:21:43 +00:00
"id" => article['id'],
"slug" => article['slug'],
2023-06-06 09:13:12 +00:00
"title" => article['title'],
"layout" => 'article',
"topics" => topics,
2023-06-12 09:14:43 +00:00
"days_ago" => days_ago,
2023-06-14 05:33:48 +00:00
# We use 300 characters here
# and the SEO plugin strips down to 200 with ellepsis
"description" => Sanitize.fragment(html)[0...199] + "",
2023-06-26 09:33:33 +00:00
"trigger_warning" => twt,
2023-06-12 09:14:43 +00:00
"syndicated" => syndicated?(article),
2023-06-09 09:05:26 +00:00
"seo" => {
"type" => "NewsArticle",
"links" => [
"https://app.beatrootnews.com/#article-#{article['id']}",
"https://beatrootnews.com/custom/share?type=article&slug=#{article['slug']}"
],
"date_modified" => date
},
2023-10-25 06:34:41 +00:00
"media_link" => article['media_link'] ? article['media_link'] : nil,
2023-06-09 09:05:26 +00:00
# This is currently disabled because the page doesn't load in desktop
2023-06-12 09:14:43 +00:00
# Or rather doesn't load at all for old links.
2023-06-09 09:05:26 +00:00
# "canonical_url" => "https://app.beatrootnews.com/#article-#{article['id']}"
2023-06-06 09:13:12 +00:00
)
file.output
end
end
end