-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebay.py
119 lines (90 loc) · 3.72 KB
/
ebay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json
import urllib.parse
import urllib.request
import base64
import requests
ebay_api_token = None
BASE_EBAY_URL = 'https://api.ebay.com/buy/browse/v1/item_summary'
def get_access_token() -> None:
'''Makes a grant request and gets the access token.'''
response = None
try:
request_body = urllib.parse.urlencode({'grant_type':'client_credentials',
'scope':'https://api.ebay.com/oauth/api_scope'}).encode()
b = base64.b64encode('GraceChi-Wishr-PRD-9c888fc41-58b902d4:PRD-c888fc4121b6-34ae-4ee4-b239-5694'.encode('utf-8')).decode('utf-8')
request = urllib.request.Request(
url = 'https://api.ebay.com/identity/v1/oauth2/token',
headers = {'Content-Type':'application/x-www-form-urlencoded',
'Authorization':'Basic '+b},
data = request_body,
method = 'POST')
response = urllib.request.urlopen(request)
json_text = response.read().decode(encoding = 'utf-8')
json_dict = json.loads(json_text)
return json_dict['access_token']
finally:
if response != None:
response.close()
def build_search_url(search_query: str, limit: int) -> str:
'''This function takes a search query and the maximum number of results
to display, and builds and returns a URL that can be used to ask the
eBay item_summary API for information about products matching the search
request.
'''
query_parameters = [
('q', search_query), ('limit', str(limit)),
]
return f'{BASE_EBAY_URL}/search?{urllib.parse.urlencode(query_parameters)}'
assert build_search_url('strawberry', 3) == 'https://api.ebay.com/buy/browse/v1/item_summary/search?q=strawberry&limit=3'
def get_result(url: str) -> dict:
'''This function takes a URL and returns a Python dictionary representing
the parsed JSON response.
'''
response = None
try:
request = urllib.request.Request(
url,
headers = {"Authorization":f'Bearer {ebay_api_token}',
"X-EBAY-C-MARKETPLACE-ID":"EBAY_US"})
response = urllib.request.urlopen(request)
json_text = response.read().decode(encoding = 'utf-8')
return json.loads(json_text)
finally:
if response != None:
response.close()
def print_title_and_description(search_result: dict) -> None:
'''This function takes a parsed JSON response from the eBay item_summary API's
search request and prints the title, price, and image url of all of the products
in the response.
'''
for item in search_result['itemSummaries']:
print(item['title'])
## print(item['currentBidPrice']['value'])
print(item['image']['imageUrl'])
print()
def run(query:str) -> None:
## search_query = input('Query: ')
## while True:
## try:
## result = get_result(build_search_url('strawberry', 3))
## print_title_and_description(result)
## break
## except:
## ebay_api_token = get_access_token()
ebay_api_token = get_access_token()
# print(ebay_api_token)
# result = get_result(build_search_url('strawberry', 3))
# print_title_and_description(result)
url = "https://api.ebay.com/buy/browse/v1/item_summary/search"
querystring = {"q":query, "limit": 3}
payload = ""
headers = {
"User-Agent": "insomnia/8.4.0",
"Authorization": "Bearer "+ebay_api_token
}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
# with open('result.json', 'w') as f:
# json.dump(response.json(), f)
return response.json()
if __name__ == '__main__':
run('')