-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebay-listing-matches.py
174 lines (134 loc) · 4.96 KB
/
ebay-listing-matches.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import json
from dotenv import load_dotenv
from inventree.api import InvenTreeAPI
from inventree.part import Part
from inventree.stock import StockItem
from ebaysdk.trading import Connection
load_dotenv()
def ensure_json_file(path):
if not os.path.exists(path):
with open(path, "w") as f:
json.dump([], f)
def save_data_to_json(data, path):
with open(path, 'w') as json_file:
json.dump(data, json_file, indent=4)
def load_data_from_json(path):
if os.path.exists(path):
with open(path, 'r') as json_file:
return json.load(json_file)
return []
stock_listings_path = "stock_listings.json"
active_listings_path = "active_listings.json"
ensure_json_file(stock_listings_path)
ensure_json_file(active_listings_path)
SERVER_ADDRESS = os.environ.get('INVENTREE_SERVER_ADDRESS')
MY_USERNAME = os.environ.get('INVENTREE_USERNAME')
MY_PASSWORD = os.environ.get('INVENTREE_PASSWORD')
inventree_api = InvenTreeAPI(SERVER_ADDRESS, username=MY_USERNAME, password=MY_PASSWORD, timeout=3600)
parts = Part.list(inventree_api)
parts.sort(key=lambda x: x.IPN[:11])
data = [{'url': part.link, 'ipn': part.IPN[:11]} for part in parts if part.link]
save_data_to_json(data, stock_listings_path)
ebay_api = Connection(
domain='api.ebay.com',
appid=os.environ.get('EBAY_APP_ID'),
devid=os.environ.get('EBAY_DEV_ID'),
certid=os.environ.get('EBAY_CERT_ID'),
token=os.environ.get('EBAY_TOKEN'),
config_file=None
)
page_number = 1
entries_per_page = 200
all_listings = []
while True:
response = ebay_api.execute('GetMyeBaySelling', {
'ActiveList': {
'Include': True,
'Pagination': {
'PageNumber': page_number,
'EntriesPerPage': entries_per_page
}
}
})
items = response.reply.ActiveList.ItemArray.Item
all_listings.extend(items)
pagination_result = response.reply.ActiveList.PaginationResult
total_pages = int(pagination_result.TotalNumberOfPages)
page_number += 1
if page_number > total_pages:
break
active_listings = [{'title': item.Title, 'id': item.ItemID, 'SKU': item.SKU if hasattr(item, 'SKU') else ''} for item in all_listings]
save_data_to_json(active_listings, active_listings_path)
stock_listings_data = load_data_from_json(stock_listings_path)
active_listings_data = load_data_from_json(active_listings_path)
seen_skus = set()
duplicate_skus = set()
for item in active_listings_data:
ebay_sku = item.get('SKU', '')
if ebay_sku:
if ebay_sku in seen_skus:
duplicate_skus.add(ebay_sku)
else:
seen_skus.add(ebay_sku)
if duplicate_skus:
print("Duplicates found:")
for sku in duplicate_skus:
print(sku)
else:
print("No duplicates found.\n")
stock_skus = {item['ipn'] for item in stock_listings_data}
active_skus = {item['SKU'] for item in active_listings_data if item.get('SKU')}
total_comparisons = 0
total_matches = 0
missing_matches = 0
for active_item in active_listings:
active_sku = active_item['SKU']
active_id = active_item['id']
active_title = active_item['title']
if '-' in active_sku:
main_ipn, variants = active_sku.split('-', 1)
main_ipn = main_ipn[:11]
active_skus.add(main_ipn)
for variant in variants.split('-'):
variant_length = len(variant)
ipn_with_variant = main_ipn[:-variant_length] + variant
total_comparisons += 1
active_skus.add(ipn_with_variant)
if ipn_with_variant in stock_skus:
total_matches += 1
else:
missing_matches += 1
print(f"No match found for: {active_title}")
else:
total_comparisons += 1
if active_sku in stock_skus:
total_matches += 1
else:
missing_matches += 1
print(f"No match found for: {active_title}")
print(f"\nMissing matches: {missing_matches}")
missing_skus = stock_skus - active_skus
missing_skus_sorted = sorted(missing_skus)
if missing_skus_sorted:
print("\nSKUs present in InvenTree but not active on eBay:")
for sku in missing_skus_sorted:
print(sku)
else:
print("\nNo SKUs missing on eBay compared to InvenTree.")
parts_data = [{"name": part.name, "IPN": part.IPN, "ID": part.pk, "packaging": ""} for part in parts]
stock_items = StockItem.list(inventree_api)
for item in parts_data:
part_ipn = item['IPN']
part_obj = next((part for part in parts if part.IPN == part_ipn), None)
if part_obj:
stock_items_for_part = [stock_item for stock_item in stock_items if stock_item.part == part_obj.pk]
if stock_items_for_part:
item['packaging'] = stock_items_for_part[0].packaging
missing_packaging_count = 0
print("\nParts Without Packaging:")
for part in parts_data:
if not part['packaging']:
missing_packaging_count += 1
print(f"IPN: {part['IPN']}")
print(f"\nTotal Parts Without Packaging: {missing_packaging_count}")