-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.py
99 lines (83 loc) · 3.38 KB
/
writer.py
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
# coding=iso-8859-1
from fin import *
class Write:
def __init__(self, finFile):
self.fence = Finance(finFile)
self.tickers = self.fence.tickers
self.prices = self.fence.prices
self.news = self.fence.news
def updateAll(self):
return self.fence.updateAll()
def printTicks(self):
print(self.tickers)
def writePrice(self, tick):
price = self.prices[tick]
writeText(" <a>$" + price + "</a><br>\n")
def writeNews(self, tick):
newsText = self.news[tick]
style = "style=\"background-color:#E2E2E2;color:black;padding:5px;\""
Articles = newsText
text = ""
web = ""
for k in range (0,3):
Articles[k][0] = Articles[k][0].translate({ord(c): None for c in 'â'})
web = (" <a href=\"" + Articles[k][1] + "\">" + Articles[k][0] + "</a>")
text += (web + "<br>" + "\n")
return text
def remFile(self, fileName):
try:
os.remove(fileName)
except OSError as e:
print("FILE DNE")
def writeText(self, text, fileName):
with open(fileName, 'a') as the_file:
try:
the_file.write(text.encode('utf-8'))
except Exception, e:
print("UnicodeEncodeError: \n")
print(e)
print("\n")
def writeTitle(self, title):
writeText(" <b style=\"color:red\">" + title + "</b><br>\n")
def writeLine(self, text):
return (text + "\n")
def writeFile(self, fileName):
self.remFile(fileName)
text = ""
text += self.writeLine("<style>")
text += self.writeLine("th, td {")
text += self.writeLine(" padding: 5px")
text += self.writeLine("}")
text += self.writeLine("th {")
text += self.writeLine(" text-align: left;")
text += self.writeLine("}")
text += self.writeLine("</style>")
text += self.writeLine("<table style=\"width:50%\">")
text += self.writeLine(" <tr>")
text += self.writeLine(" <th>" + "Name" + "</th>")
text += self.writeLine(" <th>" + "Current" + "</th>")
text += self.writeLine(" <th>" + "Change" + "</th>")
text += self.writeLine(" </tr>")
for tick in self.tickers.keys():
text += self.writeLine(" <tr>")
text += self.writeLine(tick)
text += self.writeLine(" <td>" + self.tickers[tick] + "</td>")
text += self.writeLine(" <td>$" + self.prices[tick] + "</td>")
change = self.fence.getChange(tick)
if( float(change) > 0 ):
text += self.writeLine(" <td style=\"color:#00A000\";>" + change + "%</td>")
elif( float(change) < 0 ):
text += self.writeLine(" <td style=\"color:#A00000\";>" + change + "%</td>")
else:
text += self.writeLine(" <td>" + change + "%</td>")
text += self.writeLine(" </tr>")
text += self.writeLine("</table>")
for tick in self.tickers.keys():
if self.tickers[tick] == "Gold":
continue
text += "<b>" + self.tickers[tick] + "</b><br>\n"
text += self.writeNews(tick)
# print(text)
self.writeText(text, fileName)
def writeAllPricesToFile(self, OorC):
self.fence.writeAllPricesToFile(OorC)