orsos 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6fe6169334559b768985db4d2454312220ed1ca
4
- data.tar.gz: 63303154856a9585e431d0f14e6cf5673c8b7dbe
3
+ metadata.gz: 583b7c177d70e612ff2119874e9929f089942e53
4
+ data.tar.gz: 6b0399d8c7e9076e019323a70cab445ba0905270
5
5
  SHA512:
6
- metadata.gz: 543e7f9dc9c589fa87838165c3b9c67e6d2dc2563fe9e1e57b8a24ea936b7f627719d8f76cfd27c01ea275e7f753aa04b3e992e26d0c2744177d2b1d6002bc9e
7
- data.tar.gz: 9bdd606574305f3ffbf700b2342d57cf8a3dd0d7b29353f5f3d62ceb5b9b956bf0e05350426d52301481e706b9b228c76006d8bd2609b48d74355d7154545ca9
6
+ metadata.gz: fa7196f3a009cb2d5b2f6ec3d55bfb5b2c2876d4d559367d526bb5c351e244ce849b924bfe293da284e11e4caeb89db4c36c37fc582397fd452255b95205a12e
7
+ data.tar.gz: 79182511ef087494b19b1ac2a739d977ec38f51985ae62bebb0dca419bf27d282b7e1e95d437ed2738f138eab43222e314616c5f4fcd68bf2dcf660c857ef5c0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- orsos (0.0.5)
4
+ orsos (0.0.6)
5
5
  mechanize
6
6
  rest_client (~> 1.7.3)
7
7
  thor
@@ -3,13 +3,14 @@ require_relative '../webdownloader'
3
3
 
4
4
  module Orsos::Commands
5
5
  class Get < Thor
6
+ class_option :in2csv, type: :boolean, desc: 'use in2csv to convert downloaded xls to csv'
7
+ class_option :xls2csv, type: :boolean, desc: 'use xls2csv to convert downloaded xls to csv'
8
+ class_option :stdout, type: :boolean, desc: 'output to stdout'
9
+ class_option :verbose, type: :boolean, desc: 'turn on verbose logging of search'
10
+
6
11
  desc "transactions FROM [TO]", "Download campaign finance transactions daily between FROM till TO and saves each day to sos_transactions_{%Y%m%d}-{current time stamp}. eg., orsos get transactions 2014-10-01 2014-10-31. TO defaults to today's date"
7
- option :verbose, type: :boolean, desc: 'turn on verbose logging of search'
8
12
  option :filer_id, type: :numeric, desc: 'conduct search by filer_id'
9
13
  option :single_file, type: :boolean, desc: 'search and save data in date range as a single file rather than one search per day'
10
- option :in2csv, type: :boolean, desc: 'use in2csv to convert downloaded xls to csv'
11
- option :xls2csv, type: :boolean, desc: 'use xls2csv to convert downloaded xls to csv'
12
- option :stdout, type: :boolean, desc: 'output to stdout'
13
14
  def transactions(from, to=Date.today)
14
15
  from_date = case from
15
16
  when Date
@@ -30,48 +31,82 @@ module Orsos::Commands
30
31
  end
31
32
 
32
33
  trans_opts = options.select{|k,v| ['filer_id'].include?(k) }
33
- if options['in2csv']
34
- csvbin = 'in2csv'
35
- fileext = 'csv'
36
- elsif options['xls2csv']
37
- csvbin = 'xls2csv'
38
- fileext = 'csv'
39
- else
40
- csvbin = nil
41
- fileext = 'xls'
42
- end
43
34
 
44
35
  if !options['single_file'].nil?
45
- filename = "sos_transactions_#{from_date.strftime("%Y%m%d")}-#{to_date.strftime("%Y%m%d")}-#{DateTime.now.strftime("%Y%m%d%H%M%S")}.#{fileext}"
46
- Orsos::Webdownloader.new(options[:verbose])
36
+ Orsos::Webdownloader.new(get_downloader_options(filename: "sos_transactions_#{from_date.strftime("%Y%m%d")}-#{to_date.strftime("%Y%m%d")}-#{DateTime.now.strftime("%Y%m%d%H%M%S")}", options: options))
47
37
  .save_campaign_finance_transactions from_date: from_date,
48
38
  to_date: to_date,
49
- filename: filename,
50
- csvbin: csvbin,
51
- stdout: options['stdout'],
52
39
  options: trans_opts
53
40
 
54
41
  else
55
42
  (from_date..to_date).each do |date|
56
- filename = "sos_transactions_#{date.strftime("%Y%m%d")}-#{DateTime.now.strftime("%Y%m%d%H%M%S")}.#{fileext}"
57
-
58
- Orsos::Webdownloader.new(options[:verbose])
43
+ Orsos::Webdownloader.new(get_downloader_options(filename: "sos_transactions_#{date.strftime("%Y%m%d")}-#{DateTime.now.strftime("%Y%m%d%H%M%S")}", options: options))
59
44
  .save_campaign_finance_transactions from_date: date,
