-
Notifications
You must be signed in to change notification settings - Fork 107
/
demo_forex.py
65 lines (56 loc) · 1.71 KB
/
demo_forex.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
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import drivers
import time
import requests
import datetime
from bs4 import BeautifulSoup
display = drivers.Lcd()
sleepSecond = 1
minute = 60
iteration = minute/sleepSecond
def GetTime():
currentTime = datetime.datetime.now()
return currentTime.strftime("%d.%m %a %H:%M")
def PrintTime():
display.lcd_display_string(GetTime(), 1)
def PrintCurrency(currency):
display.lcd_display_string(currency, 2)
def PrintScreen(currency):
display.lcd_clear()
PrintTime()
PrintCurrency(currency)
def GetCurrencyList():
try:
request = requests.get("https://www.investing.com/currencies/")
html_content = request.content
# parse content
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find('table', id='cr1')
rows = table.find('tbody').find_all('tr')
currencies_list = {}
for row in rows:
cells = row.find_all('td')
pair = cells[1].find('a').text.strip()
value = cells[2].text.strip()
currencies_list[pair] = value
return currencies_list
except Exception as e:
print(f"Failed to get currency list\n Error: {e}")
return False
# main logic
try:
while True:
currencyList = GetCurrencyList()
if currencyList:
for i in range(int(iteration/len(currencyList))):
for item in currencyList:
PrintScreen(f"{item} {currencyList.get(item)}")
time.sleep(sleepSecond)
else:
display.lcd_clear()
PrintTime()
time.sleep(sleepSecond)
except KeyboardInterrupt:
print("Cleaning up!")
display.lcd_clear()