Advertisement
LemmyNjaria

Python Version For Your Challenge Update!!!

Dec 2nd, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.06 KB | None | 0 0
  1. #library for calling GET Requests
  2. import urllib.request
  3. #library for mapping JSON Requests
  4. import json
  5. #library for hadling postgreSQl
  6. import psycopg2
  7.  
  8.  
  9. #this is the main function    
  10. def main():
  11.     #define the Url here in your case utakuwa na three urls
  12.     #run this url through Postman like how i taught you and see the JSON view for you to understand
  13.     urlData1 = "https://redirect.viglink.com/?format=go&jsonp=vglnk_157494196953111&key=949efb41171ac6ec1bf7f206d57e90b8&libId=k3iaga3z01021u9s000DAauhujszi&loc=https%3A%2F%2Fwww.r-bloggers.com%2Fopen-trade-statistics%2F&v=1&type=U&out=https%3A%2F%2Fapi.tradestatistics.io%2Fcountries&ref=https%3A%2F%2Fwww.google.com%2F&title=Open%20Trade%20Statistics%20%7C%20R-bloggers&txt=https%3A%2F%2Fapi.tradestatistics.io%2Fcountries"
  14.     countryCode="ken"
  15.     year="2016"
  16.     urlData2 = "https://api.tradestatistics.io/yrpc?y="+year+"&r=chl&p="+countryCode+""
  17.     urlData3 = "https://api.tradestatistics.io/products"
  18.    
  19.     version1="country"
  20.     version2="products"
  21.     version3="trade"
  22.    
  23.     if(getResponseAndStoreOnDatabase1(urlData1,version1) == True):
  24.         print("Version one is Complete")
  25.     elif(getResponseAndStoreOnDatabase2(urlData2,version2) == True):
  26.         print("Version two is complete")
  27.     else:
  28.         getResponseAndStoreOnDatabase2(urlData3,version3)
  29.         print("Version three is complete")    
  30.  
  31. def getResponseAndStoreOnDatabase1(urlData,version):
  32.     #call the getResponse function passing urlData parameter the function iko chini ya this function
  33.     jsonData = getResponse(urlData)
  34.     if(jsonData is not ''):
  35.         conectToPostGres(jsonData,version)
  36.         return True
  37.  
  38. def getResponseAndStoreOnDatabase2(urlData,version):
  39.     #call the getResponse function passing urlData parameter the function iko chini ya this function
  40.     jsonData = getResponse(urlData)
  41.     if(jsonData is not ''):
  42.         conectToPostGres(jsonData,version)
  43.         return True  
  44.  
  45. def getResponseAndStoreOnDatabase3(urlData,version):
  46.     #call the getResponse function passing urlData parameter the function iko chini ya this function
  47.     jsonData = getResponse(urlData)
  48.     if(jsonData is not ''):
  49.         conectToPostGres(jsonData,version)
  50.         return True  
  51.    
  52. #this function handles a successful connection of the url
  53. def getResponse(url):
  54.     #opens the url
  55.     operUrl = urllib.request.urlopen(url)
  56.     #cheks if the GET response is successfull
  57.     if(operUrl.getcode()==200):
  58.         #opens the url and reads the content
  59.         data = operUrl.read()
  60.         #decods the JSON content
  61.         jsonData = json.loads(data)
  62.     else:
  63.         print("Error receiving data", operUrl.getcode())
  64.     return jsonData
  65.  
  66. #this function handles successfull connection and storing of data to postgreSQL
  67. def conectToPostGres(jsonData,version):
  68.     #handles connection
  69.     #in your case you will change the password and database name umeshika!          
  70.     connection = psycopg2.connect(user="postgres",
  71.                                     password="123",
  72.                                     host="127.0.0.1",
  73.                                     port="5432",
  74.                                     database="ItcImports")
  75.    
  76.     if(version=="country"):
  77.         for i in jsonData:
  78.             try:
  79.                     #this calls the cursor
  80.                     cursor = connection.cursor()
  81.  
  82.                     #this query helps in storying values to the table
  83.                     #in this case you will just change the household name to your table name and its column names
  84.                     postgres_insert_query = """ INSERT INTO public.household("householdNumber", "safeWater", "treatedWater") VALUES (%s,%s,%s)"""
  85.                     #define here the json objects and store them
  86.                     record_to_insert = (i["country_iso"], i["country_name_english"], i["continent"])
  87.                     #execute the cursor
  88.                     cursor.execute(postgres_insert_query, record_to_insert)
  89.                     #commit the connection
  90.                     connection.commit()
  91.                     #this counts the rows
  92.                     count = cursor.rowcount
  93.                     #this prints them if its successfull
  94.                     print (count, "Record inserted successfully into member table")
  95.            
  96.             #this handles errors if it occurs    
  97.             except(Exception, psycopg2.Error) as error:
  98.                 if(connection):
  99.                     print("Failed to insert record into the table",error)
  100.                    
  101.             finally:
  102.                 #closing database connection
  103.                 if(connection):
  104.                     cursor.close()
  105.                     connection.close()
  106.                     print("PostgresQl connection is closed")
  107.                    
  108.     elif(version == "products"):
  109.        
  110.         for i in jsonData:
  111.             try:
  112.                     #this calls the cursor
  113.                     cursor = connection.cursor()
  114.  
  115.                     #this query helps in storying values to the table
  116.                     #in this case you will just change the household name to your table name and its column names
  117.                     postgres_insert_query = """ INSERT INTO public.household("householdNumber", "safeWater", "treatedWater") VALUES (%s,%s,%s)"""
  118.                     #define here the json objects and store them
  119.                     record_to_insert = (int(i["product_code"]), i["group_code"], i["group_name"])
  120.                     #execute the cursor
  121.                     cursor.execute(postgres_insert_query, record_to_insert)
  122.                     #commit the connection
  123.                     connection.commit()
  124.                     #this counts the rows
  125.                     count = cursor.rowcount
  126.                     #this prints them if its successfull
  127.                     print (count, "Record inserted successfully into member table")
  128.            
  129.             #this handles errors if it occurs    
  130.             except(Exception, psycopg2.Error) as error:
  131.                 if(connection):
  132.                     print("Failed to insert record into the table",error)
  133.                    
  134.             finally:
  135.                 #closing database connection
  136.                 if(connection):
  137.                     cursor.close()
  138.                     connection.close()
  139.                     print("PostgresQl connection is closed")
  140.    
  141.     elif(version == "trade"):
  142.        
  143.         for i in jsonData:
  144.             try:
  145.                     #this calls the cursor
  146.                     cursor = connection.cursor()
  147.  
  148.                     #this query helps in storying values to the table
  149.                     #in this case you will just change the household name to your table name and its column names
  150.                     postgres_insert_query = """ INSERT INTO public.household("householdNumber", "safeWater", "treatedWater") VALUES (%s,%s,%s)"""
  151.                     #define here the json objects and store them
  152.                     record_to_insert = (i["reporter_iso"], int(i["product_code"]), int(i["import_value_usd"]))
  153.                     #execute the cursor
  154.                     cursor.execute(postgres_insert_query, record_to_insert)
  155.                     #commit the connection
  156.                     connection.commit()
  157.                     #this counts the rows
  158.                     count = cursor.rowcount
  159.                     #this prints them if its successfull
  160.                     print (count, "Record inserted successfully into member table")
  161.            
  162.             #this handles errors if it occurs    
  163.             except(Exception, psycopg2.Error) as error:
  164.                 if(connection):
  165.                     print("Failed to insert record into the table",error)
  166.                    
  167.             finally:
  168.                 #closing database connection
  169.                 if(connection):
  170.                     cursor.close()
  171.                     connection.close()
  172.                     print("PostgresQl connection is closed")
  173.                        
  174.  
  175. #this one calls the main function
  176. if __name__ == '__main__':
  177.     #this is the main function
  178.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement