diff options
| author | Omar Roth <omarroth@hotmail.com> | 2018-03-29 21:41:05 -0500 |
|---|---|---|
| committer | Omar Roth <omarroth@hotmail.com> | 2018-03-29 21:41:05 -0500 |
| commit | 6c4cfbe39dc90c2014ae0755fe1a2bfaa255dbe6 (patch) | |
| tree | 77247323e793eed364769595334ead760516f660 /src | |
| parent | 4a7fc4ea697b238098145cad20b21dee7c04bbd3 (diff) | |
| download | invidious-6c4cfbe39dc90c2014ae0755fe1a2bfaa255dbe6.tar.gz invidious-6c4cfbe39dc90c2014ae0755fe1a2bfaa255dbe6.tar.bz2 invidious-6c4cfbe39dc90c2014ae0755fe1a2bfaa255dbe6.zip | |
Add users table
Diffstat (limited to 'src')
| -rw-r--r-- | src/helpers.cr | 46 | ||||
| -rw-r--r-- | src/invidious.cr | 20 |
2 files changed, 51 insertions, 15 deletions
diff --git a/src/helpers.cr b/src/helpers.cr index 33035d60..2e2d9de4 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -79,6 +79,15 @@ class ChannelVideo }) end +class User + add_mapping({ + id: String, + updated: Time, + notifications: Int32, + subscriptions: Array(String), + }) +end + class RedditSubmit JSON.mapping({ data: RedditSubmitData, @@ -545,3 +554,40 @@ def fetch_channel(id, client, db) return channel end + +def get_user(sid, client, headers, db) + if db.query_one?("SELECT EXISTS (SELECT true FROM users WHERE id = $1)", sid, as: Bool) + user = db.query_one("SELECT * FROM users WHERE id = $1", sid, as: User) + + if Time.now - user.updated > 1.minutes + user = fetch_user(sid, client, headers) + user_array = user.to_a + args = arg_array(user_array) + + db.exec("INSERT INTO users VALUES (#{args}) \ + ON CONFLICT (id) DO UPDATE SET updated = $2, subscriptions = $4", user_array) + end + else + user = fetch_user(sid, client, headers) + args = arg_array(user.to_a) + db.exec("INSERT INTO users VALUES (#{args})", user.to_a) + end + + return user +end + +def fetch_user(sid, client, headers) + feed = client.get("/subscription_manager?action_takeout=1", headers).body + + channels = [] of String + feed = XML.parse_html(feed) + feed.xpath_nodes("//opml/outline/outline").each do |channel| + id = channel["xmlurl"][-24..-1] + get_channel(id, client, PG_DB) + + channels << id + end + + user = User.new(sid, Time.now, 0, channels) + return user +end diff --git a/src/invidious.cr b/src/invidious.cr index 82a40ed1..c91b75f8 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -567,29 +567,19 @@ get "/feed/subscriptions" do |env| page = env.params.query["page"]?.try &.to_i page ||= 1 - client = get_client(youtube_pool) - headers = HTTP::Headers.new headers["Cookie"] = env.request.headers["Cookie"] - feed = client.get("/subscription_manager?action_takeout=1", headers).body - - channels = [] of String + sid = env.request.cookies["SID"].value - feed = XML.parse_html(feed) - feed.xpath_nodes("//opml/outline/outline").each do |channel| - id = channel["xmlurl"][-24..-1] - get_channel(id, client, PG_DB) - - channels << id - end + client = get_client(youtube_pool) + user = get_user(sid, client, headers, PG_DB) youtube_pool << client - time = Time.now - args = arg_array(channels) + args = arg_array(user.subscriptions) offset = (page - 1) * max_results videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{args})\ - ORDER BY published DESC LIMIT #{max_results} OFFSET #{offset}", channels, as: ChannelVideo) + ORDER BY published DESC LIMIT #{max_results} OFFSET #{offset}", user.subscriptions, as: ChannelVideo) templated "subscriptions" else |
