Class: Twitterscraper::Cli
- Inherits:
-
Object
- Object
- Twitterscraper::Cli
- Defined in:
- lib/twitterscraper/cli.rb
Instance Method Summary collapse
- #build_output_name(format, options) ⇒ Object
- #export(name, tweets) ⇒ Object
- #generate_json(tweets) ⇒ Object
- #initialize_logger ⇒ Object
- #options ⇒ Object
- #parse ⇒ Object
- #parse_options(argv) ⇒ Object
- #print_help ⇒ Object
- #print_help? ⇒ Boolean
- #print_version ⇒ Object
- #print_version? ⇒ Boolean
- #run ⇒ Object
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, ) query = ['query'].gsub(/[ :?#&]/, '_') date = [['start_date'], ['end_date']].select { |val| val && !val.empty? }.join('_') file = [['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) ['format'].split(',').map(&:strip).each do |format| file = build_output_name(format, ) 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.(name, tweets, )) 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 ['pretty'] ::JSON.pretty_generate(tweets) else ::JSON.generate(tweets) end end |
#initialize_logger ⇒ Object
109 110 111 |
# File 'lib/twitterscraper/cli.rb', line 109 def initialize_logger Twitterscraper.logger.level = ::Logger::DEBUG if ['verbose'] end |
#options ⇒ Object
57 58 59 |
# File 'lib/twitterscraper/cli.rb', line 57 def @options end |
#parse ⇒ Object
9 10 11 12 |
# File 'lib/twitterscraper/cli.rb', line 9 def 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 (argv) = 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', ) ['type'] ||= 'search' ['start_date'] = Query::OLDEST_DATE if ['start_date'] == 'oldest' ['lang'] ||= '' ['limit'] = (['limit'] || 100).to_i ['daily_limit'] = ['daily_limit'].to_i if ['daily_limit'] ['threads'] = (['threads'] || 10).to_i ['threads_granularity'] ||= 'auto' ['format'] ||= 'json' ['order'] ||= 'desc' ['cache'] = ['cache'] != 'false' ['proxy'] = ['proxy'] != 'false' end |
#print_help ⇒ Object
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 |
#print_help? ⇒ Boolean
113 114 115 |
# File 'lib/twitterscraper/cli.rb', line 113 def print_help? ['h'] || ['help'] end |
#print_version ⇒ Object
128 129 130 |
# File 'lib/twitterscraper/cli.rb', line 128 def print_version puts "twitterscraper-#{VERSION}" end |
#print_version? ⇒ Boolean
124 125 126 |
# File 'lib/twitterscraper/cli.rb', line 124 def print_version? ['v'] || ['version'] end |
#run ⇒ Object
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? = { type: ['type'], start_date: ['start_date'], end_date: ['end_date'], lang: ['lang'], limit: ['limit'], daily_limit: ['daily_limit'], order: ['order'], threads: ['threads'], threads_granularity: ['threads_granularity'], } client = Twitterscraper::Client.new(cache: ['cache'], proxy: ['proxy']) tweets = client.query_tweets(['query'], ) export(['query'], tweets) unless tweets.empty? end |