This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sesh = requests.Session() | |
response = sesh.get('https://ga-covid19.ondemand.sas.com/static/js/main.js') | |
response_text = response.text |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json_extract = response_text.split("{t.exports=JSON.parse('[")[1].split("]\')},function(t)")[0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json_list = json_extract.split("},{") | |
json_list = [sub.replace("{", "") for sub in json_list] | |
json_list = ["["+sub+"]" for sub in json_list] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json_df = pd.DataFrame(json_list,columns=["new"]) | |
json_df = json_df["new"].str.split(",", expand = True) | |
col_names = ['Measure','County','Test Date','Positives','Death Count','Cumulative Positives', | |
'Cumulative Deaths','Moving Average (Cases)','Moving Average (Deaths)'] | |
json_df.columns = col_names |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json_df[json_df.columns] = json_df[json_df.columns].replace({'"':'',']':'','}':''},regex=True) | |
for col_name in col_names: | |
json_df[col_name] = json_df[col_name].str.split(":",expand=True)[1] | |
json_df = json_df.sort_values(by=['Test Date','Measure','County'], ascending=[False,False,True]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
todays_date = date.today().strftime("%m_%d_%Y") | |
json_df.to_csv(f'/path/to/directory/Georgia_Extract_({todays_date}).csv',index=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sshtunnel import SSHTunnelForwarder | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy import create_engine | |
from sqlalchemy import inspect | |
import pandas as pd | |
class Postgresql_connect(object): | |
def __init__(self, pgres_host, pgres_port, db, ssh, ssh_user, ssh_host, ssh_pkey): | |
# SSH Tunnel Variables | |
self.pgres_host = pgres_host |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p_host = '0.0.0.0' | |
p_port = 5432 | |
db = 'database_name' | |
ssh = True | |
ssh_user = 'ssh_user' | |
ssh_host = 'ip address or web address' | |
ssh_pkey = '/path/to/user_authentication.pem' | |
pgres = Postgresql_connect(pgres_host=p_host, pgres_port=p_port, db=db, ssh=ssh, ssh_user=ssh_user, ssh_host=ssh_host, ssh_pkey=ssh_pkey) | |
#initiates a connection to the PostgreSQL database. In this instance we use ssh and must specify our ssh credentials. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pgres.schemas(db='database_name') | |
#returns the number of schemas and all schema names within the specified database as a pandas dataframe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pgres.tables(db='database_name',schema='schema_name') | |
#returns the number of tables and all table names within the specified schema as a pandas dataframe |
OlderNewer