|
1 | 1 | require 'rubygems'
|
2 | 2 | require 'bundler'
|
| 3 | +require 'sequel' |
3 | 4 | Bundler.require
|
4 | 5 |
|
5 | 6 | # Database setup
|
|
32 | 33 | # get the max id from the database to pass to our search query
|
33 | 34 | since_id = tweets.max(:twitter_id)
|
34 | 35 | page = 1
|
| 36 | + page_max = 20 |
| 37 | + curses = %w{shit fuck cunt arse arsehole prick bastard fucking} |
35 | 38 |
|
36 | 39 | # load all the new tweets into the DB
|
37 | 40 | while true do
|
38 |
| - @search = Twitter::Search.new('"tony abbott" shit OR fuck OR cunt OR arse OR arsehole OR prick OR bastard OR fucking') |
| 41 | + item_count = 0 |
39 | 42 | # 20 per page - twitter docs say 100, but seems to be less, so we
|
40 | 43 | # cover our bases for pagination. this pagination method also leaves
|
41 | 44 | # a small possibility of duplicates, but it's not a big deal.
|
42 |
| - @search.per_page(20) |
43 |
| - @search.page(page) |
44 |
| - @search.since(since_id) |
45 |
| - item_count = 0 |
| 45 | + options = {count: page_max, result_type: 'recent', since_id: since_id} |
46 | 46 |
|
47 |
| - @search.each do |item| |
48 |
| - tweets.insert(:twitter_id => item["id"].to_i, :content => item["text"]) |
| 47 | + twitter.search(%Q{"tony abbott" #{curses.join(" OR ")} -rt}, options).take(20).each do |item| |
| 48 | + tweets.insert(:twitter_id => item.id, :content => item.text) |
49 | 49 | item_count = item_count + 1
|
50 | 50 | end
|
51 | 51 | # if we don't have more than 20 items, we can exit
|
52 |
| - break if item_count < 20 |
| 52 | + break if item_count < page_max |
53 | 53 | page = page + 1
|
54 | 54 | end
|
55 | 55 |
|
56 | 56 | @results = []
|
57 | 57 |
|
58 |
| - @all_results = tweets.order(:twitter_id.desc).each do |item| |
| 58 | + @all_results = tweets.order(:twitter_id).reverse.each do |item| |
59 | 59 | # ignore items in the blacklist file or that start with rt/RT (retweets)
|
60 | 60 | unless BLACKLISTED_STRINGS.any? {|i| item[:content].downcase.match(i.downcase)} || item[:content].downcase.match(/^rt/)
|
61 | 61 | @results << '<p id="' + item[:id].to_s + '">' +
|
|
76 | 76 | haml :index, :options => {:format => :html4, :attr_wrapper => '"'}
|
77 | 77 | end
|
78 | 78 |
|
79 |
| - |
80 | 79 | # Configure Block.
|
81 | 80 | configure do
|
82 | 81 | BLACKLISTED_STRINGS = []
|
|
87 | 86 | end
|
88 | 87 | end
|
89 | 88 | end
|
| 89 | + |
| 90 | +def twitter |
| 91 | + @twitter ||= Twitter::REST::Client.new do |config| |
| 92 | + config.consumer_key = ENV['TWITTER_CONSUMER_KEY'] |
| 93 | + config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET'] |
| 94 | + config.access_token = ENV['TWITTER_ACCESS_TOKEN'] |
| 95 | + config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET'] |
| 96 | + end |
| 97 | + |
| 98 | + @twitter |
| 99 | +end |
0 commit comments