lolamontes69

ebaypredict.py remade for Ch8 Programming Collective Intel..

Aug 10th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.96 KB | None | 0 0
  1. """  I couldn't get ebaypredict.py to return any data so I wrote a version that uses JSON.
  2.     I haven't implemented getCategory(), but it's easy enough to add later :)
  3. """
  4. import time
  5. import re
  6. import urllib as urllib
  7. import json                        # we are after all importing json
  8. from time import sleep             # sleep to slow the requests to the server
  9.  
  10. appKey = 'Your__Ebay__AppKey__Here'
  11.  
  12. def doSearch(query,pages=1):
  13.     global appkey
  14.     query1=query.replace(' ',"%20")
  15.     results=[]
  16.     for page in range(pages):
  17.         try:
  18.             url = "http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords"
  19.             url += "&SERVICE-VERSION=1.0.0&SECURITY-APPNAME="+appKey
  20.             url += "&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&keywords="+query1
  21.             url += "&paginationInput.entriesPerPage=100&paginationOutput.pageNumber="+str(page+1)
  22.  
  23.             dic = {}
  24.             conn = urllib.urlopen(url)
  25.             for line in conn:
  26.                 dic = json.loads(line)     # load json into dic
  27.  
  28.             now = time.time()    # (current time in seconds)
  29.             for a in range(len(dic['findItemsByKeywordsResponse'][0]['searchResult'][0]['item'])):
  30.                 itemId = dic['findItemsByKeywordsResponse'][0]['searchResult'][0]['item'][a]['itemId'][0]
  31.                 itemTitle = dic['findItemsByKeywordsResponse'][0]['searchResult'][0]['item'][a]['title'][0]
  32.                 itemPrice =  dic['findItemsByKeywordsResponse'][0]['searchResult'][0]['item'][a]['sellingStatus'][0]['convertedCurrentPrice'][0]['__value__']
  33.                 timeleft = dic['findItemsByKeywordsResponse'][0]['searchResult'][0]['item'][a]['sellingStatus'][0]['timeLeft'][0]
  34.                 d = re.split('(\d+)',timeleft)
  35.                 timeleft1 = (int(d[1])*24*60*60)+(int(d[3])*60*60)+(int(d[5])*60)+(int(d[7]))     # Convert to seconds
  36.                 endtime = now + timeleft1
  37.                 itemEnds = time.ctime(endtime)
  38.                 results.append((itemId,itemTitle,itemPrice,itemEnds))
  39.         except: print "Unable to retrieve page",page+1
  40.     return results
  41.  
  42. def getItem(itemId):
  43.     global appkey
  44.     try:
  45.         url = "http://open.api.ebay.com/shopping?callname=GetSingleItem&responseencoding=JSON&appid=" + appKey
  46.         url += "&version=515&ItemID=" + itemId
  47.         url += "&IncludeSelector=ItemSpecifics,Details"
  48.  
  49.         dic = {}
  50.         conn = urllib.urlopen(url)
  51.         for line in conn:              # comes as a unicode string
  52.             dic = json.loads(line)     # load json into dic
  53.  
  54.         result={}
  55.         result['title']=dic['Item']['Title']
  56.         result['price']=dic['Item']['CurrentPrice']['Value']
  57.         result['bids']=dic['Item']['BidCount']
  58.         result['feedback']=dic['Item']['Seller']['FeedbackScore']
  59.         dic2={}
  60.         for a in dic['Item']['ItemSpecifics']['NameValueList']:
  61.             dic2[a['Name']]=a['Value'][0]
  62.         result['attributes']=dic2
  63.         return result
  64.     except: print "Unable to retrieve",itemId
  65.  
  66. def makeLaptopDataset():
  67.     searchResults=doSearch('laptop',7)
  68.     result=[]
  69.     for a in searchResults:
  70.         sleep(0.7)
  71.         print '.',
  72.         try:
  73.             item=getItem(a[0])
  74.             hdd = item['attributes']['Hard Drive Capacity']
  75.             d = re.split('(\d+)',hdd)
  76.             mem = item['attributes']['Memory']
  77.             e = re.split('(\d+)',mem)
  78.             scr = item['attributes']['Screen Size']
  79.             scr = scr.replace('"','')
  80.             pro = item['attributes']['Processor Speed']
  81.             pro = pro.replace('GHz','')
  82.             data=(float(e[1]),float(pro),float(scr),float(d[1]),float(item['feedback']))
  83.             entry={'input':data,'result':float(item['price'])}
  84.             result.append(entry)
  85.             if len(result)==300: return result
  86.         except:
  87.             try:
  88.                 print item['title']+' failed'   # We want a completed dataset
  89.             except: print 'item failed'
  90.     return result
  91.  
  92.  
  93. """
  94. Example Usage
  95. *************
  96. import ebayjson1 as ebayjson
  97. ebayjson.doSearch('laptop notebook')
  98. ebayjson.getItem('321181609967')
  99. data = ebayjson.makeLaptopDataset()
  100. import numpredict2 as numpredict
  101. numpredict.knnestimate(data,(2.0,2.5,14.1,500,1000))
  102. sdata=numpredict.rescale(data,[1,1,1,1.5,0])
  103. numpredict.weightedknn(sdata,(4.0, 1.3999999999999999, 11.6, 500.0, 158692.0),weightf=numpredict.inverseweight)
  104.  
  105. def knninverse(d,v):
  106.    return numpredict.weightedknn(d,v,weightf=numpredict.inverseweight)
  107. costf=numpredict.createcostfunction(knninverse,sdata)
  108. optimization.geneticoptimize(knninverse,costf,popsize=5,step=1,elite=0.2,maxiter=20)
  109.  
  110.  
  111. -------------------------------------------------------------------------------
  112. makeLaptopDataset()     example output len=300
  113.  
  114. Notes: the 'feedback' score is a bit OTT examples 158693.0, 21.0, 1266.0
  115.       the price is the current price converted into Dollar$
  116.  
  117. [{'input': (4.0, 2.3999999999999999, 15.6, 500.0, 5664.0), 'result': 288.0}, {'input': (4.0, 1.3999999999999999, 11.6, 500.0, 158692.0), 'result': 369.99000000000001}, {'input': (4.0, 1.0, 15.6, 320.0, 48646.0), 'result': 168.5}, {'input': (4.0, 2.7000000000000002, 13.300000000000001, 500.0, 52.0), 'result': 495.0}, {'input': (4.0, 2.3999999999999999, 14.1, 250.0, 69396.0), 'result': 350.0}, {'input': (2.0, 2.2000000000000002, 14.1, 80.0, 13347.0), 'result': 189.94999999999999}, {'input': (6.0, 2.7000000000000002, 17.300000000000001, 640.0, 5664.0), 'result': 379.99000000000001}, {'input': (2.0, 2.2599999999999998, 13.300000000000001, 160.0, 407.0), 'result': 400.0}, {'input': (4.0, 2.2999999999999998, 13.300000000000001, 320.0, 4621.0), 'result': 641.0}, {'input': (4.0, 2.5299999999999998, 17.0, 500.0, 4621.0), 'result': 909.02999999999997}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 19690.0), 'result': 394.99000000000001}, {'input': (2.0, 2.2599999999999998, 14.1, 160.0, 21.0), 'result': 124.98999999999999}, {'input': (4.0, 1.3, 15.6, 320.0, 19690.0), 'result': 334.99000000000001}, {'input': (1.0, 1.8600000000000001, 14.0, 40.0, 619.0), 'result': 99.989999999999995}, {'input': (4.0, 1.5, 15.6, 320.0, 19690.0), 'result': 324.99000000000001}, {'input': (2.0, 1.7, 15.6, 320.0, 5828.0), 'result': 309.94999999999999}, {'input': (4.0, 2.5, 17.300000000000001, 500.0, 196.0), 'result': 300.0}, {'input': (6.0, 3.1000000000000001, 15.6, 750.0, 5664.0), 'result': 398.88999999999999}, {'input': (3.0, 2.1000000000000001, 15.4, 60.0, 2637.0), 'result': 159.0}, {'input': (3.0, 2.5299999999999998, 14.1, 250.0, 69396.0), 'result': 147.5}, {'input': (2.0, 2.7999999999999998, 14.1, 160.0, 2428.0), 'result': 182.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 158693.0), 'result': 304.94999999999999}, {'input': (4.0, 1.7, 11.6, 500.0, 4494.0), 'result': 299.99000000000001}, {'input': (512.0, 1.8300000000000001, 14.1, 60.0, 1266.0), 'result': 129.99000000000001}, {'input': (4.0, 2.3999999999999999, 14.1, 160.0, 29729.0), 'result': 289.0}, {'input': (2.0, 1.8999999999999999, 15.4, 120.0, 2514.0), 'result': 159.99000000000001}, {'input': (4.0, 2.7999999999999998, 15.4, 250.0, 69397.0), 'result': 229.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 1368.0), 'result': 329.0}, {'input': (4.0, 2.7999999999999998, 17.0, 160.0, 93593.0), 'result': 329.99000000000001}, {'input': (1.0, 2.2000000000000002, 13.300000000000001, 160.0, 16.0), 'result': 100.0}, {'input': (4.0, 1.3999999999999999, 11.6, 500.0, 5664.0), 'result': 339.99000000000001}, {'input': (4.0, 2.5299999999999998, 15.6, 500.0, 13018.0), 'result': 284.99000000000001}, {'input': (1.0, 1.6000000000000001, 10.1, 160.0, 2.0), 'result': 112.5}, {'input': (4.0, 2.7999999999999998, 14.1, 250.0, 69397.0), 'result': 165.09999999999999}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 9801.0), 'result': 349.0}, {'input': (8.0, 2.2999999999999998, 14.0, 750.0, 1507.0), 'result': 1000.0}, {'input': (4.0, 2.6600000000000001, 13.300000000000001, 250.0, 69397.0), 'result': 299.99000000000001}, {'input': (2.0, 2.2000000000000002, 14.1, 120.0, 13347.0), 'result': 189.0}, {'input': (4.0, 2.5299999999999998, 15.6, 320.0, 1921.0), 'result': 249.99000000000001}, {'input': (3.0, 2.3999999999999999, 14.0, 250.0, 13347.0), 'result': 319.0}, {'input': (4.0, 2.6600000000000001, 14.1, 250.0, 434.0), 'result': 251.5}, {'input': (4.0, 2.7999999999999998, 14.1, 320.0, 15621.0), 'result': 259.25}, {'input': (4.0, 2.7999999999999998, 14.1, 250.0, 434.0), 'result': 145.0}, {'input': (4.0, 1.5, 13.300000000000001, 500.0, 35046.0), 'result': 359.99000000000001}, {'input': (4.0, 2.7999999999999998, 14.1, 320.0, 15621.0), 'result': 259.0}, {'input': (4.0, 1.3999999999999999, 13.300000000000001, 500.0, 773.0), 'result': 445.94999999999999}, {'input': (8.0, 1.73, 17.300000000000001, 500.0, 39.0), 'result': 550.0}, {'input': (4.0, 2.5299999999999998, 14.1, 160.0, 15621.0), 'result': 189.25}, {'input': (4.0, 2.2599999999999998, 15.4, 160.0, 1139.0), 'result': 73.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 158693.0), 'result': 294.94999999999999}, {'input': (4.0, 2.5299999999999998, 13.300000000000001, 250.0, 15621.0), 'result': 298.0}, {'input': (1.0, 1.6000000000000001, 14.1, 40.0, 6773.0), 'result': 117.0}, {'input': (2.0, 1.73, 15.4, 100.0, 199.0), 'result': 58.0}, {'input': (4.0, 2.5299999999999998, 14.0, 320.0, 15621.0), 'result': 269.25}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 5664.0), 'result': 288.0}, {'input': (4.0, 1.3999999999999999, 11.6, 500.0, 158693.0), 'result': 369.99000000000001}, {'input': (4.0, 1.0, 15.6, 320.0, 48646.0), 'result': 177.50999999999999}, {'input': (4.0, 2.7000000000000002, 13.300000000000001, 500.0, 52.0), 'result': 561.0}, {'input': (4.0, 2.3999999999999999, 14.1, 250.0, 69397.0), 'result': 350.0}, {'input': (2.0, 2.2000000000000002, 14.1, 80.0, 13347.0), 'result': 189.94999999999999}, {'input': (6.0, 2.7000000000000002, 17.300000000000001, 640.0, 5664.0), 'result': 379.99000000000001}, {'input': (2.0, 2.2599999999999998, 13.300000000000001, 160.0, 407.0), 'result': 461.0}, {'input': (4.0, 2.2999999999999998, 13.300000000000001, 320.0, 4622.0), 'result': 671.0}, {'input': (4.0, 2.5299999999999998, 17.0, 500.0, 4622.0), 'result': 909.02999999999997}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 19690.0), 'result': 394.99000000000001}, {'input': (2.0, 2.2599999999999998, 14.1, 160.0, 21.0), 'result': 124.98999999999999}, {'input': (4.0, 1.3, 15.6, 320.0, 19690.0), 'result': 334.99000000000001}, {'input': (1.0, 1.8600000000000001, 14.0, 40.0, 619.0), 'result': 99.989999999999995}, {'input': (4.0, 1.5, 15.6, 320.0, 19690.0), 'result': 324.99000000000001}, {'input': (2.0, 1.7, 15.6, 320.0, 5828.0), 'result': 309.94999999999999}, {'input': (4.0, 2.5, 17.300000000000001, 500.0, 196.0), 'result': 300.0}, {'input': (6.0, 3.1000000000000001, 15.6, 750.0, 5664.0), 'result': 398.88999999999999}, {'input': (3.0, 2.1000000000000001, 15.4, 60.0, 2637.0), 'result': 159.0}, {'input': (3.0, 2.5299999999999998, 14.1, 250.0, 69397.0), 'result': 175.0}, {'input': (2.0, 2.7999999999999998, 14.1, 160.0, 2428.0), 'result': 182.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 158693.0), 'result': 304.94999999999999}, {'input': (4.0, 1.7, 11.6, 500.0, 4494.0), 'result': 299.99000000000001}, {'input': (512.0, 1.8300000000000001, 14.1, 60.0, 1266.0), 'result': 129.99000000000001}, {'input': (4.0, 2.3999999999999999, 14.1, 160.0, 29730.0), 'result': 289.0}, {'input': (2.0, 1.8999999999999999, 15.4, 120.0, 2514.0), 'result': 159.99000000000001}, {'input': (4.0, 2.7999999999999998, 15.4, 250.0, 69397.0), 'result': 229.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 1368.0), 'result': 329.0}, {'input': (4.0, 2.7999999999999998, 17.0, 160.0, 93593.0), 'result': 329.99000000000001}, {'input': (1.0, 2.2000000000000002, 13.300000000000001, 160.0, 16.0), 'result': 145.49000000000001}, {'input': (4.0, 1.3999999999999999, 11.6, 500.0, 5664.0), 'result': 339.99000000000001}, {'input': (4.0, 2.5299999999999998, 15.6, 500.0, 13018.0), 'result': 284.99000000000001}, {'input': (1.0, 1.6000000000000001, 10.1, 160.0, 2.0), 'result': 112.5}, {'input': (4.0, 2.7999999999999998, 14.1, 250.0, 69397.0), 'result': 165.09999999999999}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 9802.0), 'result': 349.0}, {'input': (8.0, 2.2999999999999998, 14.0, 750.0, 1507.0), 'result': 1000.0}, {'input': (4.0, 2.6600000000000001, 13.300000000000001, 250.0, 69397.0), 'result': 304.99000000000001}, {'input': (2.0, 2.2000000000000002, 14.1, 120.0, 13347.0), 'result': 189.0}, {'input': (4.0, 2.5299999999999998, 15.6, 320.0, 1921.0), 'result': 249.99000000000001}, {'input': (3.0, 2.3999999999999999, 14.0, 250.0, 13347.0), 'result': 319.0}, {'input': (4.0, 2.6600000000000001, 14.1, 250.0, 434.0), 'result': 251.5}, {'input': (4.0, 2.7999999999999998, 14.1, 320.0, 15621.0), 'result': 259.25}, {'input': (4.0, 2.7999999999999998, 14.1, 250.0, 434.0), 'result': 145.0}, {'input': (4.0, 1.5, 13.300000000000001, 500.0, 35046.0), 'result': 359.99000000000001}, {'input': (4.0, 2.7999999999999998, 14.1, 320.0, 15621.0), 'result': 259.0}, {'input': (4.0, 1.3999999999999999, 13.300000000000001, 500.0, 773.0), 'result': 445.94999999999999}, {'input': (8.0, 1.73, 17.300000000000001, 500.0, 39.0), 'result': 550.0}, {'input': (4.0, 2.5299999999999998, 14.1, 160.0, 15621.0), 'result': 189.25}, {'input': (4.0, 2.2599999999999998, 15.4, 160.0, 1139.0), 'result': 73.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 158693.0), 'result': 294.94999999999999}, {'input': (4.0, 2.5299999999999998, 13.300000000000001, 250.0, 15621.0), 'result': 298.0}, {'input': (1.0, 1.6000000000000001, 14.1, 40.0, 6773.0), 'result': 117.0}, {'input': (2.0, 1.73, 15.4, 100.0, 199.0), 'result': 58.0}, {'input': (4.0, 2.5299999999999998, 14.0, 320.0, 15621.0), 'result': 269.25}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 5664.0), 'result': 288.0}, {'input': (4.0, 1.3999999999999999, 11.6, 500.0, 158693.0), 'result': 369.99000000000001}, {'input': (4.0, 1.0, 15.6, 320.0, 48646.0), 'result': 177.50999999999999}, {'input': (4.0, 2.7000000000000002, 13.300000000000001, 500.0, 52.0), 'result': 561.0}, {'input': (4.0, 2.3999999999999999, 14.1, 250.0, 69397.0), 'result': 350.0}, {'input': (2.0, 2.2000000000000002, 14.1, 80.0, 13347.0), 'result': 189.94999999999999}, {'input': (2.0, 2.2599999999999998, 13.300000000000001, 160.0, 407.0), 'result': 461.0}, {'input': (4.0, 2.2999999999999998, 13.300000000000001, 320.0, 4622.0), 'result': 671.0}, {'input': (6.0, 2.7000000000000002, 17.300000000000001, 640.0, 5664.0), 'result': 379.99000000000001}, {'input': (4.0, 2.5299999999999998, 17.0, 500.0, 4622.0), 'result': 909.02999999999997}, {'input': (4.0, 1.3, 15.6, 320.0, 19690.0), 'result': 334.99000000000001}, {'input': (4.0, 1.5, 15.6, 320.0, 19690.0), 'result': 324.99000000000001}, {'input': (2.0, 2.2599999999999998, 14.1, 160.0, 21.0), 'result': 124.98999999999999}, {'input': (1.0, 1.8600000000000001, 14.0, 40.0, 619.0), 'result': 99.989999999999995}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 19690.0), 'result': 394.99000000000001}, {'input': (6.0, 3.1000000000000001, 15.6, 750.0, 5664.0), 'result': 398.88999999999999}, {'input': (4.0, 2.5, 17.300000000000001, 500.0, 196.0), 'result': 300.0}, {'input': (2.0, 1.7, 15.6, 320.0, 5828.0), 'result': 309.94999999999999}, {'input': (3.0, 2.1000000000000001, 15.4, 60.0, 2637.0), 'result': 159.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 1368.0), 'result': 329.0}, {'input': (3.0, 2.5299999999999998, 14.1, 250.0, 69397.0), 'result': 175.0}, {'input': (4.0, 1.7, 11.6, 500.0, 4494.0), 'result': 299.99000000000001}, {'input': (2.0, 2.7999999999999998, 14.1, 160.0, 2428.0), 'result': 182.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 158693.0), 'result': 304.94999999999999}, {'input': (512.0, 1.8300000000000001, 14.1, 60.0, 1266.0), 'result': 129.99000000000001}, {'input': (4.0, 2.3999999999999999, 14.1, 160.0, 29730.0), 'result': 289.0}, {'input': (4.0, 2.7999999999999998, 15.4, 250.0, 69397.0), 'result': 229.0}, {'input': (2.0, 1.8999999999999999, 15.4, 120.0, 2515.0), 'result': 159.99000000000001}, {'input': (4.0, 2.7999999999999998, 17.0, 160.0, 93593.0), 'result': 329.99000000000001}, {'input': (1.0, 2.2000000000000002, 13.300000000000001, 160.0, 16.0), 'result': 145.49000000000001}, {'input': (4.0, 1.3999999999999999, 11.6, 500.0, 5664.0), 'result': 339.99000000000001}, {'input': (4.0, 2.5299999999999998, 15.6, 500.0, 13018.0), 'result': 284.99000000000001}, {'input': (1.0, 1.6000000000000001, 10.1, 160.0, 2.0), 'result': 112.5}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 9802.0), 'result': 349.0}, {'input': (4.0, 2.7999999999999998, 14.1, 250.0, 69398.0), 'result': 165.09999999999999}, {'input': (8.0, 2.2999999999999998, 14.0, 750.0, 1507.0), 'result': 1000.0}, {'input': (2.0, 2.2000000000000002, 14.1, 120.0, 13347.0), 'result': 189.0}, {'input': (4.0, 2.6600000000000001, 13.300000000000001, 250.0, 69398.0), 'result': 304.99000000000001}, {'input': (4.0, 2.5299999999999998, 15.6, 320.0, 1921.0), 'result': 249.99000000000001}, {'input': (3.0, 2.3999999999999999, 14.0, 250.0, 13347.0), 'result': 319.0}, {'input': (4.0, 2.3999999999999999, 15.6, 500.0, 158694.0), 'result': 294.94999999999999}, {'input': (4.0, 2.6600000000000001, 14.1, 250.0, 434.0), 'result': 251.5}, {'input': (4.0, 2.7999999999999998, 14.1, 320.0, 15621.0), 'result': 259.25}, {'input': (4.0, 2.7999999999999998, 14.1, 250.0, 434.0), 'result': 145.0}, {'input': (4.0, 1.5, 13.300000000000001, 500.0, 35046.0), 'result': 359.99000000000001}, {'input': (4.0, 2.7999999999999998, 14.1, 320.0, 15621.0), 'result': 259.0}, {'input': (8.0, 1.73, 17.300000000000001, 500.0, 39.0), 'result': 550.0}, {'input': (4.0, 1.3999999999999999, 13.300000000000001, 500.0, 773.0), 'result': 445.94999999999999}, {'input': (4.0, 2.2599999999999998, 15.4, 160.0, 1139.0), 'result': 73.0}, {'input': (4.0, 2.5299999999999998, 14.1, 160.0, 15621.0), 'result': 189.25}, {'input': (4.0, 2.5299999999999998, 14.0, 320.0, 15621.0), 'result': 269.25}, {'input': (1.0, 1.6000000000000001, 14.1, 40.0, 6773.0), 'result': 117.0}, {'input': (2.0, 1.73, 15.4, 100.0, 199.0), 'result': 58.0}, {'input': (2.0, 2.5299999999999998, 14.1, 80.0, 13347.0), 'result': 194.0}]
  118.  
  119.  
  120. """
Advertisement
Add Comment
Please, Sign In to add comment