| Line 31... |
Line 31... |
| 31 |
import email
|
31 |
import email
|
| 32 |
from email.mime.multipart import MIMEMultipart
|
32 |
from email.mime.multipart import MIMEMultipart
|
| 33 |
import email.encoders
|
33 |
import email.encoders
|
| 34 |
import cookielib
|
34 |
import cookielib
|
| 35 |
from multiprocessing import Pool
|
35 |
from multiprocessing import Pool
|
| 36 |
from multiprocessing.dummy import Pool as ThreadPool
|
36 |
from multiprocessing.dummy import Pool as ThreadPool
|
| - |
|
37 |
import urllib2
|
| - |
|
38 |
from BeautifulSoup import BeautifulSoup
|
| - |
|
39 |
import re
|
| - |
|
40 |
from sys import exit
|
| 37 |
|
41 |
|
| 38 |
config_client = ConfigClient()
|
42 |
config_client = ConfigClient()
|
| 39 |
host = config_client.get_property('staging_hostname')
|
43 |
host = config_client.get_property('staging_hostname')
|
| 40 |
syncPrice=config_client.get_property('sync_price_on_marketplace')
|
44 |
syncPrice=config_client.get_property('sync_price_on_marketplace')
|
| 41 |
|
45 |
|
| Line 685... |
Line 689... |
| 685 |
def fetchDetails(flipkartSerialNumber):
|
689 |
def fetchDetails(flipkartSerialNumber):
|
| 686 |
print "Inside fetch details"
|
690 |
print "Inside fetch details"
|
| 687 |
url = "http://www.flipkart.com/ps/%s"%(flipkartSerialNumber)
|
691 |
url = "http://www.flipkart.com/ps/%s"%(flipkartSerialNumber)
|
| 688 |
#url = "http://www.flipkart.com/ps/MOBDTXVZXVY3GFG8"
|
692 |
#url = "http://www.flipkart.com/ps/MOBDTXVZXVY3GFG8"
|
| 689 |
#scraper.read(url)
|
693 |
#scraper.read(url)
|
| 690 |
scraper = FlipkartScraper.FlipkartScraper()
|
694 |
# scraper = FlipkartScraper.FlipkartScraper()
|
| 691 |
vendorsData = scraper.read(url)
|
695 |
# vendorsData = scraper.read(url)
|
| - |
|
696 |
request = urllib2.Request(url)
|
| - |
|
697 |
request.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.218 Safari/535.1')
|
| - |
|
698 |
opener = urllib2.build_opener()
|
| - |
|
699 |
response_data = ""
|
| - |
|
700 |
try:
|
| - |
|
701 |
response_data = opener.open(request).read()
|
| - |
|
702 |
print datetime.now()
|
| - |
|
703 |
print "Fetched response from flipkart for %s" %(url)
|
| - |
|
704 |
|
| - |
|
705 |
except urllib2.HTTPError as e:
|
| - |
|
706 |
print 'ERROR: ', e
|
| - |
|
707 |
print 'Retrying'
|
| - |
|
708 |
self.count_trials += 1
|
| - |
|
709 |
|
| - |
|
710 |
if self.count_trials < 3:
|
| - |
|
711 |
return self.read(url)
|
| - |
|
712 |
|
| - |
|
713 |
self.response_data=response_data
|
| - |
|
714 |
page=self.response_data.decode("utf-8")
|
| - |
|
715 |
print datetime.now()
|
| - |
|
716 |
soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
|
| - |
|
717 |
page = None
|
| - |
|
718 |
self.response_data = None
|
| - |
|
719 |
print datetime.now()
|
| - |
|
720 |
print "Soup created from flipkart data for %s" %(url)
|
| - |
|
721 |
info = []
|
| - |
|
722 |
oddSeller = soup.findAll("div" , {"class" : "line seller-item odd "})
|
| - |
|
723 |
for data in oddSeller:
|
| - |
|
724 |
temp={}
|
| - |
|
725 |
try:
|
| - |
|
726 |
businessDays = data.find('span', attrs={'class' : re.compile('fk-deliverable.*')})
|
| - |
|
727 |
shippingTime = businessDays.find('span', attrs={'class' : re.compile('fk-bold')}).string.replace('to','').replace('business days.','').strip().replace(' ','-')
|
| - |
|
728 |
temp['shippingTime']=shippingTime
|
| - |
|
729 |
except:
|
| - |
|
730 |
pass
|
| - |
|
731 |
price = data.find('span', attrs={'class' : re.compile('pxs-final-price.*')}).string.strip('Rs.').strip()
|
| - |
|
732 |
temp['sellingPrice']=float(price)
|
| - |
|
733 |
for sellerInfo in data.findAll("div",{"class":re.compile(".*seller-info*")}):
|
| - |
|
734 |
sellerName = sellerInfo.find('a').string
|
| - |
|
735 |
temp['sellerName'] = sellerName
|
| - |
|
736 |
for metrics in data.find("div",{"class":"fk-text-right"}):
|
| - |
|
737 |
try:
|
| - |
|
738 |
metric = metrics.findAll('input', {'type': 'submit'})
|
| - |
|
739 |
except AttributeError:
|
| - |
|
740 |
continue
|
| - |
|
741 |
try:
|
| - |
|
742 |
inputTags = metric[0]['data-lst-buytrend']
|
| - |
|
743 |
except TypeError:
|
| - |
|
744 |
continue
|
| - |
|
745 |
dataMetrics = metric[0]['data-listing-metrics']
|
| - |
|
746 |
try:
|
| - |
|
747 |
buyTrend = inputTags[0:str(inputTags).index('NWSR')].replace('_','')
|
| - |
|
748 |
except ValueError:
|
| - |
|
749 |
buyTrend = inputTags[0:str(inputTags).index('WSR')].replace('_','')
|
| - |
|
750 |
temp['buyTrend']=buyTrend
|
| - |
|
751 |
dataMetric = dataMetrics.split(';')
|
| - |
|
752 |
sellerCode = dataMetric[0]
|
| - |
|
753 |
temp['sellerCode']=sellerCode
|
| - |
|
754 |
temp['sellingPriceMetric'] = float(dataMetric[1])
|
| - |
|
755 |
if not temp.has_key('shippingTime'):
|
| - |
|
756 |
print "Populating shipping time from metrics"
|
| - |
|
757 |
temp['shippingTime'] = dataMetric[3]
|
| - |
|
758 |
temp['sellerScore'] = int(dataMetric[4])
|
| - |
|
759 |
info.append(temp)
|
| - |
|
760 |
evenSeller = soup.findAll("div" , {"class" : "line seller-item even "})
|
| - |
|
761 |
for data in evenSeller:
|
| - |
|
762 |
temp={}
|
| - |
|
763 |
price = data.find('span', attrs={'class' : re.compile('pxs-final-price.*')}).string.strip('Rs.')
|
| - |
|
764 |
try:
|
| - |
|
765 |
businessDays = data.find('span', attrs={'class' : re.compile('fk-deliverable.*')})
|
| - |
|
766 |
shippingTime = businessDays.find('span', attrs={'class' : re.compile('fk-bold')}).string.replace('to','').replace('business days.','').strip().replace(' ','-')
|
| - |
|
767 |
temp['shippingTime']=shippingTime
|
| - |
|
768 |
except:
|
| - |
|
769 |
pass
|
| - |
|
770 |
temp['sellingPrice']=float(price)
|
| - |
|
771 |
for sellerInfo in data.findAll("div",{"class":re.compile(".*seller-info*")}):
|
| - |
|
772 |
sellerName = sellerInfo.find('a').string
|
| - |
|
773 |
temp['sellerName'] = sellerName
|
| - |
|
774 |
for metrics in data.find("div",{"class":"fk-text-right"}):
|
| - |
|
775 |
try:
|
| - |
|
776 |
metric = metrics.findAll('input', {'type': 'submit'})
|
| - |
|
777 |
except AttributeError:
|
| - |
|
778 |
continue
|
| - |
|
779 |
try:
|
| - |
|
780 |
inputTags = metric[0]['data-lst-buytrend']
|
| - |
|
781 |
except TypeError:
|
| - |
|
782 |
continue
|
| - |
|
783 |
dataMetrics = metric[0]['data-listing-metrics']
|
| - |
|
784 |
try:
|
| - |
|
785 |
buyTrend = inputTags[0:str(inputTags).index('NWSR')].replace('_','')
|
| - |
|
786 |
except ValueError:
|
| - |
|
787 |
buyTrend = inputTags[0:str(inputTags).index('WSR')].replace('_','')
|
| - |
|
788 |
temp['buyTrend']=buyTrend
|
| - |
|
789 |
dataMetric = dataMetrics.split(';')
|
| - |
|
790 |
temp['sellerCode'] = dataMetric[0]
|
| - |
|
791 |
temp['sellingPriceMetric'] = float(dataMetric[1])
|
| - |
|
792 |
if not temp.has_key('shippingTime'):
|
| - |
|
793 |
print "Populating shipping time from metrics"
|
| - |
|
794 |
temp['shippingTime'] = dataMetric[3]
|
| - |
|
795 |
temp['sellerScore'] = int(dataMetric[4])
|
| - |
|
796 |
info.append(temp)
|
| - |
|
797 |
vendorsData = info
|
| - |
|
798 |
info = []
|
| - |
|
799 |
print "Returning Json response from flipkart for %s" %(url)
|
| 692 |
fin = datetime.now()
|
800 |
fin = datetime.now()
|
| 693 |
print "Finish with data for serial Number %s %s" %(flipkartSerialNumber,str(fin))
|
801 |
print "Finish with data for serial Number %s %s" %(flipkartSerialNumber,str(fin))
|
| 694 |
sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
|
802 |
sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
|
| 695 |
vendorsData[:]=[]
|
803 |
vendorsData[:]=[]
|
| 696 |
rank ,ourSp, iterator, secondLowestSellerSp, prefSellerSp, lowestSellerSp, lowestSellerScore, prefSellerScore, secondLowestSellerScore, ourScore, shippingTimeLowerLimitLowestSeller,shippingTimeUpperLimitLowestSeller, \
|
804 |
rank ,ourSp, iterator, secondLowestSellerSp, prefSellerSp, lowestSellerSp, lowestSellerScore, prefSellerScore, secondLowestSellerScore, ourScore, shippingTimeLowerLimitLowestSeller,shippingTimeUpperLimitLowestSeller, \
|