The main goal of this project is to demonstrate the Omega's capabilities in a practical webscraping application. We will be making a stock ticker using our Omega to retrieve prices and the OLED Expansion to display them. This tutorial is done in Python.
OverviewTutorial Difficulty: Beginner
Time Required: 15 minutes
Required Materials:
- The Omega
- The Expansion Dock
- OLED Expansions
Useful Experience:
- Using the OLED Expansion
- Using Python
- Using Shell Scripts
- Scheduling with Cron
Connect your Omega, Expansion Dock, and OLED Expansion as shown.
You will need to setup Wifi so that we can install Python and fetch stock data from Google:
wifisetup
Follow the instructions and give the Omega access to your wifi network.
Update and Install PythonWe will need to install the full version of Python so that we have access to some libraries necessary for webscraping:
opkg update
opkg install python
You may or may not need to clear some space on your Omega for the installation. If you are doing this right out of the box, you should be fine.
Create the Shell ScriptNavigate to your "/" directory. Create the shell script which we will be using:
cd / cat > stock.sh
Now paste the following code (You can save and exit the command by entering CTRL + D):
#!/bin/sh
oled-exp -c
echo $1 > /root/ticker.txt
VAR=$(python ./stock_script.py)
oled-exp -i oled-exp write $VAR
Create the Python ScriptNow lets do the same with our Python script.
cat > stock_script.py
#!/usr/bin/env python
import urllib
import json
myfile = open('/root/ticker.txt', 'r')
rg=myfile.read()
site="http://www.google.com/finance/info?q="+rg
jfile=urllib.urlopen(site)
jsfile=jfile.read()
jsfile=jsfile.replace("\n","")
jsfile=jsfile.replace("/","")
jsfile=jsfile.replace("]","")
jsfile=jsfile.replace("[","")
a=json.loads(jsfile)
ticker=a['t']
price=a['l_fix']
info=ticker+":"+price
print info
Change PermissionsMake both files we just created executable by entering this:
chmod +x stock.sh stock_script.py
Execute ScriptNow let's see how Apple is doing.
./stock.sh AAPL
Going Further (optional):Going Further:
To take the project one step further, let's schedule our script to run every minute. To do this, enter the command:
crontab -e
You will be taken to the vi-editor of the scheduling file. Add the following line to your file and make sure to end the file with an empty line or #.
*/1 * * * * /stock.sh AAPL
You can save and quit the editor by entering ZZ. Using this project as a template, we can go even further with webscraping projects. Maybe build a new headline streamer or an up to date weather tracker.
Parts of this project can and are encouraged to be used in other projects.
This guide was taken from the onion wiki: https://wiki.onion.io/Projects/Homemade_Stock_Ticker
Comments
Please log in or sign up to comment.