Skip to content

Commit 6ce9b04

Browse files
committed
Initial Commit, began instructions
1 parent c767fdb commit 6ce9b04

File tree

5 files changed

+241
-0
lines changed

5 files changed

+241
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.box
2+
*.pyc
3+
.*.sw*

BeautifulSoup.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
import urllib2
3+
from bs4 import BeautifulSoup # import required modules for web scraping
4+
5+
names=["GPS", "NFLX", "GRPN", "INTC", "AAPL", "MOLG", "WAIR", "OTIV", "AMDA", "ARUN", "ICLDW", "AAPL", "GOOG", "BABA"]
6+
7+
def calc(names): # method declaration
8+
for i in names: # loop start to use the yahoo finace link on each stock name
9+
url = 'http://finance.yahoo.com/q?s='+i # the url to visit and scrape for news articles
10+
data = urllib2.urlopen(url) # getting the data from the page
11+
soup = BeautifulSoup(data) # letting beautiful soup 4 work its magic on the data
12+
13+
divs = soup.find('div',attrs={'id':'yfi_headlines'}) # looking for the particular tag
14+
div = divs.find('div',attrs={'class':'bd'}) # looking for a tag under a tag
15+
ul = div.find('ul')
16+
lis = ul.findAll('li')
17+
print i # print the stock you are now dealing with
18+
m=0.0 # score saver for results on using sentiment analysis
19+
for li in lis: # looping through tags that match the required web scraping criteria
20+
headlines = li.find('a').get('href') # getting the url in the tags, linking to actual articles
21+
print headlines
22+
ur='http://access.alchemyapi.com/calls/url/URLGetTextSentiment?apikey=9b61009a54069badce0cc7ed6bc3f229b07d150a&url='+headlines
23+
dat=urllib2.urlopen(ur) # beautiful soup again but on the sentiment analysis results
24+
sou=BeautifulSoup(dat) # beautiful soup again but on the sentiment analysis results
25+
if sou.find('score')!=None:
26+
m+=float(sou.find('score').string)
27+
return m
28+
29+
print calc(names) # print the output

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
3+
4+
5+
# Vagrant
6+
7+
Vagrant is a virtual machine manager that can be shared across platforms.
8+
9+
Download and install vagrant from [Vagrant's website](https://www.vagrantup.com/downloads.html).
10+
11+
The vagrant box can be found under the "Releases" tab of this repository. Download it and follow the Usage instructions
12+
13+
### Usage
14+
To add the vagrant box to local vagrant:
15+
```bash
16+
vagrant box add --name adsa/python [path to vagrant box]
17+
```
18+
Now the box will be available locally. You can use it by copying the Vagrantfile in this repository
19+
or by running
20+
```bash
21+
vagrant init
22+
```
23+
and changing the generated Vagrantfile line
24+
```ruby
25+
config.vm.box = "base"
26+
```
27+
to
28+
```ruby
29+
config.vm.box = "adsa/python"
30+
```
31+
32+
33+
Now, in the folder that contains the Vagrantfile, run
34+
```bash
35+
vagrant up
36+
vagrant ssh
37+
```
38+
39+
This will log you in to the virtual machine and give you console access. Files in the folder containing
40+
the Vagrantfile will be shared to the ```/vagrant``` folder in the virtual machine, so you can edit
41+
files in your normal OS with your text editor of choice (Sublime Text, Atom, etc.).

Vagrantfile

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
# All Vagrant configuration is done below. The "2" in Vagrant.configure
5+
# configures the configuration version (we support older styles for
6+
# backwards compatibility). Please don't change it unless you know what
7+
# you're doing.
8+
Vagrant.configure(2) do |config|
9+
# The most common configuration options are documented and commented below.
10+
# For a complete reference, please see the online documentation at
11+
# https://docs.vagrantup.com.
12+
13+
# Every Vagrant development environment requires a box. You can search for
14+
# boxes at https://atlas.hashicorp.com/search.
15+
config.vm.box = "adsa/python"
16+
17+
# Disable automatic box update checking. If you disable this, then
18+
# boxes will only be checked for updates when the user runs
19+
# `vagrant box outdated`. This is not recommended.
20+
# config.vm.box_check_update = false
21+
22+
# Create a forwarded port mapping which allows access to a specific port
23+
# within the machine from a port on the host machine. In the example below,
24+
# accessing "localhost:8080" will access port 80 on the guest machine.
25+
# config.vm.network "forwarded_port", guest: 80, host: 8080
26+
27+
# Create a private network, which allows host-only access to the machine
28+
# using a specific IP.
29+
# config.vm.network "private_network", ip: "192.168.33.10"
30+
31+
# Create a public network, which generally matched to bridged network.
32+
# Bridged networks make the machine appear as another physical device on
33+
# your network.
34+
# config.vm.network "public_network"
35+
36+
# Share an additional folder to the guest VM. The first argument is
37+
# the path on the host to the actual folder. The second argument is
38+
# the path on the guest to mount the folder. And the optional third
39+
# argument is a set of non-required options.
40+
# config.vm.synced_folder "../data", "/vagrant_data"
41+
42+
# Provider-specific configuration so you can fine-tune various
43+
# backing providers for Vagrant. These expose provider-specific options.
44+
# Example for VirtualBox:
45+
#
46+
# config.vm.provider "virtualbox" do |vb|
47+
# # Display the VirtualBox GUI when booting the machine
48+
# vb.gui = true
49+
#
50+
# # Customize the amount of memory on the VM:
51+
# vb.memory = "1024"
52+
# end
53+
#
54+
# View the documentation for the provider you are using for more
55+
# information on available options.
56+
57+
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
58+
# such as FTP and Heroku are also available. See the documentation at
59+
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
60+
# config.push.define "atlas" do |push|
61+
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
62+
# end
63+
64+
# Enable provisioning with a shell script. Additional provisioners such as
65+
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
66+
# documentation for more information about their specific syntax and use.
67+
# config.vm.provision "shell", inline: <<-SHELL
68+
# sudo apt-get update
69+
# sudo apt-get upgrade
70+
# sudo apt-get install python python-dev libmysqlclient-dev
71+
# wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py
72+
# sudo pip install mysql-python requests
73+
# sudo apt-get install mysql
74+
# SHELL
75+
end

get.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import requests
2+
import json
3+
import MySQLdb
4+
import datetime
5+
import sys
6+
7+
# Script to call the UIUC EWS lab utilization api
8+
# DOES NOT ACCOUNT FOR TIMEZONE!!!
9+
10+
# MYSQL datetime format
11+
time_format = '%Y-%m-%d %H:%M:%S'
12+
13+
14+
15+
16+
17+
18+
def fetch(link):
19+
"""
20+
Parameters: URL to page to read
21+
Returns: JSON from page
22+
"""
23+
# Use requests library to request the file
24+
r = requests.get(link)
25+
# Make sure status code is within acceptable range. See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
26+
if(r.status_code >= 200 and r.status_code < 300):
27+
# Throw it as error if not in 2xx
28+
r.raise_for_status()
29+
# Convert from JSON to python dictionary
30+
data = json.loads(r.text)
31+
return data
32+
33+
34+
def store(json, config):
35+
"""
36+
Parameters: JSON DB Config object of for
37+
{
38+
"host": ...,
39+
"user": ...,
40+
"passwd": ...,
41+
"db": ...,
42+
}
43+
"""
44+
# Snag the data from the "data" key
45+
data = json["data"]
46+
47+
# Connect to the mysql database from config dictionary
48+
# Note: ** is the unwinding operator. Turns a dictionary into named parameters
49+
# Documentation for MySQLdb is here: http://mysql-python.sourceforge.net/MySQLdb.html
50+
db = MySQLdb.connect(**config)
51+
cursor = db.cursor()
52+
for lab in data:
53+
table = lab["strlabname"].replace(" ", "_")
54+
columns = ", ".join(["timestamp", "inusecount" , "machinecount"])
55+
values = ", ".join(["'" + str(datetime.datetime.now().strftime(time_format)) + "'", str(lab["inusecount"]), str(lab["machinecount"])])
56+
# SQL Insert statement: see http://www.w3schools.com/sql/sql_insert.asp
57+
# """ Means keep the formatting of the string
58+
sql = """
59+
INSERT INTO %s
60+
( %s )
61+
VALUES ( %s )
62+
""" % (table, columns, values)
63+
# "Executes" the sql string but doesn't actually commit the result
64+
cursor.execute(sql)
65+
# Commits the change to the database
66+
db.commit()
67+
db.close()
68+
return True
69+
70+
71+
# Code the is equivalent to "int main() {}"
72+
if __name__=="__main__":
73+
"""
74+
Called from cron job, get and store data
75+
"""
76+
link = "https://my.engr.illinois.edu/labtrack/util_data_json.asp"
77+
if len(sys.argv) != 2:
78+
print """
79+
Usage: python %s <path to json config file>
80+
""" % sys.argv[0]
81+
sys.exit(1)
82+
with open(sys.argv[1], "r") as conf:
83+
config = json.load(conf)
84+
85+
try:
86+
# Use the defined functions
87+
data = fetch(link)
88+
# Store the value
89+
success = store(data, config)
90+
if not success:
91+
print "%s" % datetime.datetime.now().strftime(time_format)
92+
except Exception as e:
93+
print e

0 commit comments

Comments
 (0)