Class: Ebooks::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_ebooks/archive.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, path, client = nil) ⇒ Archive

Returns a new instance of Archive.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twitter_ebooks/archive.rb', line 44

def initialize(username, path, client=nil)
  @username = username
  @path = path || "#{username}.json"
  @client = client || make_client

  if File.exists?(@path)
    @tweets = JSON.parse(File.read(@path), symbolize_names: true)
    log "Currently #{@tweets.length} tweets for #{@username}"
  else
    @tweets.nil?
    log "New archive for @#{username} at #{@path}"
  end
end

Instance Attribute Details

#tweetsObject (readonly)

Returns the value of attribute tweets.



11
12
13
# File 'lib/twitter_ebooks/archive.rb', line 11

def tweets
  @tweets
end

Instance Method Details

#make_clientObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/twitter_ebooks/archive.rb', line 13

def make_client
  if File.exists?(CONFIG_PATH)
    @config = JSON.parse(File.read(CONFIG_PATH), symbolize_names: true)
  else
    @config = {}

    puts "As Twitter no longer allows anonymous API access, you'll need to enter the auth details of any account to use for archiving. These will be stored in #{CONFIG_PATH} if you need to change them later."
    print "Consumer key: "
    @config[:consumer_key] = STDIN.gets.chomp
    print "Consumer secret: "
    @config[:consumer_secret] = STDIN.gets.chomp
    print "Oauth token: "
    @config[:oauth_token] = STDIN.gets.chomp
    print "Oauth secret: "
    @config[:oauth_token_secret] = STDIN.gets.chomp

    File.open(CONFIG_PATH, 'w') do |f|
      f.write(JSON.pretty_generate(@config))
    end
  end

  Twitter.configure do |config|
    config.consumer_key = @config[:consumer_key]
    config.consumer_secret = @config[:consumer_secret]
    config.oauth_token = @config[:oauth_token]
    config.oauth_token_secret = @config[:oauth_token_secret]
  end

  Twitter::Client.new
end

#syncObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/twitter_ebooks/archive.rb', line 58

def sync
  retries = 0
  tweets = []
  max_id = nil

  opts = {
    count: 200,
    #include_rts: false,
    trim_user: true
  }

  opts[:since_id] = @tweets[0][:id] unless @tweets.nil?

  loop do
    opts[:max_id] = max_id unless max_id.nil?
    new = @client.user_timeline(@username, opts)
    break if new.length <= 1
    tweets += new
    puts "Received #{tweets.length} new tweets"
    max_id = new.last.id
  end

  if tweets.length == 0
    log "No new tweets"
  else
    @tweets ||= []
    @tweets = tweets.map(&:attrs).each { |tw|
      tw.delete(:entities)
    } + @tweets
    File.open(@path, 'w') do |f|
      f.write(JSON.pretty_generate(@tweets))
    end
  end
end