Add subCountText and add XHR alternative for subscribing to channels

This commit is contained in:
Omar Roth 2018-10-12 21:17:37 -05:00
parent 81b447782a
commit 1a39faee75
7 changed files with 144 additions and 24 deletions

View file

@ -22,6 +22,10 @@ div {
padding-right: 10px; padding-right: 10px;
} }
.pure-button-primary {
background: rgba(0, 182, 240, 1);
}
/* /*
* Navbar * Navbar
*/ */

View file

@ -22,6 +22,7 @@ CREATE TABLE public.videos
genre text COLLATE pg_catalog."default", genre text COLLATE pg_catalog."default",
genre_url text COLLATE pg_catalog."default", genre_url text COLLATE pg_catalog."default",
license text COLLATE pg_catalog."default", license text COLLATE pg_catalog."default",
sub_count_text text COLLATE pg_catalog."default",
CONSTRAINT videos_pkey PRIMARY KEY (id) CONSTRAINT videos_pkey PRIMARY KEY (id)
) )
WITH ( WITH (

View file

@ -1739,7 +1739,7 @@ get "/channel/:ucid" do |env|
page ||= 1 page ||= 1
begin begin
author, ucid, auto_generated = get_about_info(ucid) author, ucid, auto_generated, sub_count = get_about_info(ucid)
rescue ex rescue ex
error_message = "User does not exist" error_message = "User does not exist"
next templated "error" next templated "error"
@ -2298,6 +2298,7 @@ get "/api/v1/videos/:id" do |env|
json.field "author", video.author json.field "author", video.author
json.field "authorId", video.ucid json.field "authorId", video.ucid
json.field "authorUrl", "/channel/#{video.ucid}" json.field "authorUrl", "/channel/#{video.ucid}"
json.field "subCountText", video.sub_count_text
json.field "lengthSeconds", video.info["length_seconds"].to_i json.field "lengthSeconds", video.info["length_seconds"].to_i
if video.info["allow_ratings"]? if video.info["allow_ratings"]?

View file

@ -196,6 +196,12 @@ def get_about_info(ucid)
raise "User does not exist." raise "User does not exist."
end end
sub_count = about.xpath_node(%q(//span[contains(text(), "subscribers")]))
if sub_count
sub_count = sub_count.content.delete(", subscribers").to_i?
end
sub_count ||= 0
author = about.xpath_node(%q(//span[@class="qualified-channel-title-text"]/a)).not_nil!.content author = about.xpath_node(%q(//span[@class="qualified-channel-title-text"]/a)).not_nil!.content
ucid = about.xpath_node(%q(//link[@rel="canonical"])).not_nil!["href"].split("/")[-1] ucid = about.xpath_node(%q(//link[@rel="canonical"])).not_nil!["href"].split("/")[-1]
@ -207,5 +213,5 @@ def get_about_info(ucid)
auto_generated = true auto_generated = true
end end
return {author, ucid, auto_generated} return {author, ucid, auto_generated, sub_count}
end end

View file

@ -456,9 +456,10 @@ class Video
is_family_friendly: Bool, is_family_friendly: Bool,
genre: String, genre: String,
genre_url: String, genre_url: String,
license: { license: String,
sub_count_text: {
type: String, type: String,
default: "", default: "0",
}, },
}) })
end end
@ -490,11 +491,14 @@ def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32})
video = fetch_video(id, proxies) video = fetch_video(id, proxies)
video_array = video.to_a video_array = video.to_a
# Migration point
video_array = video_array[0..-2]
args = arg_array(video_array[1..-1], 2) args = arg_array(video_array[1..-1], 2)
db.exec("UPDATE videos SET (info,updated,title,views,likes,dislikes,wilson_score,\ db.exec("UPDATE videos SET (info,updated,title,views,likes,dislikes,wilson_score,\
published,description,language,author,ucid, allowed_regions, is_family_friendly,\ published,description,language,author,ucid,allowed_regions,is_family_friendly,\
genre, genre_url, license)\ genre,genre_url,license)\
= (#{args}) WHERE id = $1", video_array) = (#{args}) WHERE id = $1", video_array)
rescue ex rescue ex
db.exec("DELETE FROM videos * WHERE id = $1", id) db.exec("DELETE FROM videos * WHERE id = $1", id)
@ -505,6 +509,9 @@ def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32})
video = fetch_video(id, proxies) video = fetch_video(id, proxies)
video_array = video.to_a video_array = video.to_a
# Migration point
video_array = video_array[0..-2]
args = arg_array(video_array) args = arg_array(video_array)
db.exec("INSERT INTO videos VALUES (#{args}) ON CONFLICT (id) DO NOTHING", video_array) db.exec("INSERT INTO videos VALUES (#{args}) ON CONFLICT (id) DO NOTHING", video_array)
@ -662,11 +669,18 @@ def fetch_video(id, proxies)
if license if license
license = license.content license = license.content
else else
license ||= "" license = ""
end
sub_count_text = html.xpath_node(%q(//span[contains(@class, "yt-subscriber-count")]))
if sub_count_text
sub_count_text = sub_count_text["title"]
else
sub_count_text = "0"
end end
video = Video.new(id, info, Time.now, title, views, likes, dislikes, wilson_score, published, description, video = Video.new(id, info, Time.now, title, views, likes, dislikes, wilson_score, published, description,
nil, author, ucid, allowed_regions, is_family_friendly, genre, genre_url, license) nil, author, ucid, allowed_regions, is_family_friendly, genre, genre_url, license, sub_count_text)
return video return video
end end

View file

@ -13,23 +13,32 @@
</div> </div>
</div> </div>
<p class="h-box"> <div class="h-box">
<% if user %> <% if user %>
<% if subscriptions.includes? ucid %> <% if subscriptions.includes? ucid %>
<a href="/subscription_ajax?action_remove_subscriptions=1&c=<%= ucid %>&referer=<%= env.get("current_page") %>"> <p>
<b>Unsubscribe from <%= author %></b> <a id="subscribe" onclick="unsubscribe()" class="pure-button pure-button-primary"
</a> href="/subscription_ajax?action_remove_subscriptions=1&c=<%= ucid %>&referer=<%= env.get("current_page") %>">
<b>Unsubscribe from <%= author %> <%= number_with_separator(sub_count) %></b>
</a>
</p>
<% else %> <% else %>
<a href="/subscription_ajax?action_create_subscription_to_channel=1&c=<%= ucid %>&referer=<%= env.get("current_page") %>"> <p>
<b>Subscribe to <%= author %></b> <a id="subscribe" onclick="subscribe()" class="pure-button pure-button-primary"
</a> href="/subscription_ajax?action_create_subscription_to_channel=1&c=<%= ucid %>&referer=<%= env.get("current_page") %>">
<b>Subscribe to <%= author %> <%= number_with_separator(sub_count) %></b>
</a>
</p>
<% end %> <% end %>
<% else %> <% else %>
<a href="/login?referer=<%= env.get("current_page") %>"> <p>
<b>Login to subscribe to <%= author %></b> <a id="subscribe" class="pure-button pure-button-primary"
</a> href="/login?referer=<%= env.get("current_page") %>">
<b>Login to subscribe to <%= author %></b>
</a>
</p>
<% end %> <% end %>
</p> </div>
<p class="h-box"> <p class="h-box">
<a href="https://www.youtube.com/channel/<%= ucid %>">View channel on YouTube</a> <a href="https://www.youtube.com/channel/<%= ucid %>">View channel on YouTube</a>
@ -56,3 +65,45 @@
<% end %> <% end %>
</div> </div>
</div> </div>
<script>
document.getElementById("subscribe")["href"] = "javascript:void(0);"
function subscribe() {
var url = "/subscription_ajax?action_create_subscription_to_channel=1&c=<%= ucid %>&referer=<%= env.get("current_page") %>";
var xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.timeout = 20000;
xhr.open("GET", url, true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
subscribe_button = document.getElementById("subscribe");
subscribe_button.onclick = unsubscribe;
subscribe_button.innerHTML = '<b>Unsubscribe from <%= author %> <%= number_with_separator(sub_count + 1) %></b>'
}
}
}
}
function unsubscribe() {
var url = "/subscription_ajax?action_remove_subscriptions=1&c=<%= ucid %>&referer=<%= env.get("current_page") %>";
var xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.timeout = 20000;
xhr.open("GET", url, true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
subscribe_button = document.getElementById("subscribe");
subscribe_button.onclick = subscribe;
subscribe_button.innerHTML = '<b>Subscribe to <%= author %> <%= number_with_separator(sub_count) %></b>'
}
}
}
}
</script>

