-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.4.0 Adding all my SERP scripts for Andrei
- Loading branch information
Showing
6 changed files
with
466 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ doc | |
*.gemspec | ||
.echodo_history | ||
.ondir.was.here.touch | ||
.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Ricc figata! copiato da SERP.com | ||
# https://serpapi.com/playground?engine=google_flights&departure_id=ZRH&arrival_id=AMS&hl=it¤cy=CHF&outbound_date=2024-01-01&return_date=2024-01-05&adults=2&stops=1 | ||
require 'google_search_results' | ||
require 'rainbow' | ||
|
||
# params = { | ||
# api_key: "YOUR_SERP_API_KEY", | ||
# engine: "google_flights", | ||
# hl: "it", | ||
# departure_id: "ZRH", | ||
# arrival_id: "AMS", | ||
# outbound_date: "2024-01-01", | ||
# return_date: "2024-01-05", | ||
# currency: "CHF", | ||
# adults: "2", | ||
# stops: "1" | ||
# } | ||
|
||
# search = GoogleSearch.new(params) | ||
# hash_results = search.get_hash | ||
# | ||
def _yellow(s) | ||
"\033[1;33m#{s}\033[0m" | ||
end | ||
def _red(s) | ||
Rainbow(s).red.bold # "\033[1;33m#{s}\033[0m" | ||
end | ||
require 'google_search_results' | ||
|
||
SERP_API_KEY = ENV.fetch 'SERP_API_KEY', nil | ||
raise('Missing ENV["SERP_API_KEY"].. failing.') unless SERP_API_KEY | ||
|
||
default_query = "salsa ai q" | ||
query = ARGV.size == 0 ? default_query : ARGV.join(' ') | ||
|
||
params = { | ||
api_key: SERP_API_KEY, | ||
engine: "google_autocomplete", | ||
q: query | ||
} | ||
puts _red('Implement caching like in serp-*-cached.rb (or btw move to a library/code repo on its own)') | ||
puts "👀 Searching for autocompletion for '#{query}'.." | ||
|
||
search = GoogleSearch.new(params) | ||
hash_results = search.get_hash | ||
|
||
hash_results[:suggestions].each do |h| | ||
# {:value=>"salsa ai quattro formaggi", :relevance=>1250, :type=>"QUERY", :serpapi_link=>"https://serpapi.com/search.json?engine=google_autocomplete&q=salsa+ai+quattro+formaggi"} | ||
puts " 🎄 [#{h[:relevance]}]\t#{_yellow h[:value]}" | ||
end | ||
|
||
|
||
|
||
|
||
# require 'google_search_results' | ||
|
||
# params = { | ||
# api_key: "YOUR_SERP_API_KEY", | ||
# engine: "google_images", | ||
# google_domain: "google.com", | ||
# q: "avocado shirt", | ||
# hl: "en", | ||
# gl: "us" | ||
# } | ||
|
||
# search = GoogleSearch.new(params) | ||
# hash_results = search.get_hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Ricc figata! copiato da SERP.com | ||
# https://serpapi.com/playground?engine=google_flights&departure_id=ZRH&arrival_id=AMS&hl=it¤cy=CHF&outbound_date=2024-01-01&return_date=2024-01-05&adults=2&stops=1 | ||
require 'google_search_results' | ||
require 'rainbow' | ||
require 'digest' | ||
require 'yaml' | ||
require 'pry' | ||
require 'byebug' | ||
|
||
|
||
SERP_API_KEY = ENV.fetch 'SERP_API_KEY', nil | ||
raise('Missing ENV["SERP_API_KEY"].. failing.') unless SERP_API_KEY | ||
|
||
# Note: SPy doesnt work, probably as the default Nasdaq suffix wont work for it. | ||
$sample_stocks = %w{ AAPL GOOG GOOGL NVDA NFLX TSLA SPY} # NYSEARCA:SPY | ||
$prog_version = '1.1' | ||
$default_stock = 'GOOGL' | ||
$stock_to_check = ARGV[0] || $default_stock | ||
puts("🔧 Checking Stock: #{$stock_to_check}") | ||
|
||
def cached_google_search(params, version=$prog_version) | ||
cache_key = Digest::MD5.hexdigest(params.to_s + $prog_version) # so it changes cache if you bump up version | ||
cache_file = ".cache/google_search_#{cache_key}.json" | ||
|
||
if File.exist?(cache_file) | ||
puts Rainbow("[debug] Cache hit: #{cache_file}").green | ||
|
||
JSON.parse(File.read(cache_file)) | ||
else | ||
# puts "[debug] Cache miss: #{cache_file}" | ||
puts Rainbow("[debug] Cache miss (small bug, ): #{cache_file}").red | ||
search_result = GoogleSearch.new(params) | ||
# FIGATA GALATTICA | ||
# binding.pry | ||
|
||
Dir.mkdir('.cache') unless Dir.exist?('.cache') | ||
|
||
File.open(cache_file, 'w') { |f| f.write(JSON.dump(search_result.get_hash)) } | ||
# search_result.get_hash # non va, credo :key vs 'key' | ||
JSON.parse(File.read(cache_file)) | ||
end | ||
end | ||
|
||
def print_fancy_stock_line(x) | ||
return if x.nil? | ||
stock_name = x['name'] || x['stock'] | ||
currency = x.fetch 'currency', '💲' | ||
mov_hash =x['price_movement'] | ||
price_02 = x['price'].round(2) rescue x['price'] | ||
sign = mov_hash['movement'] == 'Up' ? '+' : '-' | ||
# movement_val = mov_hash['percentage'].to_s.format('%02d', 4) rescue 4242 | ||
movement_val = "#{sign}#{mov_hash['percentage'].round(2)}%" # .to_s.format('%02d', 4) rescue 4242 | ||
colored_movement = Rainbow(movement_val).send(mov_hash['movement'] == 'Up' ? :green : :red) | ||
puts "📈 #{stock_name}:\t #{price_02}#{currency}\t#{colored_movement}" | ||
end | ||
|
||
def print_news_snippet(n) | ||
if n['snippet'] | ||
puts(" 🗞️ #{Rainbow(n['snippet']).yellow} (#{n['source']}, #{n['date']})") | ||
#else | ||
#puts("🐞🐞🐞 #{n}") | ||
end | ||
end | ||
|
||
|
||
params = { | ||
engine: "google_finance", | ||
# q: "GOOGL:NASDAQ", | ||
q: "#{$stock_to_check}:NASDAQ", | ||
# q: "GOOG", #158 e rotti | ||
api_key: SERP_API_KEY, | ||
} | ||
|
||
search = cached_google_search(params) | ||
hash_results = search # .get_hash rescue search | ||
#exit 42 | ||
|
||
puts("1. Markets:") | ||
#binding.pry | ||
|
||
hash_results['markets']['us'].each do |x| | ||
print_fancy_stock_line(x) | ||
end | ||
|
||
if $DEBUG | ||
puts("🐞 hash_results.keys: #{hash_results.keys.join ' '}") | ||
puts("🐞 hash_results.market.keys: #{hash_results['markets'].keys.join ' '}") | ||
#puts("🐞 hash_results.market.us.keys: #{hash_results['markets']['us'].keys.join ' '}") | ||
end | ||
puts("2. Your stock info SUMMARY:") | ||
puts hash_results['summary'] if $DEBUG | ||
print_fancy_stock_line(hash_results['summary']) | ||
|
||
# puts("3. Your MARKETS stock info:") | ||
# puts hash_results['markets'].keys # ['summary'] | ||
puts("3. Market TopNews:") | ||
#puts hash_results['markets']['top_news'].keys # ['summary'] | ||
#puts hash_results['markets']['top_news'] # .keys # ['summary'] | ||
# hash_results['markets']['top_news'].each do |article| | ||
# puts "📰 #{article}" | ||
# end | ||
# puts "📰 #{hash_results['markets']['top_news']['snippet']}" | ||
n = hash_results['markets']['top_news'] | ||
print_news_snippet(n) | ||
#puts("📰 #{Rainbow(n['snippet']).green} (#{n['source']}, #{n['date']})") | ||
|
||
news_results = hash_results['news_results'] | ||
puts("4. News Results:") | ||
(hash_results['news_results'] or []).each do |nr| | ||
title_or_snippet = nr.fetch('title', nr.fetch('snippet', nr)) | ||
puts(" 📰📰 #{Rainbow(title_or_snippet).underline.bright}") | ||
#print_news_snippet(nr) | ||
# puts nr | ||
(nr['items'] or []).each do |n| | ||
print_news_snippet(n) | ||
end | ||
end | ||
|
||
puts("Done. Try again with some of these: #{Rainbow($sample_stocks.join(' ')).purple}") | ||
puts("Note. It #{Rainbow('only').underline} works with NASDAQ stuff.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Ricc figata! copiato da SERP.com | ||
# https://serpapi.com/playground?engine=google_flights&departure_id=ZRH&arrival_id=AMS&hl=it¤cy=CHF&outbound_date=2024-01-01&return_date=2024-01-05&adults=2&stops=1 | ||
require 'google_search_results' | ||
require 'rainbow' | ||
|
||
SERP_API_KEY = ENV.fetch 'SERP_API_KEY', nil | ||
raise('Missing ENV["SERP_API_KEY"].. failing.') unless SERP_API_KEY | ||
|
||
$DEBUG = false | ||
|
||
def _yellow(s) | ||
# Todo use Rainbow | ||
"\033[1;33m#{s}\033[0m" | ||
end | ||
def _red(s) | ||
Rainbow(s).red.bold # "\033[1;33m#{s}\033[0m" | ||
end | ||
|
||
puts _red('Implement caching like in serp-*-cached.rb (or btw move to a library/code repo on its own)') | ||
puts _yellow('Ricc todo aggiungi un Tool a Langchain che fa Aeroporti e Hotel.. nell ordine.') | ||
tomorrow = (Date.today + 1).to_s | ||
in_8_days = (Date.today + 8).to_s | ||
puts(_yellow("Searching for best flights through SerpAPI from tomorrow #{tomorrow} and return in 7 days")) | ||
params = { | ||
api_key: SERP_API_KEY, | ||
engine: "google_flights", | ||
departure_id: "ZRH", | ||
arrival_id: "SJJ", # Sarajevo | ||
outbound_date: tomorrow, # "2024-09-12", # tomorrow | ||
return_date: in_8_days, | ||
currency: "USD", | ||
hl: "en", | ||
q: 'pioggia', | ||
} | ||
|
||
def format_flight_duration_with_m(minutes) | ||
hours = minutes / 60 | ||
remaining_minutes = minutes % 60 | ||
formatted_string = "" | ||
|
||
formatted_string << "#{hours}h" if hours > 0 | ||
formatted_string << "0#{remaining_minutes}m" if remaining_minutes < 10 | ||
formatted_string << "#{remaining_minutes}m" if remaining_minutes >= 10 | ||
|
||
formatted_string.empty? ? "0m" : formatted_string | ||
end | ||
# Omitting final m | ||
def format_flight_duration(minutes) | ||
hours = minutes / 60 | ||
remaining_minutes = minutes % 60 | ||
formatted_string = "" | ||
|
||
formatted_string << "#{hours}h" if hours > 0 | ||
formatted_string << "0#{remaining_minutes}" if remaining_minutes < 10 && hours > 0 | ||
formatted_string << "#{remaining_minutes}" if remaining_minutes >= 10 && hours > 0 | ||
formatted_string << "#{remaining_minutes}m" if remaining_minutes > 0 && hours == 0 | ||
|
||
formatted_string.empty? ? "0m" : formatted_string | ||
end | ||
|
||
def sanitize_airport_name(name) | ||
name.gsub(/Airport/, '').gsub(/International/, '').chomp | ||
end | ||
|
||
def print_flight_with_legs(ix, h) # h is flight | ||
puts "#{ix} ✈️ Best flight: #{h.except :flights}" if $DEBUG | ||
total_duration = format_flight_duration(h[:total_duration]) | ||
price = h[:price] | ||
#{:layovers=>[{:duration=>50, :name=>"Frankfurt Airport", :id=>"FRA"}], | ||
layovers = (h[:layovers] or []).map{|h| "#{format_flight_duration h[:duration]} @#{h[:id]}"} | ||
|
||
layover_string = layovers.count == 0 ? | ||
'👍 Direct' : | ||
"#{layovers.count} layover (#{layovers.join(', ')})" | ||
|
||
puts(_yellow("✈️ Flight ##{ix}: #{price}$ Tot⏱️ #{total_duration}m, #{layover_string}")) | ||
h[:flights].each_with_index do |flight,ix| | ||
# LEG🦵 | ||
departure = flight[:departure_airport][:id] | ||
duration = format_flight_duration flight[:duration] | ||
airline = flight[:airline] | ||
travel_class = flight[:travel_class].gsub('Economy', '') # default -> nil | ||
dest = sanitize_airport_name flight[:arrival_airport][:name] | ||
flugzeug = flight[:airplane] | ||
#puts flight | ||
str =" 🦵 #{flight[:departure_airport][:time]} #{flight[:flight_number].gsub(' ','')} #{departure} => #{flight[:arrival_airport][:id]} (#{duration}) #{airline} #{travel_class} #{flugzeug} to #{dest}" | ||
puts(str) | ||
end | ||
end | ||
search = GoogleSearch.new(params) | ||
hash_results = search.get_hash | ||
puts(hash_results) if $DEBUG | ||
#puts _yellow(hash_results.keys) | ||
# search = GoogleSearch.new(params) | ||
# hash_results = search.get_hash | ||
best_flights = hash_results.dig(:best_flights) | ||
puts("== #{best_flights.count} Best flights ==" ) | ||
best_flights.each_with_index do |h,ix| | ||
print_flight_with_legs(ix, h) | ||
|
||
end | ||
puts("Other flights: #{hash_results.dig(:other_flights).count}") | ||
hash_results.dig(:other_flights).each_with_index do |flight,ix| | ||
# puts(flight) | ||
print_flight_with_legs(ix, flight) | ||
end | ||
puts("💙 JSON Endpoint: #{hash_results[:search_metadata][:json_endpoint]}") | ||
puts("🥶 Created at: #{hash_results[:search_metadata][:created_at]}") | ||
|
||
# params = { | ||
# api_key: "YOUR_SERP_API_KEY", | ||
# engine: "google_autocomplete", | ||
# q: "salsa ai q" | ||
# } | ||
|
||
# puts params | ||
# exit 42 | ||
# search = GoogleSearch.new(params) | ||
# hash_results = search.get_hash | ||
|
||
# hash_results[:suggestions].each do |h| | ||
# # {:value=>"salsa ai quattro formaggi", :relevance=>1250, :type=>"QUERY", :serpapi_link=>"https://serpapi.com/search.json?engine=google_autocomplete&q=salsa+ai+quattro+formaggi"} | ||
# puts "🎄 [#{h[:relevance]}]\t#{_yellow h[:value]}" | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.