-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllegroApi_User.py
138 lines (117 loc) · 6.03 KB
/
AllegroApi_User.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Przed uzyciem na nowym kliencie wyczyscic pliki textowe access_token i expiration_date
import requests
from http.server import BaseHTTPRequestHandler, HTTPServer
import webbrowser
import datetime
import sqlite3
import slugify
class AllegroAPI():
access_token_did_expire = True
client_id = None
client_secret = None
DEFAULT_OAUTH_URL = 'https://allegro.pl/auth/oauth'
DEFAULT_REDIRECT_URI = 'http://localhost:8000'
api_url = 'https://api.allegro.pl/'
def __init__(self, client_id, client_secret, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client_id = client_id
self.client_secret = client_secret
# Authorizationon ####################################################################################
def get_access_code(self):
oauth_url = self.DEFAULT_OAUTH_URL
redirect_uri = self.DEFAULT_REDIRECT_URI
client_id = self.client_id
client_secret = self.client_secret
auth_url = '{}/authorize' \
'?response_type=code' \
'&client_id={}' \
'&api-key={}' \
'&redirect_uri={}'.format(oauth_url, client_id, client_secret, redirect_uri)
parsed_redirect_uri = requests.utils.urlparse(redirect_uri)
server_address = parsed_redirect_uri.hostname, parsed_redirect_uri.port
class AllegroAuthHandler(BaseHTTPRequestHandler):
def __init__(self, request, address, server):
super().__init__(request, address, server)
def do_GET(self):
self.send_response(200, 'OK')
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.server.path = self.path
self.server.access_code = self.path.rsplit('?code=', 1)[-1]
print('server_address:', server_address)
webbrowser.open(auth_url)
httpd = HTTPServer(server_address, AllegroAuthHandler)
print('Waiting for response with access_code from Allegro.pl (user authorization in progress)...')
httpd.handle_request()
httpd.server_close()
_access_code = httpd.access_code
print('Got an authorize code: ', _access_code)
return _access_code
def perform_auth(self, access_code):
redirect_uri= self.DEFAULT_REDIRECT_URI
oauth_url= self.DEFAULT_OAUTH_URL
client_id = self.client_id
client_secret = self.client_secret
token_url = oauth_url + '/token'
access_token_data = {'grant_type': 'authorization_code', 'code': access_code, 'redirect_uri': redirect_uri}
token_result = requests.post(url=token_url, auth=requests.auth.HTTPBasicAuth(client_id, client_secret), data=access_token_data)
token_object = token_result.json()
time_now = datetime.datetime.now()
token_expires_in = token_object['expires_in']
expires = time_now + datetime.timedelta(seconds=token_expires_in)
with open('access_token', 'w') as f:
f.write(token_object['access_token'])
with open('expiration_date', 'w') as f:
f.write(str(expires))
self.access_token_did_expire = expires < time_now
return True
def get_access_token(self):
with open('access_token') as f:
token = f.readline()
with open('expiration_date') as f:
expires = datetime.datetime.strptime(f.readline(), "%Y-%m-%d %H:%M:%S.%f")
time_now = datetime.datetime.now()
if expires < time_now or token == None:
self.perform_auth(self.get_access_code())
return self.get_access_token()
return token
# Queries #########################################################################################
def get_total_count(self):
headers = {'Authorization': f'Bearer {self.get_access_token()}', 'Accept': 'application/vnd.allegro.public.v1+json'}
r = requests.get(self.api_url + 'sale/offers', headers=headers)
res = r.json()
return res['totalCount']
def get_all_offers(self):
headers = {'Authorization': f'Bearer {self.get_access_token()}', 'Accept': 'application/vnd.allegro.public.v1+json'}
all_offers = set()
for x in range(self.get_total_count()//1000+1):
payload = {'limit': 1000, 'offset':x*1000, 'publication.status': 'ACTIVE'}
r = requests.get(self.api_url + 'sale/offers', params=payload, headers=headers)
res = r.json()
all_offers.update({(item['id'], item['name'], item['primaryImage']['url']) for item in res['offers']})
return all_offers
def get_all_offers_ids(self):
headers = {'Authorization': f'Bearer {self.get_access_token()}', 'Accept': 'application/vnd.allegro.public.v1+json'}
all_offers_ids = set()
for x in range(self.get_total_count()//1000+1):
payload = {'limit': 1000, 'offset':x*1000, 'publication.status': 'ACTIVE'}
r = requests.get(self.api_url + 'sale/offers', params=payload, headers=headers)
res = r.json()
all_offers_ids.update({item['id'] for item in res['offers']})
return all_offers_ids
def get_offer_fields(self, offer_id):
headers = {'Authorization': f'Bearer {self.get_access_token()}', 'Accept': 'application/vnd.allegro.public.v1+json'}
r = requests.get(self.api_url + 'sale/offers/' + offer_id, headers=headers)
res = r.json()
try:
if res['errors']: return 'error'
except: pass
id = res['id']
name = res['name']
slug = slugify.slugify(name)
description = res['description']['sections'][0]['items'][0]['content']
images = [x['url'] for x in res['images']]
price = float(res['sellingMode']['price']['amount'])
create_date = res['createdAt'].replace('T', ' ').replace('Z', '')
offer = {'id': id, 'name': name, 'slug': slug, 'description': description, 'images': images, 'price': price, 'create_date': create_date}
return offer