Browse Source

Use DB constant and sequel models

master
Dylan Baker 4 years ago
parent
commit
d4cff431b9
5 changed files with 19 additions and 31 deletions
  1. 2
    3
      db/connect.rb
  2. 7
    7
      db/scrape.rb
  3. 0
    21
      lib/insert.rb
  4. 5
    0
      lib/models/post.rb
  5. 5
    0
      lib/models/thread.rb

+ 2
- 3
db/connect.rb View File

@@ -2,11 +2,10 @@ require 'dotenv'
2 2
 
3 3
 Dotenv.load(File.expand_path('../.env'))
4 4
 
5
-def connect
5
+DB =
6 6
   Sequel.connect(
7 7
     adapter: :postgres,
8 8
     database: ENV['DB_DATABASE'],
9 9
     user: ENV['DB_USERNAME'],
10
-    password: ENV['DB_PASSWORD'],
10
+    password: ENV['DB_PASSWORD']
11 11
   )
12
-end

+ 7
- 7
db/scrape.rb View File

@@ -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

+ 0
- 21
lib/insert.rb View File

@@ -1,21 +0,0 @@
1
-module Insert
2
-  def self.post(post, db)
3
-    db.from(:posts).insert(
4
-      body: post[:body],
5
-      created_at: post[:created_at],
6
-      creator: post[:creator],
7
-      thread_id: post[:thread_id],
8
-      remote_id: post[:remote_id],
9
-    )
10
-  end
11
-
12
-  def self.thread(thread, db)
13
-    id = db.from(:threads).insert(
14
-      title: thread[:title],
15
-      creator: thread[:creator],
16
-      remote_id: thread[:remote_id],
17
-      created_at: thread[:created_at]
18
-    )
19
-    thread.merge(id: id)
20
-  end
21
-end

+ 5
- 0
lib/models/post.rb View File

@@ -0,0 +1,5 @@
1
+module VLV
2
+  class Post < Sequel::Model
3
+    many_to_one :thread
4
+  end
5
+end

+ 5
- 0
lib/models/thread.rb View File

@@ -0,0 +1,5 @@
1
+module VLV
2
+  class Thread < Sequel::Model
3
+    one_to_many :posts
4
+  end
5
+end

Loading…
Cancel
Save