-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebay_items.py
executable file
·171 lines (163 loc) · 7.26 KB
/
ebay_items.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
import json
import logging
from ebay.utils import set_config_file
from ebay.utils import get_config_store
from ebay.finding import findItemsByKeywords
from ebay.shopping import GetMultipleItems
import settings
class EbayItemError(Exception):
pass
class EbayItems(object):
@staticmethod
def item_attr(data, attr_array, default=''):
result_temp = data
result = None
try:
for attr in attr_array:
result_temp = result_temp[attr]
result = result_temp
except Exception as e:
# logging.info('no such attribute in item: ' + str(e))
return default
else:
return result
@classmethod
def search_items(cls,
search_term=None,
category=None,
buyitnow=None,
condition=None,
max_price=None,
Zip=None):
logging.info(str(locals()))
set_config_file(settings.EBAY_API_SETTINGS_FILE)
if not search_term:
return []
if not Zip:
config = get_config_store()
Zip = config.get("settings", "zip")
logging.info(Zip)
itemFilter = []
if max_price:
itemFilter = [{'name': 'MaxPrice',
'value': str(float(max_price)),
'paramName': 'Currency',
'paramValue': 'USD'}]
if condition:
itemFilter.append({'name': 'Condition',
'value': condition})
outputSelector = ('PictureURLSuperSize', 'SellerInfo')
json_str = findItemsByKeywords(keywords=search_term,
buyerPostalCode=Zip,
itemFilter=itemFilter,
outputSelector=outputSelector,
aspectFilter=[],
domainFilter=[])
# logging.info(json_str)
data = json.loads(json_str)
items_count = int(cls.item_attr(data, ['findItemsByKeywordsResponse', 0,
'searchResult', 0, '@count'], '0'))
if items_count == 0:
return []
items = cls.item_attr(data, ['findItemsByKeywordsResponse', 0,
'searchResult', 0, 'item'], [])
data_items = []
for item in items:
try:
processed_item = cls.process_item(item,
search_term,
category,
buyitnow,
max_price)
except EbayItemError:
processed_item = None
if processed_item:
data_items.append(processed_item)
return data_items
@classmethod
def process_item(cls, item, search_term, category, buyitnow, max_price):
#First check 'Buy It Now' price, if it doesn't exist, get current price
if cls.item_attr(item, ["listingInfo", 0,
'buyItNowAvailable', 0]) == 'true':
item_price = cls.item_attr(item, ["listingInfo", 0, "buyItNowPrice", 0,
"__value__"])
item_buyitnow = True
else:
#if buyitnow option enabled - continue the cycle
if buyitnow:
return None
item_buyitnow = False
item_price = cls.item_attr(item, ['sellingStatus', 0, 'currentPrice', 0,
'__value__'], 'no')
if item_price == 'no':
return None
if max_price:
if float(item_price) > float(max_price):
return None
item_bestOffer = cls.item_attr(item, ["listingInfo", 0, 'bestOfferEnabled', 0], False)
item_bestOffer = True if (item_bestOffer == "true") else False
if(cls.item_attr(item, ["listingInfo", 0, 'listingType', 0]) == "StoreInventory") or\
(cls.item_attr(item, ["listingInfo", 0, 'listingType', 0]) == "FixedPrice"):
item_buyitnow = True
item_openbidding = False
#AuctionWithBIN
if(cls.item_attr(item, ["listingInfo", 0, 'listingType', 0]) == "AuctionWithBIN"):
item_buyitnow = True
item_openbidding = True
#Auction
if(cls.item_attr(item, ["listingInfo", 0, 'listingType', 0]) == "Auction"):
item_openbidding = True
item_condition = cls.item_attr(item, ['condition', 0, 'conditionDisplayName', 0])
# if condition:
# if condition.lower() not in item_condition.lower():
# continue
item = {
'itemID': cls.item_attr(item, ['itemId', 0]),
'search_term': search_term,
'category': category,
'condition': item_condition,
'description': cls.item_attr(item, ['title', 0]),
'price': float(item_price),
'max_price': float(max_price),
'buyitnow': item_buyitnow,
'bestoffer': item_bestOffer,
'openbidding': item_openbidding,
'percent': int(round((float(max_price) - float(item_price))
/ float(item_price)*100)),
'shipping': float(cls.item_attr(item, ['shippingInfo', 0,
'shippingServiceCost', 0,
'__value__'], '0')),
'details': cls.item_attr(item, ['itemId', 0]),
'photos': cls.item_attr(item, ['galleryURL', 0]),
'viewitem': cls.item_attr(item, ['viewItemURL', 0]),
'buybid': cls.item_attr(item, ['viewItemURL', 0]),
'pictureURLSuperSize': cls.item_attr(item,
['pictureURLSuperSize',
0]),
'sellerUserName': cls.item_attr(item, ['sellerInfo', 0,
'sellerUserName', 0]),
'feedbackScore': float(cls.item_attr(item, ['sellerInfo', 0,
'feedbackScore', 0])),
'positiveFeedbackPercent': float(cls.item_attr(item, ['sellerInfo',
0,
'positiveFeedbackPercent',
0]))
}
return item
@staticmethod
def quantity(itemID):
result = 'no info'
json_str = GetMultipleItems(item_id=itemID)
# logging.info(json_str)
data = json.loads(json_str)
# logging.info(data)
for item in data['Item']:
try:
quantity = item["Quantity"]
quantity_sold = item["QuantitySold"]
quantity_available = quantity - quantity_sold
itemID = item["ItemID"]
result = quantity_available
except Exception as e:
logging.error(e)
return result