-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmonzo-export.rb
executable file
·118 lines (104 loc) · 5.2 KB
/
monzo-export.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
#!/usr/bin/env ruby
require 'rubygems'
require 'commander/import'
require 'colorize'
require_relative 'lib/transaction_fetcher'
require_relative 'lib/qif_creator'
require_relative 'lib/csv_creator'
require_relative 'lib/oauth'
program :name, 'Monzo Export'
program :version, '0.0.1'
program :description, 'Create QIF files from your Monzo account'
command :qif do |c|
c.syntax = 'monzo-export qif [options]'
c.summary = 'Generate the QIF file'
c.option '--access_token TOKEN', String, 'Your access token from: https://developers.monzo.com/'
c.option '--since DATE', String, 'The date (YYYY-MM-DD) to start exporting transactions from. Defaults to 2 weeks ago'
c.option '--folder PATH', String, 'The folder to export to. Defaults to ./exports'
c.option '--settled_only', String, 'Only export settled transactions'
c.option '--account_id', String, 'Export transactions from a specific account id'
c.option '--config_file FILE', String, 'Optional config filename'
c.action do |args, options|
since = options.since ? Date.parse(options.since).to_time : (Time.now - (60*60*24*14)).to_date
config = options.config_file ? options.config_file : 'config.yml'
access_token = options.access_token || OAuth.new(config).getAccessToken
fetcher = TransactionFetcher.new(access_token, account_id: options.account_id)
QifCreator.new(fetcher.fetch(since: since)).create(options.folder, settled_only: options.settled_only, account_number: (fetcher.account_number || 'prepaid'))
say "Account Number: #{fetcher.account_number}"
say "Sort Code: #{fetcher.sort_code}"
say "Balance: £#{fetcher.balance}"
end
end
command :token do |c|
c.syntax = 'monzo-export token'
c.summary = 'Token'
c.action do |args, options|
config = options.config_file ? options.config_file : 'config.yml'
access_token = options.access_token || OAuth.new(config).getAccessToken
puts access_token
end
end
command :balance do |c|
c.syntax = 'monzo-export balance [options]'
c.summary = 'Show the balance'
c.option '--access_token TOKEN', String, 'Your access token from: https://developers.monzo.com/'
c.option '--account_id ACCOUNT_ID', String, 'Export transactions a specific account'
c.option '--config_file FILE', String, 'Optional config filename'
c.action do |args, options|
config = options.config_file ? options.config_file : 'config.yml'
access_token = options.access_token || OAuth.new(config).getAccessToken
fetcher = TransactionFetcher.new(access_token, account_id: options.account_id)
say "Balance: £#{fetcher.balance}"
end
end
command :csv do |c|
c.syntax = 'monzo-export csv [options]'
c.summary = 'Generate the CSV file'
c.option '--access_token TOKEN', String, 'Your access token from: https://developers.monzo.com/'
c.option '--since DATE', String, 'The date (YYYY-MM-DD) to start exporting transactions from. Defaults to 2 weeks ago'
c.option '--folder PATH', String, 'The folder to export to. Defaults to ./exports'
c.option '--settled_only', String, 'Only export settled transactions'
c.option '--account_id ACCOUNT_ID', String, 'Export transactions from a specific account'
c.option '--config_file FILE', String, 'Optional config filename'
c.action do |args, options|
since = options.since ? Date.parse(options.since).to_time : (Time.now - (60*60*24*14)).to_date
config = options.config_file ? options.config_file : 'config.yml'
access_token = options.access_token || OAuth.new(config).getAccessToken
fetcher = TransactionFetcher.new(access_token, account_id: options.account_id)
CsvCreator.new(fetcher.fetch(since: since)).create(options.folder, settled_only: options.settled_only, account_number: (fetcher.account_number || 'prepaid'))
end
end
command :authurl do |c|
c.syntax = 'monzo-export auth_url [options]'
c.summary = 'Configure OAuth for passwordless/tokenless login'
c.option '--url URL', String, 'The authorization URL you received when you authorized Monzo Export from: https://developers.monzo.com/ you will need to enclose the URL in double quotes'
c.option '--config_file FILE', String, 'Optional config filename'
c.action do |args, options|
config = options.config_file ? options.config_file : 'config.yml'
if options.url
urlParams = CGI.parse(URI.parse(options.url).query)
unless urlParams['code'][0]
say "Badly formatted URL. Missing the code parameter"
abort
end
unless urlParams['state'][0]
say "Badly formatted URL. Missing the state parameter"
abort
end
auth = OAuth.new(config).processAuthUrl(urlParams['code'][0], urlParams['state'][0])
else
say "authcode is required"
end
end
end
command :auth do |c|
c.syntax = 'monzo-export auth [options]'
c.summary = 'Configure OAuth for passwordless/tokenless login'
c.option '--clientid ID', String, 'Your confidential client ID from: https://developers.monzo.com/'
c.option '--clientsecret ID', String, 'Your confidential client secret from: https://developers.monzo.com/'
c.option '--config_file FILE', String, 'Optional config filename'
c.action do |args, options|
config = options.config_file ? options.config_file : 'config.yml'
OAuth.new(config).initialAuth(options.clientid, options.clientsecret)
end
end