Skip to content

Instantly share code, notes, and snippets.

@nil96
Created August 8, 2021 10:03
Show Gist options
  • Save nil96/bbe90edb34f446811effbf875c1790f5 to your computer and use it in GitHub Desktop.
Save nil96/bbe90edb34f446811effbf875c1790f5 to your computer and use it in GitHub Desktop.
this file contains swiggy expenditure over life time
#!/usr/bin/ruby
require "uri"
require "json"
require "net/http"
# hash map contain month and expenditure about month
spending_global = Hash.new
def getData(order_id_list,spending)
url = URI("https://www.swiggy.com/dapi/order/all?order_id=#{order_id_list}")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Get.new(url)
request["authority"] = "www.swiggy.com"
request["sec-ch-ua"] = "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""
request["__fetch_req__"] = "true"
request["sec-ch-ua-mobile"] = "?0"
request["user-agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
request["content-type"] = "application/json"
request["accept"] = "*/*"
request["sec-fetch-site"] = "same-origin"
request["sec-fetch-mode"] = "cors"
request["sec-fetch-dest"] = "empty"
request["referer"] = "https://www.swiggy.com/my-account"
request["accept-language"] = "en-US,en;q=0.9"
request["cookie"] = "*put your own token here*"
request["if-none-match"] = "W/\"1d296-41JtVHuoHfGW5UD0aa7yFUAbgss\""
response = https.request(request)
swiggyResponse = JSON.parse(response.body)
orders = swiggyResponse['data']['orders']
orders.each do |order|
order_id = order['order_id']
order_total = order['order_total']
order_date = order['order_time'].split(" ")
order_year_month = order_date[0].split("-")[0] + "-" + order_date[0].split("-")[1]
if (spending.has_key?(order_year_month))
spending[order_year_month] = spending[order_year_month] + order_total
else
spending.store(order_year_month,order_total)
end
end
if(orders.length()>0)
return orders[orders.length()-1]['order_id']
end
return "-1";
end
def populate_hash_map()
order_id_list = ""
spending_local = Hash.new
while(!order_id_list.eql?("-1"))
order_id_list = getData(order_id_list,spending_local)
end
total_spent = 0
spending_local.each do |key, value|
puts "monthly spent = " + key.to_s + ' : ' + value.to_s + "\n";
total_spent = total_spent + value
end
puts "Your total expenditure is #{total_spent}\n"
end
populate_hash_map();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment