Add HN Proxy

master
Marvin Johanning 2022-02-06 16:33:05 +00:00
parent 1c0932fb68
commit fde375521c
8 changed files with 146 additions and 16 deletions

View File

@ -1,3 +1,7 @@
%%%
gem_header 20 'text/gemini'
%%%
```
███╗ ███╗██╗ ██╗███████╗██╗██████╗ █████╗ ██████╗ ██████╗
████╗ ████║╚██╗ ██╔╝██╔════╝██║██╔══██╗██╔══██╗██╔══██╗██╔══██╗
@ -28,6 +32,9 @@ Some information about the server running this capsule.
This contains my gemlog, the Gemini version of a blog. On here, I will be posting about various topics that interest me, so be sure to check out the About Me page to find out what that might be.
## Projects and Services
=> services/hackernews/index.bliz 📰Hacker News Proxy
A Gemini proxy for Hacker News
=> services/todo.gmi 📝Planned Services (To-Do List)
I am planning on writing a few proxy services on here. You can see what I have planned on this site.

View File

@ -5,6 +5,5 @@ set b_words (bliz_word_count)
%%%
%%%
set directory (dirname $blizfile)
ruby serve/test/test.rb
ruby serve/services/hackernews/comments.rb $req_query
%%%

View File

@ -0,0 +1,53 @@
require 'uri'
require 'net/http'
require 'json'
require 'date'
require 'nokogiri'
puts "=> index.bliz 📰 Back To Overview"
def get_article(id)
json = String.new
uri = URI("https://hacker-news.firebaseio.com/v0/item/#{id}.json?print=pretty)")
res = Net::HTTP.get_response(uri)
json << res.body if res.is_a?(Net::HTTPSuccess)
puts "# Comments for: #{JSON.parse(json)['title']}\n"
return json
end
def get_comment_ids(num, article)
counter = 0
comment_ids = Array.new
JSON.parse(article)["kids"].each do |comment|
comment_ids << comment
counter += 1
break if counter == num
end
return comment_ids
end
def display_comments(num, article_id)
json = Array.new
get_comment_ids(num, get_article(article_id)).each do |id|
uri = URI("https://hacker-news.firebaseio.com/v0/item/#{id}.json?print=pretty)")
res = Net::HTTP.get_response(uri)
json << res.body if res.is_a?(Net::HTTPSuccess)
end
json.each do |comment|
puts "## Comment by " + JSON.parse(comment)["by"]
puts Nokogiri::HTML(JSON.parse(comment)["text"]).text
puts DateTime.strptime(JSON.parse(comment)["time"].to_s, "%s").strftime("↳🕰 Published on %d/%m/%Y at %H:%M")
puts ""
end
end
display_comments(10, ARGV[0])

View File

@ -0,0 +1,8 @@
require_relative 'main.rb'
num_of_articles = ARGV[0]
if num_of_articles.numeric?() then
parse_and_print(num_of_articles.to_i)
else
parse_and_print(10)
end

View File

@ -0,0 +1,11 @@
%%%
if [ $req_query = "" ]
gem_header 10 'How many articles do you want to load?'
else
gem_header 20 'text/gemini'
end
%%%
%%%
ruby serve/services/hackernews/hackernews.rb $req_query
%%%

View File

@ -0,0 +1,66 @@
require 'uri'
require 'net/http'
require 'json'
require 'date'
puts "# Hacker News Proxy 📰"
puts "This is a small Hacker News Proxy written in Ruby. It is still a work in progress, but it already allows for the fetching of articles from the main page and reading of their comments."
puts ""
class String
def numeric?
!self.match(/[^0-9]/)
end
end
def get_ids(url)
uri = URI(url)
res = Net::HTTP.get_response(uri)
ids = Array.new
if res.is_a?(Net::HTTPSuccess) then
res.body.gsub("[", "").gsub("]", "").split(",").each do |item|
ids << item
end
end
return ids
end
def get_stories(num)
counter = 0
json = Array.new
get_ids('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty').each do |story|
story = story.gsub(" ", "")
uri = URI("https://hacker-news.firebaseio.com/v0/item/#{story}.json?print=pretty)")
res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess) then
json << res.body
counter += 1
break if counter == num
end
end
return json
end
def parse_and_print(num)
puts "#{num} article(s) have been loaded."
get_stories(num).each do |story|
puts "## " + JSON.parse(story)["title"] + " (⇧ " + JSON.parse(story)["score"].to_s + ")"
begin
puts "=> " + JSON.parse(story)["url"]
rescue TypeError
puts ""
end
puts "✍️ By: " + JSON.parse(story)["by"]
puts DateTime.strptime(JSON.parse(story)["time"].to_s, "%s").strftime("🕰 Published on %d/%m/%Y at %H:%M")
puts "=> comments.bliz?#{JSON.parse(story)['id']}" + " 📝 Comments (#{JSON.parse(story)['descendants']})"
puts ""
end
end

View File

@ -1,13 +0,0 @@
%%%
if [ $req_query = "" ]
gem_header 10 'text/gemini'
else
gem_header 20 'text/gemini'
end
%%%
# Greeting test
%%%
set name $req_query
echo "Hello and welcome to my test, $name"
%%%

View File

@ -1 +0,0 @@
puts "# This is a heading"