View file

@ -92,20 +92,23 @@
<% if user %> <% if user %>
<% if subscriptions.includes? video.ucid %> <% if subscriptions.includes? video.ucid %>
<p> <p>
<a href="/subscription_ajax?action_remove_subscriptions=1&c=<%= video.ucid %>&referer=<%= env.get("current_page") %>"> <a id="subscribe" onclick="unsubscribe()" class="pure-button pure-button-primary"
<b>Unsubscribe from <%= video.author %></b> href="/subscription_ajax?action_remove_subscriptions=1&c=<%= video.ucid %>&referer=<%= env.get("current_page") %>">
<b>Unsubscribe from <%= video.author %> <%= video.sub_count_text %></b>
</a> </a>
</p> </p>
<% else %> <% else %>
<p> <p>
<a href="/subscription_ajax?action_create_subscription_to_channel=1&c=<%= video.ucid %>&referer=<%= env.get("current_page") %>"> <a id="subscribe" onclick="subscribe()" class="pure-button pure-button-primary"
<b>Subscribe to <%= video.author %></b> href="/subscription_ajax?action_create_subscription_to_channel=1&c=<%= video.ucid %>&referer=<%= env.get("current_page") %>">
<b>Subscribe to <%= video.author %> <%= video.sub_count_text %></b>
</a> </a>
</p> </p>
<% end %> <% end %>
<% else %> <% else %>
<p> <p>
<a href="/login?referer=<%= env.get("current_page") %>"> <a id="subscribe" class="pure-button pure-button-primary"
href="/login?referer=<%= env.get("current_page") %>">
<b>Login to subscribe to <%= video.author %></b> <b>Login to subscribe to <%= video.author %></b>
</a> </a>
</p> </p>
@ -152,6 +155,46 @@
</div> </div>
<script> <script>
document.getElementById("subscribe")["href"] = "javascript:void(0);"
function subscribe() {
var url = "/subscription_ajax?action_create_subscription_to_channel=1&c=<%= video.ucid %>&referer=<%= env.get("current_page") %>";
var xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.timeout = 20000;
xhr.open("GET", url, true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
subscribe_button = document.getElementById("subscribe");
subscribe_button.onclick = unsubscribe;
subscribe_button.innerHTML = '<b>Unsubscribe from <%= video.author %> <%= video.sub_count_text %></b>'
}
}
}
}
function unsubscribe() {
var url = "/subscription_ajax?action_remove_subscriptions=1&c=<%= video.ucid %>&referer=<%= env.get("current_page") %>";
var xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.timeout = 20000;
xhr.open("GET", url, true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
subscribe_button = document.getElementById("subscribe");
subscribe_button.onclick = subscribe;
subscribe_button.innerHTML = '<b>Subscribe to <%= video.author %> <%= video.sub_count_text %></b>'
}
}
}
}
<% if plid %> <% if plid %>
function get_playlist() { function get_playlist() {
var plid = "<%= plid %>" var plid = "<%= plid %>"