diff options
| author | Omar Roth <omarroth@hotmail.com> | 2018-03-24 22:38:35 -0500 |
|---|---|---|
| committer | Omar Roth <omarroth@hotmail.com> | 2018-03-24 22:38:35 -0500 |
| commit | 076eaa7635cc320518a2e96775ff50e3712a7e77 (patch) | |
| tree | 50e0881ba9e33d5674e94194b518935f5d52322c /src/helpers.cr | |
| parent | 0ed3e5d54703b73739083c5804a4304e3e24bc32 (diff) | |
| download | invidious-076eaa7635cc320518a2e96775ff50e3712a7e77.tar.gz invidious-076eaa7635cc320518a2e96775ff50e3712a7e77.tar.bz2 invidious-076eaa7635cc320518a2e96775ff50e3712a7e77.zip | |
Add subscriptions
Diffstat (limited to 'src/helpers.cr')
| -rw-r--r-- | src/helpers.cr | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/helpers.cr b/src/helpers.cr index a8de6b0a..cd11baab 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -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 |
