-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_my_news.rb
199 lines (183 loc) · 5.8 KB
/
get_my_news.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env ruby
require 'json'
require 'addressable'
require 'rest-client'
require 'byebug'
require 'pdfkit'
require 'nokogiri'
require File.expand_path('get_ip_info.rb')
# ----------------------------------------------------------------------------
class GetMyNews
LANGUAGE = %w[
ar de en es fr he it nl
no pt ru se ud zh
].freeze
CATEGORY = %w[
business entertainment general health science
sports technology
].freeze
COUNTRY = %w[
ae ar at au be bg br ca ch cn co
cu cz de eg fr gb gr hk hu id ie il
in it jp kr lt lv ma mx my ng nl no
nz ph pl pt ro rs ru sa se sg si sk
th tr tw ua us ve za
].freeze
SORT = %w[
popularity publishedAt
].freeze
def start
puts '[newsapi] GET YOUR NEWS FREAKING FAST'
puts '[newsapi] ENTER YOU APIKEY'
api_key = gets.chomp
puts '[newsapi] ENTER YOUR QUERY OPTIONS SEPARATED BY A COMMA'
puts '[newsapi] FORMAT country,category,keyword'
puts '[newsapi] eg. de,business,xrp'
puts '[newsapi] eg. us,sports,boxing'
puts '[newsapi] eg. health,brain'
puts '[newsapi] eg. fifa'
search = gets.chomp
search = search.split(',')
query = "#{setup_query(search)}apiKey=#{api_key}"
news_items = get_my_news(query)
get_ip_obj = GetIpInfo.new
if !news_items.nil?
news_items.each do |article|
ip_info = get_ip_obj.start(article['url'])
article.merge!(ip_info)
write_articles_to_pdf(article)
end
else
puts '[newsapi] NO RESULTS FOUND'
end
end
def get_my_news(query)
response = get_request(query)
news_items = []
begin
json = JSON.parse(response.body)
if json['status'] == 'ok' && !json['totalResults'].zero?
json['articles'].each do |item|
article = {}
article['soure_name'] = item['source']['name']
article['url'] = item['url']
news_items << article
# article['author']
# article['title']
# article['description']
# article['url']
# article['urlToImage']
# article['publishedAt']
# article['content']
end
return news_items
elsif json['status'] == 'ok' && json['totalResults'].zero?
# no results found
end
rescue JSON::ParserError => e
puts e
return nil
end
end
def write_articles_to_pdf(article)
response = get_request(article['url'])
sleep 1
html = response.body
html_to_pdf(html, article)
html = add_ip_info_to_html(response,article)
html_to_pdf(html, article, 'ip-info-added')
end
def add_ip_info_to_html(response,article)
doc = Nokogiri::XML(response.body)
ip_info = Nokogiri::XML::Node.new "ip_info", doc
ip_info.content = "#{article['as']} city: #{article['city']}
country: #{article['country']}
isp: #{article['isp']}
lat:#{article['lat']} long:#{article['lon']}
org: #{article['org']} ip:#{article['query']}
region: #{article['regionName']} zip: #{article['zip']}"
# doc.at("document").add_child(ip_info)
if doc.at("title")
doc.at("title").add_child(ip_info)
elsif doc.at("head")
doc.at("head").add_child(ip_info)
elsif doc.at("body")
doc.at("body").add_child(ip_info)
end
doc.to_html
end
def setup_query(search)
puts '[newsapi] CHOOSE YOUR OPTION'
puts '[newsapi] 1 - GET TOP HEADLINES'
puts '[newsapi] 2 - GET EVERYTHING'
# puts '[newsapi] 3 - GET SOURCES'
option = gets.chomp
query = assign_query_type(search, option)
if search
search.each do |search_item|
term_type = check_term_type(search_item)
query = update_query(query, term_type, search_item)
# if search_item.split(":") # if can be splited further
# container = []
# term_type = nil
# terms = search_item.split(":")
# terms.each do |term|
# term_type = check_term_type(term)
# container << collect_terms(query, term_type, term)
# end
# query = update_query(query, term_type, container)
# else # one search item
# term_type = check_term_type(search_item)
# end
end
end
query
end
def update_query(query, term_type, search_item)
"#{query}#{term_type}=#{search_item}&"
end
def assign_query_type(search, option)
option = option.to_i
return 'https://newsapi.org/v2/top-headlines?' if option == 1
return 'https://newsapi.org/v2/everything?' if option == 2
return 'https://newsapi.org/v2/top-headlines' if search.nil && option == 1
return 'https://newsapi.org/v2/everything' if search.nil && option == 2
end
def check_term_type(term)
if CATEGORY.include?(term)
'category'
elsif COUNTRY.include?(term)
'country'
elsif LANGUAGE.include?(term)
'language'
elsif SORT.include?(term)
'sortBy'
# :NOTE implement date from to
else # it is a keyword to search for
'q'
end
end
def collect_terms(_query, term_type, term)
if term_type == 'category'
container << term
elsif term_type == 'language'
container << term
elsif term_type == 'country'
container << term
end
end
def html_to_pdf(html, article, additional_info = '')
# grab html title or domain name and use for file name
# change to pdf and save to file
file_name = article['soure_name'].to_s.gsub(' ','-')
PDFKit.new(html).to_file("articles\/#{file_name}#{additional_info}.pdf")
end
def get_request(url)
response = RestClient::Request.execute(method: :get, url: Addressable::URI
.parse(url) .normalize.to_str,
timeout: 5)
response
end
end
freaking_fast_news = GetMyNews.new
freaking_fast_news.start