Add subscriptions

This commit is contained in:
Omar Roth
2018-03-24 22:38:35 -05:00
parent 0ed3e5d547
commit 076eaa7635
4 changed files with 161 additions and 5 deletions

View File

@@ -54,6 +54,26 @@ class Video
})
end
class InvidiousChannel
module XMLConverter
def self.from_rs(rs)
XML.parse_html(rs.read(String))
end
end
add_mapping({
id: String,
rss: {
type: XML::Node,
default: XML.parse_html(""),
converter: InvidiousChannel::XMLConverter,
},
updated: Time,
author: String,
})
end
class RedditSubmit
JSON.mapping({
data: RedditSubmitData,
@@ -464,3 +484,33 @@ def login_req(login_form, f_req)
return HTTP::Params.encode(data)
end
def get_channel(id, client, db)
if db.query_one?("SELECT EXISTS (SELECT true FROM channels WHERE id = $1)", id, as: Bool)
channel = db.query_one("SELECT * FROM channels WHERE id = $1", id, as: InvidiousChannel)
if Time.now - channel.updated > 1.hours
db.exec("DELETE FROM channels * WHERE id = $1", id)
channel = fetch_channel(id, client)
args = arg_array(channel.to_a)
db.exec("INSERT INTO channels VALUES (#{args})", channel.to_a)
end
else
channel = fetch_channel(id, client)
args = arg_array(channel.to_a)
db.exec("INSERT INTO channels VALUES (#{args})", channel.to_a)
end
return channel
end
def fetch_channel(id, client)
rss = client.get("/feeds/videos.xml?channel_id=#{id}").body
rss = XML.parse_html(rss)
author = rss.xpath_node("//feed/author/name").not_nil!.content
channel = InvidiousChannel.new(id, rss, Time.now, author)
return channel
end