Last active
August 28, 2023 14:03
-
-
Save josephfinlayson/4e9325465ccabf654ad7 to your computer and use it in GitHub Desktop.
python script to access tesco API
This file contains 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
import urllib | |
import json | |
import datetime | |
now = datetime.datetime.now() | |
outputFilename = 'tescoproduct_' + now.strftime("%Y%m%d") + '.csv' | |
def LoginAndGetSessionKey(): | |
print("Logging into API...") | |
f = urllib.urlopen("https://secure.techfortesco.com/groceryapi/restservice.aspx?command=LOGIN&email=&password=&developerkey=0ujRTU8FlFnyfad11Ium&applicationkey=417361BD075D0FCFD502") | |
s = json.loads(f.read()) | |
f.close() | |
return s['SessionKey'] | |
def GetProductHierarchy(sessionKey): | |
print("Requesting product hierarchy...") | |
fullURL = "https://secure.techfortesco.com/groceryapi/restservice.aspx?command=LISTPRODUCTCATEGORIES&sessionkey=" + sessionKey | |
urlResult = urllib.urlopen(fullURL) | |
jsonData = urlResult.read() | |
results = json.loads(jsonData) | |
urlResult.close() | |
return results | |
def IterateThroughProductHierarchy(hierarchy, sessionKey): | |
for departments in hierarchy["Departments"]: | |
print("Department: " + departments["Name"]) | |
for aisles in departments["Aisles"]: | |
print("Aisle: " + aisles["Name"]) | |
for shelves in aisles["Shelves"]: | |
print("Shelf: " + shelves["Name"] + " - retrieving products...") | |
GetAndSaveProducts(shelves, sessionKey) | |
def GetAndSaveProducts(shelf, sessionkey): | |
global outputFilename | |
fullURL = "https://secure.techfortesco.com/groceryapi/restservice.aspx?command=LISTPRODUCTSBYCATEGORY&CATEGORY=" + shelf["Id"] +"&sessionkey=" + sessionKey | |
productsRetrievedOK_Flag = 'N' | |
while(productsRetrievedOK_Flag == 'N'): | |
try: | |
urlResult = urllib.urlopen(fullURL) | |
jsonData = urlResult.read() | |
productsRetrievedOK_Flag = 'Y' | |
except IOError: | |
productsRetrievedOK_Flag = 'N' | |
print("Problem retrieving products for shelf " + shelf["Name"] + " - trying again...") | |
products = json.loads(jsonData) | |
urlResult.close() | |
productCount = 0 | |
productFile = open(outputFilename, 'a') | |
for product in products["Products"]: | |
#print("Product Id: " + product["ProductId"] + ", " + product["Name"] + ", " + str(product["Price"])) | |
productFile.write(product["ProductId"] + "," + product["Name"] + "," + str(product["Price"]) + "\r") | |
productCount = productCount + 1 | |
productFile.close() | |
print(str(productCount) + " products saved") | |
if __name__ == '__main__': | |
print("Python Product extract script v1.1 by Nick Lansley") | |
sessionKey = LoginAndGetSessionKey() | |
hierarchy = GetProductHierarchy(sessionKey) | |
IterateThroughProductHierarchy(hierarchy, sessionKey) | |
print("Completed!") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment