Skip to content

Instantly share code, notes, and snippets.

@liz-miller
Created June 28, 2018 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liz-miller/bf396b4cb70f65f364c66ed53dc5cdd9 to your computer and use it in GitHub Desktop.
Save liz-miller/bf396b4cb70f65f364c66ed53dc5cdd9 to your computer and use it in GitHub Desktop.
Full Code Retrieve and Store data from dweet.io
import dweepy
import sqlite3
import time
# Written by Liz from Learn Robotics (www.learnrobotics.org)
# Version 1.0 | Last Updated: June 27, 2018
# ---
# Software downloaded from the Learn Robotics website is provided 'as is'
# without warranty of any kind, either express or implied, including,
# but not limited to, the implied warranties of fitness for a purpose,
# or the warranty of non-infringement.
# name of the thing as it's spelled on dweet.io
thing = input('Enter thing name: ')
#create a database for the given thing
conn = sqlite3.connect(thing+'.sqlite')
cur = conn.cursor()
cur.execute('''
CREATE TABLE IF NOT EXISTS Data
(id INTEGER UNIQUE, today DATE, sent_at TIME,
temperature INTEGER, humidity INTEGER)
''')
# Pick up where we left off
start = None
cur.execute('SELECT max(id) FROM Data' )
try:
row = cur.fetchone()
if row is None :
start = 0
else:
start = row[0]
except:
start = 0
if start is None : start = 0
count = 0
fail = 0
many = 0
while True:
if ( many < 1 ) :
conn.commit()
sval = input('How many messages: (Press enter to exit) ')
if ( len(sval) <1 ) : break
many = int(sval)
start = start + 1
cur.execute('SELECT id FROM Data WHERE id=?', (start,) )
try:
print("fetching data...")
row = cur.fetchone()
if row is not None : continue
except:
row = None
many = many - 1
url = dweepy.get_latest_dweet_for(thing)
text = "None"
try:
#store the data as a dictionary
dict = url[0]
#print(dict)
longdate = dict['created']
#print(longdate)
date = longdate[:10]
#print(date)
stamptime = longdate[11:19]
#print(stamptime)
tempC = dict['content']['temp']
#print(tempC)
tempF = (int(tempC)*(9/5))+32
#print(tempF)
hum = dict['content']['humidity']
#print(hum)
except KeyboardInterrupt:
print('')
print('Program interrupted by user...')
break
except Exception as e:
print("Unable to retrieve device data",url)
print("Error",e)
fail = fail + 1
if fail > 5 : break
continue
cur.execute('''INSERT OR IGNORE INTO Data (id, today, sent_at, temperature, humidity)
VALUES ( ?, ?, ?, ?, ? )''', ( start, date, stamptime, tempC, hum))
print("reading #: ",start)
if count % 50 == 0 : conn.commit()
if count % 100 == 0 : time.sleep(1)
#if many == start : break;
print("Done fetching!")
print('Do you want to print out the current readings? ')
printout = input('Type Y for YES or any key for NO ')
if (printout == 'Y' or printout == 'y'):
print("Status update for",thing)
print("Date:",date)
print("Time:",stamptime)
print("Current temperature is...",tempC,"C")
print("Current temperature is...",tempF,"F")
print("Current humidity is...",hum)
else:
print("Done!")
print('open',thing+'.sqlite','to view collected data.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment