-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalogService.py
66 lines (53 loc) · 2.29 KB
/
catalogService.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
from datetime import date, timedelta
import pandas as pd
import numpy as np
from amzService import AmzService
from pricingService import PricingService
from utils.searchTerms import searchTerms
class CatalogService:
def getSearchResults(self):
service = AmzService()
pricingService = PricingService()
df = pd.DataFrame()
asins = []
for term in searchTerms.values():
ranking = 0
result = service.getCatalogItems(term)
numResults = result['numberOfResults']
items = result['items'].values[0:1][0]
for item in items:
ranking = ranking+1
asin = item['asin']
asins.append(asin)
itemName = item['summaries'][0]['itemName']
brandName = item['summaries'][0]['brand']
record = pd.DataFrame({"Query":[term],"ASIN":[asin], "Item Name":[itemName], "Brand":[brandName], "Ranking":[ranking]})
df = pd.concat([df, record])
# remove dupes in asin list
asins = list(set(asins))
# Call pricing service to get pricing for the asins[]. Append to df before returning
pricingDf = pricingService.getPricingByAsin(asins)
#iterate through df, find price from pricingDf, add values to new dataframe
df.reset_index()
pricingDf.reset_index()
prices = []
categoryRankings = []
listingLinks = []
for index, row in df.iterrows():
record = pricingDf.loc[pricingDf['ASIN'] == row['ASIN']]
price = record['ListingPrice'][0]
categoryRank = record['SalesRanking']
prices.append(price)
categoryRankings.append(categoryRank)
#add link to listing as column
listingLinks.append('https://www.amazon.com/dp/' + row['ASIN'])
df['Listing Price'] = prices
df['Category Rank'] = categoryRankings
df['Link'] = listingLinks
df_list = []
for term in searchTerms.values():
trimmedDf = df[df['Query'] == term]
df_list.append(trimmedDf)
return df_list
def getOrganicSearchRanking(self, df, term):
return np.where(ASIN == term)