60
45
  to_date: date,
61
- filename: filename,
62
- csvbin: csvbin,
63
- stdout: options['stdout'],
64
46
  options: trans_opts
65
47
  end
66
48
  end
67
49
  end
68
50
 
51
+ desc "committees", "Download committees information sos_committees_{search query}. eg., orsos get committees kitzhaber."
52
+ option :committee_name_contains, type: :string, desc: 'search by name of committee which contains.... eg., --committee_name=kitzhaber searches for records that contain kitzhaber in the name.'
53
+ def committees
54
+ Orsos::Webdownloader.new(get_downloader_options(filename: "sos_committees_#{options['committee_name_contains']}", options: options))
55
+ .save_committees committee_name_contains: options['committee_name_contains']
56
+ end
57
+
58
+ desc "candidate_filings FROM [TO]", "Download candidate_filings between FROM till TO into sos_candidate_filings_{from %Y%m%d}-{to %Y%m%d}-{current time stamp}. eg., orsos get candidate_filings 2014-10-01 2014-10-31. TO defaults to today's date"
59
+ def candidate_filings(from, to=Date.today)
60
+ from_date = case from
61
+ when Date
62
+ from
63
+ when String
64
+ Date.parse from
65
+ else
66
+ raise 'invalid from date'
67
+ end
68
+
69
+ to_date = case to
70
+ when Date
71
+ to
72
+ when String
73
+ Date.parse to
74
+ else
75
+ raise 'invalid to date'
76
+ end
77
+
78
+ Orsos::Webdownloader.new(get_downloader_options(filename: "sos_candidate_filings_#{from_date.strftime("%Y%m%d")}-#{to_date.strftime("%Y%m%d")}-#{DateTime.now.strftime("%Y%m%d%H%M%S")}", options: options))
79
+ .save_candidate_filings from_date: from_date, to_date: to_date
80
+ end
81
+
69
82
  ### FIX for help issue (see commit) ###
70
83
  package_name "get"
71
84
 
72
85
  def self.banner(command, namespace = nil, subcommand = false)
73
86
  "#{basename} #{@package_name} #{command.usage}"
74
87
  end
88
+
75
89
  ### END FIX ###
90
+ private
91
+
92
+ def get_downloader_options filename: , options: {}
93
+ if options['in2csv']
94
+ csvbin = 'in2csv'
95
+ fileext = 'csv'
96
+ elsif options['xls2csv']
97
+ csvbin = 'xls2csv'
98
+ fileext = 'csv'
99
+ else
100
+ csvbin = nil
101
+ fileext = 'xls'
102
+ end
103
+
104
+ {
105
+ verbose: options[:verbose],
106
+ csvbin: csvbin,
107
+ stdout: options['stdout'],
108
+ filename: "#{filename}.#{fileext}"
109
+ }
110
+ end
76
111
  end
77
112
  end
data/lib/orsos/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Orsos
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -7,43 +7,55 @@ module MakeMakefile::Logging
7
7
  end
8
8
 
9
9
  class Orsos::Webdownloader
10
- def initialize(verbose=false)
10
+ def initialize(verbose: false, csvbin: nil, stdout: false, filename: nil)
11
11
  @verbose = verbose
12
+ @csvbin = csvbin
13
+ @stdout = stdout
14
+ @filename = filename
12
15
  end
13
16
 
14
- def save_campaign_finance_transactions from_date:, to_date:, filename: , csvbin:, stdout:, options: {}
15
- puts "downloading transactions for #{from_date.strftime('%Y-%m-%d')} till #{to_date.strftime('%Y-%m-%d')}" if !stdout
16
-
17
- export_page = download_campaign_finance_transactions from_date: from_date, to_date: to_date, filer_id: options['filer_id']
18
- raise "could not download campaign finance transactions" if export_page.nil?
19
-
20
- data = if !csvbin.nil?
21
- csvpath = find_executable0 csvbin
22
- raise "could not find #{csvbin} in $PATH" if csvpath.nil?
23
- file = Tempfile.new(['xls2csv-', '.xls'])
24
- file.binmode
25
- begin
26
- file.write(export_page.body)
27
- file.rewind
28
-
29
- `#{csvpath} #{file.path}`
30
- ensure
31
- file.close
32
- file.unlink
33
- end
34
- else
17
+ def save_campaign_finance_transactions from_date:, to_date:, options: {}
18
+ save("transactions for #{from_date.strftime('%Y-%m-%d')} till #{to_date.strftime('%Y-%m-%d')}") do
19
+ export_page = download_campaign_finance_transactions from_date: from_date, to_date: to_date, filer_id: options['filer_id']
20
+ raise "could not download campaign finance transactions" if export_page.nil?
21
+
22
+ export_page.body
23
+ end
24
+ end
25
+
26
+ def save_committees committee_name_contains:, options: {}
27
+ save("committees searched by #{committee_name_contains}") do
28
+ export_page = download_committees committee_name: committee_name_contains, committee_name_search_type: 'contains'
29
+ raise "could not download committees" if export_page.nil?
30
+ export_page.body
31
+ end
32
+ end
33
+
34
+ def save_candidate_filings from_date: , to_date: , options: {}
35
+ save("candidates filings for #{from_date.strftime('%Y-%m-%d')} till #{to_date.strftime('%Y-%m-%d')}") do
36
+ export_page = download_candidate_filings from_date: from_date, to_date: to_date
37
+ raise "could not download committees" if export_page.nil?
35
38
  export_page.body
