Add published field

This commit is contained in:
Omar Roth 2018-02-02 21:44:10 -06:00
parent 640508669a
commit 26c9a87e88
2 changed files with 24 additions and 5 deletions

View File

@ -11,7 +11,7 @@ class Video
end end
end end
def initialize(id, info, html, updated, title, views, likes, dislikes, wilson_score) def initialize(id, info, html, updated, title, views, likes, dislikes, wilson_score, published)
@id = id @id = id
@info = info @info = info
@html = html @html = html
@ -21,10 +21,11 @@ class Video
@likes = likes @likes = likes
@dislikes = dislikes @dislikes = dislikes
@wilson_score = wilson_score @wilson_score = wilson_score
@published = published
end end
def to_a def to_a
return [@id, @info, @html, @updated, @title, @views, @likes, @dislikes, @wilson_score] return [@id, @info, @html, @updated, @title, @views, @likes, @dislikes, @wilson_score, @published]
end end
DB.mapping({ DB.mapping({
@ -45,6 +46,7 @@ class Video
likes: Int32, likes: Int32,
dislikes: Int32, dislikes: Int32,
wilson_score: Float64, wilson_score: Float64,
published: Time,
}) })
end end
@ -101,7 +103,23 @@ def fetch_video(id, client)
wilson_score = ci_lower_bound(likes, likes + dislikes) wilson_score = ci_lower_bound(likes, likes + dislikes)
video = Video.new(id, info, html, Time.now, title, views, likes, dislikes, wilson_score) published = html.xpath_node(%q(//strong[@class="watch-time-text"]))
if published
published = published.content
published = published.lchop("Published on ")
published = published.lchop("Streamed live on ")
published = published.lchop("Started streaming on ")
if !published.includes?("ago")
published = Time.parse(published, "%b %-d, %Y")
else
# Time matches format "20 hours ago", "40 minutes ago"...
published = Time.now.date
end
else
published = Time.epoch(0)
end
video = Video.new(id, info, html, Time.now, title, views, likes, dislikes, wilson_score, published)
return video return video
end end
@ -114,11 +132,11 @@ def get_video(id, client, db, refresh = true)
if refresh && Time.now - video.updated > 1.hours if refresh && Time.now - video.updated > 1.hours
video = fetch_video(id, client) video = fetch_video(id, client)
db.exec("UPDATE videos SET info = $2, html = $3, updated = $4,\ db.exec("UPDATE videos SET info = $2, html = $3, updated = $4,\
title = $5, views = $6, likes = $7, dislikes = $8, wilson_score = $9 WHERE id = $1", video.to_a) title = $5, views = $6, likes = $7, dislikes = $8, wilson_score = $9, published = $10 WHERE id = $1", video.to_a)
end end
else else
video = fetch_video(id, client) video = fetch_video(id, client)
db.exec("INSERT INTO videos VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", video.to_a) db.exec("INSERT INTO videos VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", video.to_a)
end end
return video return video

View File

@ -11,6 +11,7 @@ CREATE TABLE public.videos
likes integer, likes integer,
dislikes integer, dislikes integer,
wilson_score double precision, wilson_score double precision,
published timestamp with time zone,
CONSTRAINT videos_pkey PRIMARY KEY (id) CONSTRAINT videos_pkey PRIMARY KEY (id)
) )
WITH ( WITH (