-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.rb
82 lines (74 loc) · 2.47 KB
/
fetcher.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
require './parser'
require 'csv'
require 'patron'
class Fetcher
def initialize(config)
cookie = config['paytm']['credentials']
@paytm_session = Patron::Session.new
@paytm_session.headers = {
#'User-Agent' => 'Gringotts (https://github.com/captn3m0/gringotts)',
'Cookie' => "connect.sid=#{cookie};"
}
@paytm_session.base_url = 'https://paytm.com'
@paytm_session.enable_debug "/tmp/patron.debug"
@config = config
@parser = Parser.new(config)
end
def splitwise(mock = false)
config = @config['splitwise']
body = ""
if mock
body = File.read('raw_data/splitwise.json')
else
consumer = OAuth::Consumer.new(config["key"], config["secret"], {
:site => config["base_url"],
:scheme => :header,
:http_method => :post,
:authorize_path => config["authorize_path"],
:request_token_path => config["request_path"],
:access_token_path => config["access_token_path"]
})
access_token = OAuth::AccessToken.new(consumer, config["credentials"]["token"], config["credentials"]["secret"])
response = access_token.get('https://secure.splitwise.com/api/v3.0/get_expenses?limit=0')
body = response.body
end
expenses = @parser.splitwise(body)
write('splitwise', expenses)
end
def amazon_com(mock = false)
csv = CSV.read('raw_data/amazon_com.csv', :headers=>true)
expenses = @parser.amazon_com(csv)
write('amazon_com', expenses)
end
def paytm(mock = false)
body = ''
if mock
# For now, we just parse the json file
body = File.read('raw_data/paytm.json')
else
response = @paytm_session.get('/shop/orderhistory?pagesize=300')
body = response.body
end
orders = @parser.paytm(body)
write('paytm', orders)
end
def uber(mock = false)
body = ''
if mock
# For now, we just parse the json file
body = File.read('raw_data/paytm_txn.json')
else
response = @paytm_session.get('/shop/wallet/txnhistory?page_size=199&page_number=0')
body = response.body
end
# Now we parse the transactions
rides = @parser.uber(body)
write('uber', rides)
end
def write(method, data)
data.each do |month, e|
content = e.to_yaml
File.write("reports/#{method}/#{month}.yml", content)
end
end
end