Browse Source

Add formatters and basic python packaging files

Ian Adam Naval 5 years ago
parent
commit
a8632ce2ef
5 changed files with 73 additions and 6 deletions
  1. 1
    0
      .gitignore
  2. 51
    0
      formatters/console.py
  3. 0
    0
      scrapers/__init__.py
  4. 11
    6
      scrapers/bank_of_america.py
  5. 10
    0
      setup.py

+ 1
- 0
.gitignore View File

@@ -3,4 +3,5 @@ ghostdriver.log
3 3
 *.pyc
4 4
 .env
5 5
 db.sqlite3
6
+*.egg-info
6 7
 

+ 51
- 0
formatters/console.py View File

@@ -0,0 +1,51 @@
1
+class ConsoleFormatter(object):
2
+    """Formats data to be printed to a console."""
3
+
4
+    def __init__(self, account_data):
5
+        self.account_data = account_data
6
+
7
+    def as_string(self):
8
+        result = ''
9
+        for account in self.account_data['accounts']:
10
+            name = account['name']
11
+            balance = account['balance']
12
+            transactions = account['transactions']
13
+            result += name + '\n'
14
+            result += '=' * len(name) + '\n'
15
+            result += 'date,description,amount\n'
16
+            for transaction in transactions:
17
+                if 'CHANGE' in transaction['description']:
18
+                    continue
19
+                result += str(transaction['date']) + ','
20
+                result += str(transaction['description']) + ','
21
+                result += str(transaction['amount']) + '\n'
22
+            result += 'Balance: ' + balance + '\n'
23
+            result += '\n'
24
+        return result
25
+
26
+
27
+def main():
28
+    from selenium import webdriver
29
+    from scrapers.bank_of_america import BankOfAmericaBankScraper
30
+    from getpass import getpass
31
+    import os
32
+    import pickle
33
+
34
+    cache_filename = '/tmp/cache'
35
+    data = ''
36
+    if os.path.exists(cache_filename):
37
+        with open(cache_filename, 'rb') as cache_file:
38
+            data = pickle.load(cache_file)
39
+    else:
40
+        with open(cache_filename, 'wb') as cache_file:
41
+            driver = webdriver.PhantomJS()
42
+            scraper = BankOfAmericaBankScraper(driver)
43
+            credentials = (input("username: "), getpass("password: "))
44
+            data = scraper.get_data(credentials)
45
+            pickle.dump(data, cache_file)
46
+    print(ConsoleFormatter(data).as_string())
47
+
48
+
49
+
50
+if __name__ == '__main__':
51
+    main()

+ 0
- 0
scrapers/__init__.py View File


+ 11
- 6
scrapers/bank_of_america.py View File

@@ -6,7 +6,7 @@ from selenium import webdriver
6 6
 from selenium.common.exceptions import NoSuchElementException
7 7
 from bs4 import BeautifulSoup
8 8
 
9
-from common import BankWebAuthenticator, BankScraper
9
+from scrapers.common import BankWebAuthenticator, BankScraper
10 10
 
11 11
 
12 12
 class BankOfAmericaWebAuthenticator(BankWebAuthenticator):
@@ -63,15 +63,20 @@ class BankOfAmericaBankScraper(BankScraper):
63 63
         name = account['name']
64 64
         self.driver.get(self.ACCOUNTS_URL)
65 65
         self.driver.find_element_by_id(name).click()
66
-        soup = BeautifulSoup(self.driver.page_source)
67
-        rows = soup.select('.transaction-records tr')
68
-        transactions = [self._tr_to_transaction(row) for row in rows]
69
-        account['transactions'] = [e for e in transactions if e]  # filter None
66
+        account['transactions'] = []
67
+
68
+        for i in range(3):
69
+            soup = BeautifulSoup(self.driver.page_source)
70
+            rows = soup.select('.transaction-records tr')
71
+            transactions = [self._tr_to_transaction(row) for row in rows]
72
+            account['transactions'] += [e for e in transactions if e]  # filter None
73
+            self.driver.find_element_by_partial_link_text('Previous').click()
70 74
         return account
71 75
 
72 76
     def _tr_to_transaction(self, tr):
73 77
         try:
74
-            date = tr.select('.date-action span')[0].text.strip()
78
+            tr.select('.date-action a')
79
+            date = tr.select('.date-action > span')[0].text.strip()
75 80
             description = tr.select('.transTitleForEditDesc')[0].text.strip()
76 81
             amount = tr.select('.amount')[0].text.strip()
77 82
             return {

+ 10
- 0
setup.py View File

@@ -0,0 +1,10 @@
1
+from distutils.core import setup
2
+
3
+setup(name='librebudget',
4
+      version='1.0',
5
+      description='Free personal finance tool',
6
+      author='Ian Naval',
7
+      author_email='ianonavy@gmail.com',
8
+      url='https://git.ianonavy.com/ianonavy/librebudget',
9
+      packages=['scrapers', 'librebudget'],
10
+     )

Loading…
Cancel
Save