36
39
  end
37
40
 
38
- if stdout
41
+ end
42
+
43
+
44
+ private
45
+ def save msg, &block
46
+ puts "downloading #{msg}" if !@stdout
47
+
48
+ body = block.call
49
+ data = !@csvbin.nil? ? convert_to_csv(body) : body
50
+
51
+ if @stdout
39
52
  $stdout.write data
40
53
  else
41
- File.open(filename, 'wb') {|f| f.write(data) }
42
- puts "saved transactions for #{from_date.strftime("%Y-%m-%d")} till #{to_date.strftime('%Y-%m-%d')} to #{filename}"
54
+ File.open(@filename, 'wb') {|f| f.write(data) }
55
+ puts "saved #{msg} to #{@filename}"
43
56
  end
44
57
  end
45
58
 
46
- private
47
59
  def download_campaign_finance_transactions from_date:, to_date:, filer_id: nil
48
60
  set_agent
49
61
  export_page = nil
@@ -64,6 +76,73 @@ private
64
76
  return export_page
65
77
  end
66
78
 
79
+ def download_committees committee_name: '', committee_name_search_type: 'contains'
80
+ set_agent
81
+ export_page = nil
82
+
83
+ @agent.get("#{@base_url}/orestar/GotoSearchByName.do") do |search_page|
84
+ @results_page = @agent.post('https://secure.sos.state.or.us/orestar/CommitteeSearchFirstPage.do', {
85
+ buttonName: '',
86
+ page: 100,
87
+ committeeName: committee_name,
88
+ committeeNameMultiboxText: committee_name_search_type,
89
+ committeeId: '',
90
+ firstName: '',
91
+ firstNameMultiboxText: 'contains',
92
+ lastName: '',
93
+ lastNameMultiboxText: 'contains',
94
+ discontinuedSOO: 'on',
95
+ submit: 'Submit',
96
+ approvedSOO: 'true',
97
+ pendingApprovalSOO: 'false',
98
+ insufficientSOO: 'false',
99
+ resolvedSOO: 'false',
100
+ rejectedSOO: 'false'
101
+ })
102
+
103
+ if link = @results_page.link_with(text: "Export To Excel Format")
104
+ export_page = @agent.click(link)
105
+ end
106
+ end
107
+
108
+ return export_page
109
+ end
110
+
111
+ def download_candidate_filings from_date: , to_date: nil
112
+ set_agent
113
+ export_page = nil
114
+
115
+ @agent.get("#{@base_url}/orestar/CFSearchPage.do") do |search_page|
116
+ search_page.form_with(name: 'cfSearchPageForm') do |form|
117
+ form.cfFilingFromDate = from_date.strftime("%m/%d/%Y")
118
+ form.cfFilingToDate = to_date.strftime("%m/%d/%Y")
119
+
120
+ @results_page = @agent.submit(form, form.button_with(value: "Submit"))
121
+ if link = @results_page.link_with(text: "Export")
122
+ export_page = @agent.click(link)
123
+ end
124
+ end
125
+ end
126
+
127
+ return export_page
128
+ end
129
+
130
+ def convert_to_csv body
131
+ csvpath = find_executable0 @csvbin
132
+ raise "could not find #{@csvbin} in $PATH" if csvpath.nil?
133
+ file = Tempfile.new(['xls2csv-', '.xls'])
134
+ file.binmode
135
+ begin
136
+ file.write(body)
137
+ file.rewind
138
+
139
+ return `#{csvpath} #{file.path}`
140
+ ensure
141
+ file.close
142
+ file.unlink
143
+ end
144
+ end
145
+
67
146
  def set_source_xls_file_and_downloaded_at body, filename
68
147
  file = StringIO.new(body)
69
148
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orsos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Chang