-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliexpress_cart.py
96 lines (85 loc) · 4.62 KB
/
aliexpress_cart.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 26 03:01:02 2019
@author: Dictador
"""
from __future__ import division
import re, os, sys, errno, glob
import json
import requests
from bs4 import BeautifulSoup
from time import sleep
import datetime as dt
#import HTML
# Login to your ali account first. Make sure webpage is in english, dollars (or any other language, currency, but keep the things consistent each time you save the json)
# The link below placed in your webbrowser will download your ali cart in json format.
# Save it from your webbrowser to a local file under a name ali_cart_2020_12_04.json
#
# https://shoppingcart.aliexpress.com/api/1.0/cart/items.do?currentPage=0&uniqueId=1
file_names = []
for name in glob.glob('ali_cart_*.json'): # saved files are named 'ali_cart_2020_11_28.json'
print(name)
file_names.append(name)
file_names = sorted(file_names)
files_n =len(file_names)
with open(file_names[-1], encoding="utf8") as json_file:
data = json.load(json_file)
#products_list = [{
# 'productId':'',
# 'productKey':'',
# 'itemId':'',
# 'detailUrl':'',
# 'title':'',
# 'price':'',
# 'currencyCode':'',
# 'priceMin':'',
# 'priceMax':'',
# 'priceAvg':'',
# 'coupons':''
# }]
products_list = []
price_history={0:[0]}
for store in data['stores']: # we fill product_list[] with data from the most recent data file
#print(store)
for item in store["storeList"]:
for product in item["products"]:
price_history.update({product['itemId']:[product['cost']['price']['amount']]})
products_list.append( {
'productId':product['productId'],
'productKey':product['productKey'],
'itemId':product['itemId'],
'detailUrl':product['detailUrl'],
'title':product['title'],
'price':product['cost']['price']['amount'],
'currencyCode':product['cost']['price']['currencyCode'],
'priceMin':product['cost']['price']['amount'],
'priceMax':product['cost']['price']['amount'],
'priceAvg':product['cost']['price']['amount']
} )
#products_list['coupons'].append(
#print(n,' ',product['title'][0:40],' ',str(product['cost']['price']['amount']),' ',product['cost']['price']['currencyCode'])
for n in range(0,files_n-1): # how many files are stored on the disk, -1 because we initialized above the memory with the data from tha last file
with open(file_names[n], encoding="utf8") as json_file: # open each file starting from the last one
data = json.load(json_file)
for i,x in enumerate(products_list):
for store in data['stores']:
for item in store["storeList"]:
for product in item["products"]:
if x['itemId'] == product['itemId']:
products_list[i]['priceMin'] = min(product['cost']['price']['amount'], products_list[i]['priceMin'])
products_list[i]['priceMax'] = max(product['cost']['price']['amount'], products_list[i]['priceMax'])
products_list[i]['priceAvg'] = ((products_list[i]['priceAvg']*100) + (product['cost']['price']['amount']*100))/200
price_history[product['itemId']].append(product['cost']['price']['amount'])
#for n in range(0,files_n-1):
# print(n+1,file_names[n])
# This section prints the analysis results on the screen.
# Products priced higher than the lowest price ever recorded are printed in black
# Products priced at the lowest price ever recorded are printed in red
# Products priced lower than the lowest price ever recorded are printed in purple
for n,product in enumerate(products_list):
if product['price'] == product['priceMin']:
print(n+1, '\t\33[31;91m', product['title'][0:30], '\t', product['price'], ' ' ,product['currencyCode'], '\t' ,product['priceAvg'], '\t' ,product['priceMin'],'\33[0m')
elif (product['price'] < product['priceMin']):
print(n+1, '\t\33[35;91m', product['title'][0:30], '\t', product['price'], ' ' ,product['currencyCode'], '\t' ,product['priceAvg'], '\t' ,product['priceMin'],'\33[0m')
else:
print(n+1, '\t', product['title'][0:30], '\t', product['price'], ' ' ,product['currencyCode'], '\t' ,product['priceAvg'], '\t' ,product['priceMin'])