Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Commit

Permalink
Several fixes. Added Weboob library as a submodule. Updated the readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcghorayeb committed Feb 10, 2012
1 parent 8aa1833 commit 2ee1b63
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "app/libraries/weboob"]
path = app/libraries/weboob
url = git://git.symlink.me/pub/romain/weboob-stable.git
15 changes: 0 additions & 15 deletions README

This file was deleted.

36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
turtl - Bank account aggregator
===============================


Requiremed libraries
--------------------
Clone the following libraries into a libraries folder (next to the main app folder).

- lithium
```jinja
git clone git://github.com/UnionOfRAD/lithium.git
```
- li3_swiftmailer
```jinja
git clone git://github.com/greut/li3_swiftmailer.git
```
- li3_flash
```jinja
git clone git://github.com/rich97/li3_flash.git
```
- swiftmailer
```jinja
git clone https://github.com/swiftmailer/swiftmailer.git
```

Todo
----

- More bank connectors based on the weboob tools.
- Graphics
- Mail summaries

License
-------

You can reuse this code as you want and modify it as you seem fit. Just give the copyright when due.
38 changes: 20 additions & 18 deletions app/extensions/data/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ abstract class Connector extends \lithium\core\Object {
protected $_scripts = array(
'SG' => '/home/admin/tools/connectors/sg.py'
);

protected $_results = array();

protected $_accounts = array();

protected $_header = array();

protected $_transactions = array();

protected function _init() {
parent::_init();
}
Expand All @@ -29,19 +29,21 @@ public function setCredentials($login, $password) {
$this->_config['login'] = $login;
$this->_config['password'] = $password;
}

public function scrape($account = null) {
$this->_scrape($account);

if ($account) {
$this->_filterHeader();
$this->_filterTransactions();
}
else {
$this->_filterAccounts();
}
if (!empty($this->_results)) {
if ($account) {
$this->_filterHeader();
$this->_filterTransactions();
}
else {
$this->_filterAccounts();
}

$this->_cleanInput();
$this->_cleanInput();
}
}

public function getResults() {
Expand All @@ -51,22 +53,22 @@ public function getResults() {
public function getAccounts() {
return $this->_accounts;
}

public function getHeader() {
return $this->_header;
}

public function getTransactions() {
return $this->_transactions;
}

public function __destruct() {
}

abstract protected function _scrape();

abstract protected function _filterHeader();

abstract protected function _filterTransactions();

abstract protected function _cleanInput();
Expand Down
4 changes: 2 additions & 2 deletions app/extensions/data/connector/SG.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class SG extends \app\extensions\data\Connector {
protected function _init() {
parent::_init();

$this->_config['bank'] = 'SG';
}

Expand Down Expand Up @@ -133,7 +133,7 @@ function (&$value, $field) use ($connector) {
}
}
);

array_walk_recursive(
$this->_transactions,
function (&$value, $field) {
Expand Down
1 change: 1 addition & 0 deletions app/libraries/weboob
Submodule weboob added at b3e1c4
44 changes: 22 additions & 22 deletions app/models/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class Accounts extends \app\models\AppBaseModel {
'title' => array('type' => 'string', 'default' => ''),
'bankCode' => array('type' => 'string', 'default' => ''),
'agencyCode' => array('type' => 'string', 'default' => ''),

'period' => array('type' => 'object'),
'period.start' => array('type' => 'date'),
'period.end' => array('type' => 'date'),
'period.activityCount' => array('type' => 'integer', 'default' => 0),

'balance' => array('type' => 'object'),
'balance.date' => array('type' => 'date'),
'balance.amount' => array('type' => 'float', 'default' => 0),
Expand All @@ -46,7 +46,7 @@ class Accounts extends \app\models\AppBaseModel {

'snapshots' => array('type' => 'object', 'array' => true, 'default' => array())
);

public $validates = array(
'id' => 'notEmpty',
'user_id' => 'notEmpty',
Expand All @@ -63,7 +63,7 @@ class Accounts extends \app\models\AppBaseModel {

/**
* Creates and returns a data parser for a given $filepath and $bank.
*
*
* @see app\extensions\data\Parser
* @param string $filepath Path to the bank file to be parsed.
* @param string $bank Bank name which determines the file format for the parser.
Expand All @@ -77,7 +77,7 @@ class Accounts extends \app\models\AppBaseModel {
'guessBank' => $guessBank,
'readFile' => true
));
return $parser;
}*/

Expand Down Expand Up @@ -108,7 +108,7 @@ public static function createConnector($bank, $login, $password) {
*/
public function makeConnector($entity) {
$bank = Banks::first($entity->bank_id);

if (empty($this->_connector) && !empty($bank)) {
$this->_connector = self::createConnector(
$bank->short_title,
Expand All @@ -130,7 +130,7 @@ public function scrape($entity) {
if (!empty($this->_connector)) {
$lastAccess = $entity->portal->lastAccess->sec;
$now = time();

// Make sure you didn't already scrape in the last hour. Let's not spam the website.
//if (empty($lastAccess) || ($now - $lastAccess) > 3600) {
$entity->portal->lastAccess = $now;
Expand All @@ -155,10 +155,10 @@ public function fetchTransactions($entity) {

return array();
}

/**
* Inputs transactions into the database for a given account.
*
*
* @param object $entity
* @param array $transactions List of transactions to be inserted in the database.
*/
Expand All @@ -167,11 +167,11 @@ public function inputTransactions($entity, array $transactions) {
$account_id = (string) $entity->_id;
$bank = Banks::first($entity->bank_id);
$keys = compact('user_id', 'account_id');

foreach ($transactions as $transaction) {
$data = $keys + $transaction;
$t = Transactions::create($data);

if ($t->validates()) {
$t->populateDetails($bank);
$t->guessCategory();
Expand All @@ -190,15 +190,15 @@ public function inputTransactions($entity, array $transactions) {
* $existingAccount->mergeHeaders($newAccount)
* $existingAccount->save();
* }}}
*
*
* @see app\models\Accounts::buildSnapshots
* @param object $entity Existing account.
* @param object $source New account from which data will be copied into $entity.
*/
public function mergeHeaders($destination, $source) {
$date1 = $destination->balance->date->sec;
$date2 = $source->balance->date->sec;

if ($date1 < $date2) {
$destination['balance']['date'] = $source['balance']['date'];
$destination['balance']['amount'] = $source['balance']['amount'];
Expand All @@ -218,10 +218,10 @@ public function mergeHeaders($destination, $source) {
$destination->period->end = $source->period->end;
}
}

/**
* Returns a list of MongoIds for a given list of Accounts.
*
*
* @param array $accounts
*/
public static function getIds($accounts) {
Expand All @@ -230,14 +230,14 @@ public static function getIds($accounts) {
foreach ($accounts as $account) {
$ids[] = (string) $account->_id;
}

return $ids;
}

/**
* Builds snapshots based on a per-month basis for a list of given transactions.
* Stores the result in `$entity->snapshots`.
*
*
* @see app\models\Accounts::buildSnapshots
* @param object $entity
* @todo Optimize loop.
Expand Down Expand Up @@ -269,7 +269,7 @@ public function refresh($entity) {
/**
* Builds snapshots based on a per-month basis for a list of given transactions.
* Stores the result in the $snapshots field.
*
*
* @see app\models\Accounts::buildSnapshot
* @todo Optimize loop.
*/
Expand Down Expand Up @@ -299,15 +299,15 @@ public function buildSnapshots($entity, $transactions = array()) {
/**
* Builds a snapshot out of a list of transactions.
* A snapshot totals the debits, credits, and transaction count (activityCount).
*
*
* Example:
* {{{
* $transactions = Transactions::all();
* $snapshot = Accounts::buildSnapshot($transactions);
* }}}
*
* Usefull when building snapshots on a per-month basis.
*
*
* @see app\models\Accounts::buildSnapshots
* @param array $transactions
* @return array Array containing the total of debit, credit, and activityCount.
Expand All @@ -323,7 +323,7 @@ public static function buildSnapshot($transactions) {

return $snapshot;
}

// ******************************************
// Bridges
// ******************************************
Expand All @@ -340,7 +340,7 @@ public function transactions($entity, $limit = 30) {
/**
* Returns a list of periods available for a list of accounts.
* A period is usually a month designated by the format `mm-yyyy`.
*
*
* @param array $accounts List of accounts.
* @return array Periods for the accounts given.
*/
Expand Down
Loading

0 comments on commit 2ee1b63

Please sign in to comment.