Class: Twitterscraper::Cli

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

Instance Method Summary collapse

Instance Method Details

#build_output_name(format, options) ⇒ Object



102
103
104
105
106
107
# File 'lib/twitterscraper/cli.rb', line 102

def build_output_name(format, options)
  query = options['query'].gsub(/[ :?#&]/, '_')
  date = [options['start_date'], options['end_date']].select { |val| val && !val.empty? }.join('_')
  file = [options['type'], 'tweets', date, query].compact.join('_') + '.' + format
  File.join('out', file)
end

#export(name, tweets) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/twitterscraper/cli.rb', line 34

def export(name, tweets)
  options['format'].split(',').map(&:strip).each do |format|
    file = build_output_name(format, options)
    Dir.mkdir(File.dirname(file)) unless File.exist?(File.dirname(file))

    if format == 'json'
      File.write(file, generate_json(tweets))
    elsif format == 'html'
      File.write(file, Template.new.tweets_embedded_html(name, tweets, options))
    else
      puts "Invalid format #{format}"
    end
  end
end

#generate_json(tweets) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/twitterscraper/cli.rb', line 49

def generate_json(tweets)
  if options['pretty']
    ::JSON.pretty_generate(tweets)
  else
    ::JSON.generate(tweets)
  end
end

#initialize_loggerObject



109
110
111
# File 'lib/twitterscraper/cli.rb', line 109

def initialize_logger
  Twitterscraper.logger.level = ::Logger::DEBUG if options['verbose']
end

#optionsObject



57
58
59
# File 'lib/twitterscraper/cli.rb', line 57

def options
  @options
end

#parseObject



9
10
11
12
# File 'lib/twitterscraper/cli.rb', line 9

def parse
  @options = parse_options(ARGV)
  initialize_logger
end

#parse_options(argv) ⇒ Object



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
92
93
94
95
96
97
98
99
100
# File 'lib/twitterscraper/cli.rb', line 61

def parse_options(argv)
  options = argv.getopts(
      'h',
      'help',
      'v',
      'version',
      'type:',
      'query:',
      'start_date:',
      'end_date:',
      'lang:',
      'limit:',
      'daily_limit:',
      'order:',
      'threads:',
      'threads_granularity:',
      'chart_grouping:',
      'output:',
      'format:',
      'cache:',
      'proxy:',
      'pretty',
      'verbose',
  )

  options['type'] ||= 'search'
  options['start_date'] = Query::OLDEST_DATE if options['start_date'] == 'oldest'
  options['lang'] ||= ''
  options['limit'] = (options['limit'] || 100).to_i
  options['daily_limit'] = options['daily_limit'].to_i if options['daily_limit']
  options['threads'] = (options['threads'] || 10).to_i
  options['threads_granularity'] ||= 'auto'
  options['format'] ||= 'json'
  options['order'] ||= 'desc'

  options['cache'] = options['cache'] != 'false'
  options['proxy'] = options['proxy'] != 'false'

  options
end


117
118
119
120
121
122
# File 'lib/twitterscraper/cli.rb', line 117

def print_help
  puts <<~'SHELL'
    Usage:
      twitterscraper --query KEYWORD --limit 100 --threads 10 --start_date 2020-07-01 --end_date 2020-07-10 --lang ja --proxy --output output.json
  SHELL
end

Returns:

  • (Boolean)


113
114
115
# File 'lib/twitterscraper/cli.rb', line 113

def print_help?
  options['h'] || options['help']
end


128
129
130
# File 'lib/twitterscraper/cli.rb', line 128

def print_version
  puts "twitterscraper-#{VERSION}"
end

Returns:

  • (Boolean)


124
125
126
# File 'lib/twitterscraper/cli.rb', line 124

def print_version?
  options['v'] || options['version']
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/twitterscraper/cli.rb', line 14

def run
  print_help || return if print_help?
  print_version || return if print_version?

  query_options = {
      type: options['type'],
      start_date: options['start_date'],
      end_date: options['end_date'],
      lang: options['lang'],
      limit: options['limit'],
      daily_limit: options['daily_limit'],
      order: options['order'],
      threads: options['threads'],
      threads_granularity: options['threads_granularity'],
  }
  client = Twitterscraper::Client.new(cache: options['cache'], proxy: options['proxy'])
  tweets = client.query_tweets(options['query'], query_options)
  export(options['query'], tweets) unless tweets.empty?
end