root/experimental_v2/Cronjobs/currencies_update_yahoo.php

Revision 546, 3.7 kB (checked in by areski, 1 year ago)

UPDATE : lib for Crontjobs

  • Property svn:executable set to *
Line 
1 #!/usr/bin/php -q
2 <?php
3 /***************************************************************************
4  *            currencies_update_yahoo.php
5  *
6  *  ADD THIS SCRIPT IN A CRONTAB JOB
7  *
8     crontab -e
9     0 6 * * * php /var/lib/asterisk/agi-bin/libs_a2billing/crontjob/currencies_update_yahoo.php
10     
11     field     allowed values
12     -----     --------------
13     minute             0-59
14     hour             0-23
15     day of month    1-31
16     month             1-12 (or names, see below)
17     day of week         0-7 (0 or 7 is Sun, or use names)
18     
19     The sample above will run the script every day at 6AM
20     
21
22 ****************************************************************************/
23
24 set_time_limit(0);
25 error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
26 //dl("pgsql.so"); // remove "extension= pgsql.so !
27
28
29 include (dirname(__FILE__)."/lib/Class.A2Billing.php");
30 include_once (dirname(__FILE__)."/lib/Class.Table.php");
31 include (dirname(__FILE__)."/lib/Misc.php");
32
33
34 $FG_DEBUG=0;
35
36
37 $A2B = new A2Billing();
38
39 // SELECT THE FILES TO LOAD THE CONFIGURATION
40 $A2B -> load_conf($agi, DEFAULT_A2BILLING_CONFIG, 1);   
41
42
43 // DEFINE FOR THE DATABASE CONNECTION
44 define ("BASE_CURRENCY", strtoupper($A2B->config["global"]['base_currency']));
45
46 // get in a csv file USD to EUR and USD to CAD
47 // http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X+USDCAD=X&f=l1
48
49
50 $A2B -> load_conf($agi, NULL, 0, $idconfig);
51 //$A2B -> log_file = $A2B -> config["log-files"]['cront_currencies_update'];
52 //$A2B -> write_log("[START CURRENCY UPDATE]", 0);
53 write_log(LOGFILE_CRONT_CURRENCY_UPDATE, basename(__FILE__).' line:'.__LINE__."[#### START CURRENCY UPDATE ####]");
54
55 if (!$A2B -> DbConnect()){
56     echo "[Cannot connect to the database]\n";
57     write_log(LOGFILE_CRONT_CURRENCY_UPDATE, basename(__FILE__).' line:'.__LINE__."[Cannot connect to the database]");
58     exit;
59 }
60
61
62 $instance_table = new Table();
63 $A2B -> set_instance_table ($instance_table);
64
65 $QUERY "SELECT id,currency,basecurrency FROM cc_currencies ORDER BY id";
66 $result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
67     
68 $url = "http://download.finance.yahoo.com/d/quotes.csv?s=";
69
70 /* result[index_result][field] */
71
72 $index_base_currency = 0;
73
74 if (is_array($result)){
75     $num_cur = count($result);
76     write_log(LOGFILE_CRONT_CURRENCY_UPDATE, basename(__FILE__).' line:'.__LINE__."[CURRENCIES TO UPDATE = $num_cur]", 0);
77     for ($i=0;$i<$num_cur;$i++){
78         if ($FG_DEBUG >= 1) echo $result[$i][0].' - '.$result[$i][1].' - '.$result[$i][2]."\n";
79         // Finish and add termination ?
80         if ($i+1 == $num_cur) $url .= BASE_CURRENCY.$result[$i][1]."=X&f=l1";
81         else $url .= BASE_CURRENCY.$result[$i][1]."=X+";
82         
83         // Check what is the index of BASE_CURRENCY to save it
84         if (strcasecmp(BASE_CURRENCY, $result[$i][1]) == 0) {
85             $index_base_currency = $result[$i][0];
86         }
87     }
88     
89     // Create the script to get the currencies
90     exec("wget '".$url."' -O /tmp/currencies.cvs  2>&1", $output);
91     if ($FG_DEBUG >= 1) echo "wget '".$url."' -O /tmp/currencies.cvs";
92     
93     // get the file with the currencies to update the database
94     $currencies = file("/tmp/currencies.cvs");
95     
96     // update database
97     foreach ($currencies as $currency){
98         
99         $currency = trim($currency);
100         
101         if (!is_numeric($currency)){
102             continue;
103         }
104         $id++;
105         // if the currency is BASE_CURRENCY the set to 1
106         if ($id == $index_base_currency) $currency = 1;
107         
108         if ($currency!=0) $currency=1/$currency;
109         $QUERY="UPDATE cc_currencies SET value=".$currency;
110         
111         if (BASE_CURRENCY != $result[$i][2]){
112             $QUERY .= ", basecurrency='".BASE_CURRENCY."'";
113         }
114         $QUERY .= " , lastupdate = CURRENT_TIMESTAMP WHERE id =".$id;
115         
116         $result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY, 0);
117         if ($FG_DEBUG >= 1) echo "$QUERY \n";
118         //if ($id == 5) exit;
119     }
120     write_log(LOGFILE_CRONT_CURRENCY_UPDATE, basename(__FILE__).' line:'.__LINE__."[CURRENCIES UPDATED !!!]", 0);
121 }
122
123 ?>
124
Note: See TracBrowser for help on using the browser.


Google