~luisbg/booty/trunk

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
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

# Copyright (C) 2008 Luis de Bethencourt
# <luisbg@ubuntu.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 1, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import ystockquote, sys, os
import smtplib
import optparse

usage = """booty [OPTIONS]"""
parser = optparse.OptionParser(usage = usage)
parser.add_option("-q", "--quiet", action="store_true", \
                          default=False, dest="quiet")
(options, args) = parser.parse_args()

stocks = []
num_stocks = []
purchase_prices = []

stocks_file = open("/home/luisbg/.booty/stocks")

EOF = False
while EOF == False:
    line = stocks_file.readline()
    if line != '':
	stocks.append(line[:-1])
	num_stocks.append(int(stocks_file.readline()[:-1]))
        purchase_prices.append(float(stocks_file.readline()[:-1]))
    else:
	EOF = True

# email info
# pending: read mail info from file also so anybody can use this
fromaddr = "finance <luisbg@ubuntu.com>" 
toaddrs  = "bethencourt@gmail.com"
subject = "[LBG] Finance update"

smtpuser = "bethencourt@gmail.com"
gpass = open("/home/luisbg/gmailpass")
smtppass = gpass.readlines(1)[0]

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
       % (fromaddr, toaddrs, subject))

# Print all the information and add it to the mail
# pending: stock name should also be grabbed from file
c = 0
total = 0
balance_total = 0
for elements in stocks:
    quote = float(ystockquote.get_price(stocks[c]))
    diff = quote - purchase_prices[c]
    if diff > 0:
        quote_str = "Banco Santander: " + str(quote) + "€  (+" + str(diff) \
            + ")" 
    else:
        quote_str = "Banco Santander: " + str(quote) + "€  (" + str(diff) + \
            ")" 
    print quote_str + "\n"

    balance = (quote * num_stocks[c]) - (purchase_prices[c] * num_stocks[c])
    percentage = ((purchase_prices[c] - quote ) / purchase_prices[c]) * -100

    if balance > 0:
        balance_str = "Balance:  +" + str(balance) + "€   (+" \
            + str(percentage)[:4] + "%)"
    else:
        balance_str = "Balance:  " + str(balance) + "€   (" \
            + str(percentage)[:4] + "%)"
    print balance_str

    value = quote * num_stocks[c]
    value_str = "Value:     " + str(value) + "€  (" + str(num_stocks[c]) + " stocks)"
    print value_str

    total += value
    balance_total += balance
    msg = msg + quote_str + " \n" + "\n" + balance_str + "\n" + value_str + "\n\n"
    print ""

    c += 1

if balance_total > 0:
    total_str = "Total:    " + str(total) + "€   (+" + str(balance_total) + ")"
else:
    total_str = "Total:    " + str(total) + "€   (" + str(balance_total) + ")"
print total_str
msg = msg + total_str

if options.quiet == False:
    # Login and send
    print "\nSending..."
    server = smtplib.SMTP('smtp.gmail.com')
    server.set_debuglevel(False)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(smtpuser, smtppass)
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()
    print "Finished :)"