Permalink
Browse files

support per-space comments

This should make temperatures more accurate.
  • Loading branch information...
shyouhei committed Oct 3, 2017
1 parent e277e50 commit 4de3c03070b40438509c8b1fca9414cee0d2e9be
Showing with 31 additions and 7 deletions.
  1. +28 −1 app/models/comment.rb
  2. +3 −6 app/models/post.rb
View
@@ -3,11 +3,13 @@
class Comment < ApplicationRecord
belongs_to :post
def self.import space, json, parent
def self.import space, json, parent = nil
ctime = Time.iso8601 json['created_at']
mtime = Time.iso8601 json['updated_at']
obj = find_by namespace: space, number: json['id']
return false if obj&.updated_at == mtime
parent ||= find_post_using_url(json['url'])
raise "unable to find post: #{json['url']}" unless parent
obj ||= new
obj.update_attributes(
namespace: space,
@@ -20,6 +22,31 @@ def self.import space, json, parent
return true
end
def self.find_post_using_url url
return nil unless %r'esa.io/posts/(?<id>\d+)' =~ url
return Post.find_by(number: id.to_i)
end
def self.crawl force: false
space = Rails.application.secrets.esa_space
token = Rails.application.secrets.esa_token
esa = Esa::Client.new current_team: space, access_token: token
1.upto Float::INFINITY do |i|
comments = esa.comments nil, page: i, per_page: 100
raise comments.inspect unless comments.status == 200
flag = false
comments.body['comments'].each do |j|
f1, obj = import space, j
flag ||= f1
end
Rails.logger.info "done #{i*100}/#{comments.body['total_count']}"
return self if !flag && !force # bail out no new things beyond
sleep(15 * 60.0 / 75.0)
return self unless comments.body["next_page"]
end
end
def self.updated_at_per_post
group(:post_id).maximum(:updated_at)
end
View
@@ -32,16 +32,12 @@ def self.crawl force: false
esa = Esa::Client.new current_team: space, access_token: token
1.upto Float::INFINITY do |i|
posts = esa.posts page: i, per_page: 100, include: 'comments'
posts = esa.posts page: i, per_page: 100
raise posts.inspect unless posts.status == 200
flag = false
posts.body['posts'].each do |j|
f1, obj = import space, j
flag ||= f1
j['comments'].each do |k|
f2 = Comment.import space, k, obj
flag ||= f2
end
end
Rails.logger.info "done #{i*100}/#{posts.body['total_count']}"
return self if !flag && !force # bail out no new things beyond
@@ -51,7 +47,8 @@ def self.crawl force: false
end
def self.to_md
crawl
crawl # force: true
Comment.crawl # force: true
@@id2updated_at = Comment.updated_at_per_post
ret = StringIO.new
ret.printf "| 温度 | 記事 | \n"

0 comments on commit 4de3c03

Please sign in to comment.