Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import urllib.request
- import json
- import pymysql
- import pymysql.cursors
- #Get data
- def GetAPIData(): #Get the dictionary from the API
- fullsite = urllib.request.urlopen("http://api.openweathermap.org/data/2.5/weather?id=2759886&appid=4bde3f68e4ed269f928bd2535c2b30a0")
- encoding = fullsite.info().get_content_charset('utf8')
- data = json.loads(fullsite.read().decode(encoding))
- return data
- def GetToSave(): #Create the variable to save with SQL
- data = GetAPIData()
- temperature = data.get('main').get('temp')
- pressure = data.get('main').get('pressure')
- humidity = data.get('main').get('humidity')
- description = data.get('weather')[0].get('description')
- wind_speed = data.get('wind').get('speed')
- wind_degree = data.get('wind').get('deg')
- print(type(temperature))
- sqldata = temperature, pressure, humidity, description, wind_speed, wind_degree
- return "INSERT INTO WEATHER VALUES(" + temperature + ")"
- def SaveToSQL(Data): #Save the data to the SQL server
- #connect to sql database
- connection = pymysql.connect(host='localhost',
- user='root',
- password='Mysql',
- db='test')
- cursor = connection.cursor()
- #save to sql database
- try:
- cursor.execute(to_save)
- connection.commit()
- print('IT WORKED')
- except:
- connection.rollback()
- print('nope')
- #disconnect from server
- connection.close()
- SaveToSQL(GetToSave())
- # testprint
- #print(type(description))
- #print(description)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement