Skip to content

Instantly share code, notes, and snippets.

@jsphang
Created December 24, 2013 10:04
Show Gist options
  • Save jsphang/8111154 to your computer and use it in GitHub Desktop.
Save jsphang/8111154 to your computer and use it in GitHub Desktop.
import sys, csv, BeautifulSoup
from ebaysdk import finding
import pandas as pd
import sklearn
import cgi, cgitb
import matplotlib.pyplotlib as plt
import MySQLdb
#form = cgi.FieldStorage()
#str(product1) = form.getvalue('product1')
#str(name) = form.getvalue('firstname')
#print product1
#import Scipy
db = MySQLdb.connect(host="mysql.jeff.rolldc.com", # your host, usually localhost
user="jsphang", # your username
passwd="petesucks", # your password
db="_jeff_db")
cur = db.cursor()
api=finding(appid='jeffphan-0282-40b1-a351-515a77e0d718')
#api2 = finding(appid='jeffphan-0282-40b1-a351-515a77e0d718')
cur.execute("SELECT * FROM PRODUCT")
searchedForRecent = cur.fetchone()
#searchedFor = "imac G5 20"
apiResult= api.execute('findCompletedItems',{'keywords':searchedForRecent})
#api2Result = api2.execute('findCompletedItems',{'keywords':'imac G5 20'})
#apiDynamicResult = api.execute('findCompletedItems', {'keywords'': product1})
apiDict = apiResult.response_dict()
ebay_dict = apiResult.response_dict()
ebay_soup = apiResult.response_soup()
ebay_str = apiResult.response_content()
#rebuild= {}
listOfKeysNeeded = ['itemId', 'isMultiVariationListing', 'topRatedListing', 'globalId', 'title', 'country', 'listingInfo', 'shippingInfo', 'galleryURL', 'autoPay', 'location', 'postalCode', 'returnsAccepted', 'viewItemURL', 'price', 'paymentMethod', 'primaryCategory', 'condition', 'productId']
#this is iterable....
actualLength=0
#lets start with the imag g5 and see what we can value it at/get a dstribution of values (imaging will be done via python statistics package)
#imacAPI = (finding(appid='jeffphan-d75e-4773-88d8-4b00bb0e8834'))
#resultImac = api2.response_dict()
#print(apiResult.searchResult.item[0])
length=len(apiDict.searchResult.item)
#print length
#print(type(length))
#priceSold=[]
titleSold=[""]*length
titleSoldActual=[""]*length
sumPrice=0
priceSold=[0]*length
priceSoldActual=[0]*length
#print(type(apiDict.searchResult.item))
#sumPrice=0
#print(apiDict.searchResult.item[0]['sellingStatus']['sellingState'])
#print((apiDict.searchResult.item[1]['sellingStatus']['sellingState']['value']))
i=0
x=0
g=0
#Obviously for production we can't rely on something like this - specific to macs. It has to "Learn";
#Or, with the pandas, cut out outliers of a list of numbers.
while i<length:
#print[i]
if apiDict.searchResult.item[i]['sellingStatus']['sellingState']['value'] == "EndedWithSales":
#print(length)
titleSold[i]=apiDict.searchResult.item[i]['title']['value']
for x in range(4):
if "not working" in apiDict.searchResult.item[i]['title'].value:
i=i+1
if "assembly" in apiDict.searchResult.item[i]['title'].value:
i=i+1
if "logic board" in apiDict.searchResult.item[i]['title'].value:
i=i+1
if "FOR PARTS" in apiDict.searchResult.item[i]['title'].value:
i=i+1
if "for parts" in apiDict.searchResult.item[i]['title'].value:
i=i+1
#if apiDict.searchResult.item[i]['sellingStatus']['convertedCurrentPrice'].value>'450':
if apiDict.searchResult.item[i].primaryCategory.categoryName == 'Apple Desktops & All-In-Ones':
priceSold[i]=float(apiDict.searchResult.item[i]['sellingStatus']['convertedCurrentPrice'].value)
priceSoldActual[g] = float(apiDict.searchResult.item[i]['sellingStatus']['convertedCurrentPrice'].value)
titleSoldActual[g] = titleSold[i]
#print(priceSold[i])
actualLength = actualLength+1
g=g+1
print(apiDict.searchResult.item[i])
if i==length:
break
#if i!=0:
# print["moving average"]
# print(sumPrice/i)
#with open('ebayCSV.csv', 'wb') as ebayCSV:
# writer = csv.writer(ebayCSV)
# writer.writerow(listOfKeysNeeded)
#writer.writerow([apiDict.searchResult.item[i]['itemId'][value],apiDict.searchResult.item[i]['isMultiVariationListing']['value'],apiDict.searchResult.item[i]['topRatedListing']['value'],apiDict.searchResult.item[i]['globalId']['value'],apiDict.searchResult.item[i]['title']['value']])
#writer.writerow(apiDict.searchResult.item[i]['topRatedListing'].value)
#writer.writerow(apiDict.searchResult.item[i]['globalId'].value)
#writer.writerow(a)
#writer.writerow(apiDict.searchResult.item[i]['country'].value)
#writer.writerow(apiDict.searchResult.item[i]['listingInfo'].value)
#writer.writerow(apiDict.searchResult.item[i]['shippingInfo'].value)
#writer.writerow(apiDict.searchResult.item[i]['galleryURL'].value)
#writer.writerow(apiDict.searchResult.item[i]['autoPay'].value)
#writer.writerow(apiDict.searchResult.item[i]['location'].value)
#writer.writerow(apiDict.searchResult.item[i]['postalCode'].value)
#writer.writerow(apiDict.searchResult.item[i]['returnsAccepted'].value)
#writer.writerow(apiDict.searchResult.item[i]['viewItemURL'].value)
#writer.writerow(apiDict.searchResult.item[i]['sellingStatus']['convertedCurrentPrice'].value)
#writer.writerow(apiDict.searchResult.item[i]['paymentMethod'].value)
#writer.writerow(apiDict.searchResult.item[i]['primaryCategory'].value)
#writer.writerow(apiDict.searchResult.item[i]['condition'].value)
#writer.writerow(apiDict.searchResult.item[i]['productId'].value)
i=i+1
for g in range(0,actualLength):
print(titleSoldActual[g])
print(priceSoldActual[g])
sumPrice=sumPrice+priceSoldActual[g]
#sumPrice = sum(priceSold[i])
#ebay_soup portion - get this into something that i can analyze
Average = sumPrice/actualLength
print("overAll average for "+searchedFor + " is ")
print(Average)
plt.hist(priceActual[g])
#overallAverage = "Overall average"
#print(Average)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment