Class: Twitterscraper::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/twitterscraper/template.rb

Instance Method Summary collapse

Instance Method Details

#chart_data(tweets, grouping: 'auto') ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twitterscraper/template.rb', line 20

def chart_data(tweets, grouping: 'auto')
  if grouping && tweets.size > 100
    if grouping == 'auto'
      month = 28 * 24 * 60 * 60 # 28 days
      duration = tweets[-1].created_at - tweets[0].created_at

      if duration > 3 * month
        grouping = 'day'
      elsif duration > month || tweets.size > 10000
        grouping = 'hour'
      else
        grouping = 'minute'
      end
    end
  end

  Twitterscraper.logger.info "Chart grouping #{grouping}"

  data = tweets.each_with_object(Hash.new(0)) do |tweet, memo|
    t = tweet.created_at

    if grouping == 'day'
      time = Time.new(t.year, t.month, t.day, 0, 0, 0, '+00:00')
    elsif grouping == 'hour'
      time = Time.new(t.year, t.month, t.day, t.hour, 0, 0, '+00:00')
    elsif grouping == 'minute'
      time = Time.new(t.year, t.month, t.day, t.hour, t.min, 0, '+00:00')
    else
      time = t
    end
    memo[time.to_i] += 1
  end

  data.sort_by { |k, _| k }.map do |timestamp, count|
    [timestamp * 1000, count]
  end
end

#tweets_embedded_html(name, tweets, options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/twitterscraper/template.rb', line 3

def tweets_embedded_html(name, tweets, options)
  path = File.join(File.dirname(__FILE__), 'template/tweets.html.erb')
  template = ERB.new(File.read(path))

  tweets = tweets.sort_by { |t| t.created_at.to_i }
  grouping = options['chart_grouping'] || 'auto'

  template.result_with_hash(
      chart_name: name,
      chart_data: chart_data(tweets, grouping: grouping).to_json,
      first_tweet: tweets[0],
      last_tweet: tweets[-1],
      tweets: tweets,
      convert_limit: 30,
  )
end