|
@@ -4,8 +4,9 @@ require 'sequel'
|
4
|
4
|
require_relative '../db/connect'
|
5
|
5
|
require_relative '../lib/auth'
|
6
|
6
|
require_relative '../lib/fetch'
|
7
|
|
-require_relative '../lib/insert'
|
8
|
7
|
require_relative '../lib/parse'
|
|
8
|
+require_relative '../lib/models/post'
|
|
9
|
+require_relative '../lib/models/thread'
|
9
|
10
|
|
10
|
11
|
class Scraper
|
11
|
12
|
def initialize(first: 0, last: 0, log: false)
|
|
@@ -13,7 +14,6 @@ class Scraper
|
13
|
14
|
@last = last
|
14
|
15
|
@log = log
|
15
|
16
|
@cookie = login(ENV['VLV_USERNAME'], ENV['VLV_PASSWORD'])
|
16
|
|
- @db = connect
|
17
|
17
|
end
|
18
|
18
|
|
19
|
19
|
def scrape
|
|
@@ -41,9 +41,9 @@ class Scraper
|
41
|
41
|
|
42
|
42
|
t[:created_at] = Parse.thread_created_at(first_post)
|
43
|
43
|
|
44
|
|
- thread = @db.from(:threads).first(remote_id: t[:remote_id])
|
|
44
|
+ thread = DB.from(:threads).first(remote_id: t[:remote_id])
|
45
|
45
|
if thread.nil?
|
46
|
|
- thread = Insert.thread(t, @db)
|
|
46
|
+ thread = VLV::Thread.create(t.delete_if { |k| k == :is_sticky })
|
47
|
47
|
log ' Inserting thread'
|
48
|
48
|
end
|
49
|
49
|
|
|
@@ -53,7 +53,7 @@ class Scraper
|
53
|
53
|
def scrape_posts(thread, page)
|
54
|
54
|
posts = Parse.posts(thread, page)
|
55
|
55
|
last_post = posts.last
|
56
|
|
- unless @db.from(:posts).first(remote_id: last_post[:remote_id]).nil?
|
|
56
|
+ unless DB.from(:posts).first(remote_id: last_post[:remote_id]).nil?
|
57
|
57
|
log ' No new posts'
|
58
|
58
|
return true
|
59
|
59
|
end
|
|
@@ -62,8 +62,8 @@ class Scraper
|
62
|
62
|
posts.each_with_index do |p, index|
|
63
|
63
|
msg = " Inserting post #{index + 1}/#{posts_count}"
|
64
|
64
|
print msg if @log
|
65
|
|
- if @db.from(:posts).first(remote_id: p[:remote_id]).nil?
|
66
|
|
- Insert.post(p, @db)
|
|
65
|
+ if DB.from(:posts).first(remote_id: p[:remote_id]).nil?
|
|
66
|
+ VLV::Post.create(p)
|
67
|
67
|
end
|
68
|
68
|
print "\b" * msg.size unless index == posts_count - 1 if @log
|
69
|
69
|
end
|