From 8ca86acf63e154f9be9cc0b1e055e14682c2d797 Mon Sep 17 00:00:00 2001 From: Narath Carlile Date: Fri, 30 Jul 2021 09:15:03 -0400 Subject: [PATCH] Updated file writing so it does not fail if there are no query results --- searchtweets/search_tweets.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/searchtweets/search_tweets.rb b/searchtweets/search_tweets.rb index ab8e1d3..8ebf5dc 100644 --- a/searchtweets/search_tweets.rb +++ b/searchtweets/search_tweets.rb @@ -428,15 +428,18 @@ def make_data_request(query, start_time, end_time, since_id, until_id, max_resul if @write_mode == 'files' #write the file. - #Each 'page' has a start and end time, go get those for generating filename. - - filename = "" - #TODO: just pass in first timestamp: results.first['created_at'] - filename = get_file_name(query, api_response['data']) - - puts "Storing Search API data in file: #{filename}" - File.open("#{@out_box}/#{filename}.json", "w") do |new_file| - new_file.write(api_response.to_json) + if api_response.key?("data") + #Each 'page' has a start and end time, go get those for generating filename. + filename = "" + #TODO: just pass in first timestamp: results.first['created_at'] + filename = get_file_name(query, api_response['data']) + + puts "Storing Search API data in file: #{filename}" + File.open("#{@out_box}/#{filename}.json", "w") do |new_file| + new_file.write(api_response.to_json) + end + else + puts "No Tweets." if @verbose end elsif @write_mode == 'standard_out' #Standard out