first commit
commit
27dcddf442
|
@ -0,0 +1,7 @@
|
|||
%%%
|
||||
gem_header 20 'text/gemini'
|
||||
%%%
|
||||
|
||||
%%%
|
||||
ruby serve/services/hackernews/comments.rb $req_query
|
||||
%%%
|
|
@ -0,0 +1,70 @@
|
|||
require 'uri'
|
||||
require 'net/http'
|
||||
require 'json'
|
||||
require 'date'
|
||||
require 'nokogiri'
|
||||
require 'reverse_markdown'
|
||||
require_relative 'funcs.rb'
|
||||
|
||||
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']} (⇧#{JSON.parse(json)['score']})\n"
|
||||
|
||||
puts "=> #{JSON.parse(json)['url']} 🌍 Article Link"
|
||||
puts "=> users.bliz?#{JSON.parse(json)['by']} 📡 By #{JSON.parse(json)['by']}"
|
||||
puts ""
|
||||
|
||||
return json
|
||||
end
|
||||
|
||||
def get_comment_ids(num, article)
|
||||
counter = 0
|
||||
comment_ids = Array.new
|
||||
|
||||
begin
|
||||
JSON.parse(article)["kids"].each do |comment|
|
||||
comment_ids << comment
|
||||
counter += 1
|
||||
break if counter == num
|
||||
end
|
||||
rescue NoMethodError
|
||||
comment_ids = "empty"
|
||||
end
|
||||
|
||||
return comment_ids
|
||||
end
|
||||
|
||||
def display_comments(num, article_id)
|
||||
json = Array.new
|
||||
|
||||
begin
|
||||
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
|
||||
rescue NoMethodError
|
||||
puts "There do not appear to be any comments here"
|
||||
end
|
||||
|
||||
json.each do |comment|
|
||||
puts "## Comment by " + JSON.parse(comment)["by"]
|
||||
puts "=> users.bliz?#{JSON.parse(comment)['by']}" + " 📡 Display User Page"
|
||||
|
||||
puts comment.to_gemini("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])
|
|
@ -0,0 +1,28 @@
|
|||
class String
|
||||
def numeric?
|
||||
!self.match(/[^0-9]/)
|
||||
end
|
||||
|
||||
def to_gemini(json_class)
|
||||
html = Nokogiri::HTML(JSON.parse(self)["#{json_class}"]).serialize
|
||||
markdown = ReverseMarkdown.convert(html)
|
||||
parsed = String.new
|
||||
|
||||
markdown.each_line do |line|
|
||||
if line =~ /^\\/ then
|
||||
parsed << line.slice!(0)
|
||||
else
|
||||
line.gsub!(/\[([^\]]+)\]\(([^)]+)\)/, '\1')
|
||||
if line =~ /^https?:/ then
|
||||
parsed << "=> #{line}"
|
||||
else
|
||||
parsed << line
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return parsed
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
require_relative 'funcs.rb'
|
||||
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
|
|
@ -0,0 +1,7 @@
|
|||
%%%
|
||||
gem_header 20 'text/gemini'
|
||||
%%%
|
||||
|
||||
%%%
|
||||
ruby serve/services/hackernews/hackernews.rb 20
|
||||
%%%
|
|
@ -0,0 +1,61 @@
|
|||
require 'uri'
|
||||
require 'net/http'
|
||||
require 'json'
|
||||
require 'date'
|
||||
|
||||
puts "=> /index.bliz 🏠Back Home"
|
||||
|
||||
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, reading of their comments and viewing a user’s page. Click a username below to be taken to their userpage."
|
||||
puts ""
|
||||
|
||||
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 "=> users.bliz?#{JSON.parse(story)['by']}" + " ✍️ " + JSON.parse(story)["by"]
|
||||
puts "=> comments.bliz?#{JSON.parse(story)['id']}" + " 📝 Comments (#{JSON.parse(story)['descendants']})"
|
||||
puts DateTime.strptime(JSON.parse(story)["time"].to_s, "%s").strftime("🕰 Published on %d/%m/%Y at %H:%M")
|
||||
|
||||
puts ""
|
||||
end
|
||||
end
|
|
@ -0,0 +1,7 @@
|
|||
%%%
|
||||
gem_header 20 'text/gemini'
|
||||
%%%
|
||||
|
||||
%%%
|
||||
ruby serve/services/hackernews/users.rb $req_query
|
||||
%%%
|
|
@ -0,0 +1,72 @@
|
|||
require 'uri'
|
||||
require 'net/http'
|
||||
require 'json'
|
||||
require 'date'
|
||||
require 'nokogiri'
|
||||
require 'reverse_markdown'
|
||||
require_relative 'funcs.rb'
|
||||
|
||||
puts "=> index.bliz 📰 Back To Overview"
|
||||
|
||||
def get_user(id)
|
||||
json = String.new
|
||||
|
||||
uri = URI("https://hacker-news.firebaseio.com/v0/user/#{id}.json?print=pretty")
|
||||
res = Net::HTTP.get_response(uri)
|
||||
|
||||
json << res.body if res.is_a?(Net::HTTPSuccess)
|
||||
return json
|
||||
end
|
||||
|
||||
def get_user_posts(num, user)
|
||||
counter = 0
|
||||
json = Array.new
|
||||
|
||||
JSON.parse(user)["submitted"].each do |post|
|
||||
json << post
|
||||
|
||||
counter += 1
|
||||
break if counter == num
|
||||
end
|
||||
|
||||
return json
|
||||
end
|
||||
|
||||
def display_user_posts(num, user)
|
||||
get_user_posts(num, user).each do |post|
|
||||
uri = URI("https://hacker-news.firebaseio.com/v0/item/#{post}.json?print=pretty)")
|
||||
res = Net::HTTP.get_response(uri)
|
||||
user_content = res.body
|
||||
content_type = JSON.parse(user_content)["type"]
|
||||
|
||||
if content_type == "story" then
|
||||
puts DateTime.strptime(JSON.parse(user_content)["time"].to_s, "%s").strftime("### Post from %d/%m/%Y at %H:%M")
|
||||
puts "=> comments.bliz?#{post}" + " 📜 #{JSON.parse(user_content)['title']}"
|
||||
puts ""
|
||||
|
||||
elsif content_type == "comment" && JSON.parse(user_content)["text"] != nil then
|
||||
puts DateTime.strptime(JSON.parse(user_content)["time"].to_s, "%s").strftime("### Comment from %d/%m/%Y at %H:%M")
|
||||
puts user_content.to_gemini("text")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
def display_user(id, num_posts)
|
||||
user = get_user(id)
|
||||
|
||||
puts "# Viewing user: #{JSON.parse(user)['id']}" + " (⇧#{JSON.parse(user)['karma']})"
|
||||
puts DateTime.strptime(JSON.parse(user)["created"].to_s, "%s").strftime("This user account was created on %d/%m/%Y at %H:%M")
|
||||
|
||||
puts "## About"
|
||||
unless JSON.parse(user)["about"] == nil then
|
||||
puts user.to_gemini("about")
|
||||
else
|
||||
puts "#{id} does not appear to have anything in their about section."
|
||||
end
|
||||
|
||||
puts "## #{num_posts} Latest Posts and Comments"
|
||||
display_user_posts(num_posts, user)
|
||||
end
|
||||
|
||||
display_user(ARGV[0], 15)
|
Loading…
Reference in New Issue