Просмотр исходного кода

Use DB constant and sequel models

master
Dylan Baker 4 лет назад
Родитель
Сommit
d4cff431b9
5 измененных файлов: 19 добавлений и 31 удалений
  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 Просмотреть файл

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

+ 7
- 7
db/scrape.rb Просмотреть файл

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

+ 0
- 21
lib/insert.rb Просмотреть файл

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 Просмотреть файл

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

+ 5
- 0
lib/models/thread.rb Просмотреть файл

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

Загрузка…
Отмена
Сохранить