· 5 years ago · Oct 12, 2020, 05:18 PM
1#imports
2import urllib.parse
3import requests
4from tabulate import tabulate
5
6#Authentication
7ts = "1" # Default 1
8apikey = "1b5bf1a13923de31427bd01a978a7ca5" # public key
9hash = "37ffda9c9edbd186e2cf562e9e40824d" # This is an md5 hash (generated by dropping the following info in an md5 generator: 'ts+apikey+privatekey' you need to do this manually)
10
11# Main program
12
13while True: #exit the while loop if user-input for one of the values is 'q' or 'quit'
14
15 characterRequest = input("\nSearch a comic book character: ")
16 if characterRequest == 'quit' or characterRequest == 'q':
17 break
18
19 #building the url and printing it to control parameters
20 main_api = "http://gateway.marvel.com/v1/public/characters?"
21 url = main_api + urllib.parse.urlencode({"ts":ts, "apikey":apikey, "hash":hash,"nameStartsWith":characterRequest})
22
23 print("URL:" + (url))
24
25 # Making the API call request
26
27 json_data = requests.get(url).json() # Our json data
28 json_status = json_data["code"] # Our json status codes
29
30 if json_status == 200: #if API call is succesful
31
32 # Default table layout
33
34 menuTable = "="*(42+len(characterRequest))
35
36 #Use the data
37
38 print("\n" + menuTable)
39 print("You requested info for Marvel character - " + str(characterRequest))
40 print(menuTable + "\n")
41 namelist = []
42 descriptionlist = []
43
44 for each in json_data['data']['results']:
45
46 name = each['name'] # character name
47 series_count = each['series']['available'] #character appears in 'x' comic book series
48 series = each['series']['items'] #names of comic book series character appeared in
49
50 # get description of each character, if they don't have one we will create a link to marvel fandom wiki
51 description = each['description']
52
53 if description == '':
54 description = "No description found visit -> https://marvel.fandom.com/wiki/" + name.replace(" ","_")
55
56
57 # ---
58 namelist.append(name)
59 if "No description found visit" in description:
60 descriptionlist.append("/")
61 else:
62 descriptionlist.append(description)
63
64 # to convert lists to dictionary
65
66
67 tableData = []
68 for key in namelist:
69 for value in descriptionlist:
70 tableData.append([key, value ])
71 break
72 #print ("Resultant dictionary is : " + str(res))
73
74 #print('[x]' + str(name))
75 #print('[y]' + str(description))
76
77 #Using Tabulate to display general info
78 #print("\n")
79
80
81 #print('ok' + str(res))
82 print(tabulate(tableData,headers=["Name", "disc"])) #grid
83
84
85 elif json_status == 402:
86 print("************************************")
87 print("Status Code: " + str(json_status) + "; Invalid user inputs for one of the parameters.")
88 print("************************************")
89
90 elif json_status == 611:
91 print("************************************")
92 print("Status Code: " + str(json_status) + "; Missing an entry for one of the parameters.")
93 print("************************************")
94
95 else:
96 print("************************************")
97 print("status Code: " + str(json_status) + "; Following link may provide an answer to your problem.")
98 print("https://developer.marvel.com/documentation/authorization")
99 print("************************************\n")
100
101