From ffdf2ea4c57a8c4b0cf99b106eb71dbfa6b1695a Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 31 Mar 2018 10:30:17 -0500 Subject: [PATCH] Add email to user table --- config/sql/users.sql | 3 +++ src/helpers.cr | 38 ++++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/config/sql/users.sql b/config/sql/users.sql index e46cd744b..fc4adef51 100644 --- a/config/sql/users.sql +++ b/config/sql/users.sql @@ -8,6 +8,9 @@ CREATE TABLE public.users updated timestamp with time zone, notifications integer, subscriptions text[] COLLATE pg_catalog."default", + notifications_viewed timestamp with time zone, + email text COLLATE pg_catalog."default" NOT NULL, + CONSTRAINT users_email_key UNIQUE (email), CONSTRAINT users_id_key UNIQUE (id) ) WITH ( diff --git a/src/helpers.cr b/src/helpers.cr index c0928e52f..88095b02e 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -81,10 +81,12 @@ end class User add_mapping({ - id: String, - updated: Time, - notifications: Int32, - subscriptions: Array(String), + id: String, + updated: Time, + notifications: Int32, + subscriptions: Array(String), + notifications_viewed: Time, + email: String, }) end @@ -565,29 +567,41 @@ def get_user(sid, client, headers, db) args = arg_array(user_array) db.exec("INSERT INTO users VALUES (#{args}) \ - ON CONFLICT (id) DO UPDATE SET updated = $2, subscriptions = $4", user_array) + ON CONFLICT (email) DO UPDATE SET id = $1, updated = $2, subscriptions = $4", user_array) end else user = fetch_user(sid, client, headers) + user_array = user.to_a args = arg_array(user.to_a) - db.exec("INSERT INTO users VALUES (#{args})", user.to_a) + + db.exec("INSERT INTO users VALUES (#{args}) \ + ON CONFLICT (email) DO UPDATE SET id = $1, updated = $2, subscriptions = $4", user_array) end return user end def fetch_user(sid, client, headers) - feed = client.get("/subscription_manager?action_takeout=1", headers).body + feed = client.get("/subscription_manager?disable_polymer=1", headers).body + File.write("feed.html", feed) 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 + feed.xpath_nodes(%q(//a[@class="subscription-title yt-uix-sessionlink"]/@href)).each do |channel| + channel_id = channel.content.lstrip("/channel/").not_nil! + get_channel(channel_id, client, PG_DB) + + channels << channel_id end - user = User.new(sid, Time.now, 0, channels) + email = feed.xpath_node(%q(//a[@class="yt-masthead-picker-header yt-masthead-picker-active-account"])) + if email + email = email.content.lstrip.rstrip + else + email = "" + end + + user = User.new(sid, Time.now, 0, channels, Time.now, email) return user end