|
7 | 7 | import ussl as ssl |
8 | 8 | except: |
9 | 9 | import ssl |
| 10 | +import time |
10 | 11 |
|
11 | | -# Get wifi details and more from a secrets.py file |
| 12 | +config = {} |
| 13 | +envfound = True |
12 | 14 | try: |
13 | | - from secrets import secrets |
14 | | -except ImportError: |
15 | | - print("WiFi secrets are kept in secrets.py, please add them there!") |
16 | | - raise |
| 15 | + envfile = open('/.env') |
| 16 | +except: |
| 17 | + envfound = False |
| 18 | + |
| 19 | +if envfound: |
| 20 | + for line in envfile: |
| 21 | + try: |
| 22 | + config[line.split('=')[0].strip()] = line.split('=')[1].strip() |
| 23 | + except: |
| 24 | + pass |
| 25 | + envfile.close() |
| 26 | + |
| 27 | +# Check .env has updated credentials |
| 28 | +if not envfound or config.get('CIRCUITPY_WIFI_SSID','') == '': |
| 29 | + assert False, ("/.env has not been updated with your unique keys and data") |
17 | 30 |
|
18 | 31 | # AP info |
19 | | -SSID=secrets['ssid'] # Network SSID |
20 | | -KEY=secrets['password'] # Network key |
| 32 | +SSID=config['CIRCUITPY_WIFI_SSID'] # Network SSID |
| 33 | +KEY=config['CIRCUITPY_WIFI_PASSWORD'] # Network key |
21 | 34 |
|
22 | 35 | PORT = 443 |
23 | | -HOST = "money.cnn.com" |
| 36 | +#HOST = "money.cnn.com" |
24 | 37 | #HOST2 = "www.money.cnn.com/data/markets" |
| 38 | +HOST = "finance.yahoo.com" |
25 | 39 |
|
26 | 40 | wlan = network.WLAN(network.STA_IF) |
27 | 41 | if not wlan.active(): |
|
55 | 69 | #client.settimeout(3.0) |
56 | 70 |
|
57 | 71 | # Send HTTP request and recv response |
58 | | -print("Fetching text from %s/data/markets" % HOST) |
59 | | -client.write("GET /data/markets HTTP/1.1\r\nHost: %s\r\n\r\n"%(HOST)) |
| 72 | +print("Fetching text from %s/quote/%%5EIXIC" % HOST) |
| 73 | +#client.write("GET /data/markets HTTP/1.1\r\nHost: %s\r\n\r\n"%(HOST)) |
| 74 | +client.write("GET /quote/%%5EIXIC HTTP/1.1\r\nHost: %s\r\n\r\n"%(HOST)) |
60 | 75 |
|
61 | 76 | print("-" * 40) |
62 | 77 | response = client.read(800).decode('utf-8') |
|
65 | 80 | print() |
66 | 81 |
|
67 | 82 | nasdaq = -1 |
68 | | -while nasdaq == -1: |
| 83 | +strtime = time.ticks_ms() |
| 84 | +timer = 0 |
| 85 | +while nasdaq == -1 and timer < 60000: |
69 | 86 | gc.collect() |
70 | | - response = client.read(4096).decode('utf-8') |
71 | | - |
72 | | - nasdaq = response.find('data-ticker-name="Nasdaq"') |
73 | | - pct = response[nasdaq:].find('%') |
74 | | - pctst = response[nasdaq+35:].find('>') |
75 | | - pctend = response[nasdaq+35:].find('<')-1 |
76 | | - |
77 | | -print("Nasdaq: ",response[nasdaq+36+pctst:nasdaq+36+pctend]) |
| 87 | + #response = client.read(4096).decode('utf-8') |
| 88 | + encresponse = client.read(4096) |
| 89 | + decerror = False |
| 90 | + try: |
| 91 | + response = encresponse.decode() |
| 92 | + except: |
| 93 | + decerror = True |
| 94 | + |
| 95 | + if decerror: |
| 96 | + response = "" |
| 97 | + for chresp in encresponse: |
| 98 | + try: |
| 99 | + response += chr(chresp) |
| 100 | + except: |
| 101 | + pass |
| 102 | + |
| 103 | + #nasdaq = response.find('data-ticker-name="Nasdaq"') |
| 104 | + nasdaq = response.find('data-symbol="^IXIC" data-field="regularMarketChangePercent"') |
| 105 | + pct = response[nasdaq:].find('%)') |
| 106 | + pctst = response[nasdaq+pct-15:].find('>') |
| 107 | + pctend = response[nasdaq+pct:].find('<') |
| 108 | + |
| 109 | + timedelta = time.ticks_ms() - strtime |
| 110 | + if timedelta > 0: |
| 111 | + timer += timedelta |
| 112 | + strtime += timedelta |
| 113 | + else: |
| 114 | + strtime = time.ticks_ms() |
| 115 | + |
| 116 | +if nasdaq == -1: |
| 117 | + print("Timeout fetching Web data") |
| 118 | +else: |
| 119 | + print("Nasdaq: ",response[nasdaq+pct-14+pctst:nasdaq+pct+pctend]) |
78 | 120 |
|
79 | 121 | # Close socket |
80 | 122 | #sslclient.close() |
|
0 commit comments