Esempio n. 1
1
 def ensureorder(self, order):
     curmet_list = Merchant.objects.filter(id = self.merchantid)
     if curmet_list.count() == 0:
         return None
     if not self.iflogin():
         res = self.loginele()
         if res is None or res is False:
             return None
     requrl = 'http://napos.ele.me/order/processOrder/id/' + str(order) + '/category/1'
     html = self.net.GetResFromRequest('GET', requrl, 'utf-8')
     if str(html) == '0':
         newlog = RunTimeLog()
         newlog.content = '确认饿了没订单失败'
         newlog.status = False
         newlog.err_message = str(order)
         newlog.ltype = 16
         newlog.merchant = curmet_list[0]
         newlog.save()
         return False
     return True
Esempio n. 2
1
 def getVerify(self, curmet):
     cache_list = CatcheData.objects.filter(merchant = curmet)
     cache = cache_list[0]
     phone = curmet.alin_account
     print phone
     postdic = {'utm_medium': 'android',
                'dVersion': '16_4.1.1',
                'utm_source': '1',
                'sid': cache.mei_sid,
                'uuid': cache.mei_sid,
                'utm_content': '863077020776914',
                'appName': '美团外卖商家版',
                'phone': phone,
                'dType': 'MI 2S',
                'appCode': '59',
                'utm_term': '2.3.8',
                'appType': '4',
                'logType': 'C'}
     if self.__use_proxy:
         print 'use proxy get verify m'
         html = self.net.GetResFromRequest('POST', 'http://waimaieapi.meituan.com/api/poi/getValidCode', 'utf-8', postdic, use_proxy=True)
     else:
         html = self.net.GetResFromRequest('POST', 'http://waimaieapi.meituan.com/api/poi/getValidCode', 'utf-8', postdic)
     print 'verfiy code'
     print html
     if not isinstance(html, str):
         newlog = RunTimeLog()
         newlog.content = '美团服务器未返回响应'
         newlog.status = False
         newlog.err_message = str(html)
         newlog.ltype = 30
         newlog.merchant = curmet
         newlog.save()
         return None
     res_json = simplejson.loads(html)
     res_code = str(res_json['code'])
     res_msg = str(res_json['msg'])
     if res_code != '0':
         curmet.mei_message = res_msg
         curmet.save()
         newlog = RunTimeLog()
         newlog.content = '美团验证码接收失败'
         newlog.merchant = curmet
         newlog.err_message = html
         newlog.ltype = 34
         newlog.save()
         return self.errhandle(res_code, curmet)
     return True
Esempio n. 3
1
 def refuseOrder(self, curmet, orderid):
     cache_list = CatcheData.objects.filter(merchant = curmet)
     cache = None
     if cache_list.count > 0:
         if cache_list[0].mei_token == '':
             self.get_token(curmet)
             cache_list = CatcheData.objects.filter(merchant = curmet)
     cache = cache_list[0]
     postdic = {'utm_medium': 'android',
                'wmPoiId': cache.mei_id,
                'dVersion': '16_4.1.1',
                'utm_source': '1',
                'utm_content': '863077020776914',
                'appName': '美团外卖商家版',
                'dType': 'MI 2S',
                'appCode': '59',
                'reasonId': '338',
                'uuid': cache.mei_sid,
                'orderId': str(orderid),
                'utm_term': '2.3.8',
                'appType': '4',
                'logType': 'C',
                'remark': '美食已售完',
                 'acctId': cache.mei_acctid,
                'token': cache.mei_token}
     if self.__use_proxy:
         print 'use proxy refuse m'
         html = self.net.GetResFromRequest('POST', 'http://waimaieapi.meituan.com/api/order/cancel/v3', 'utf-8', postdic, use_proxy=True)
     else:
         html = self.net.GetResFromRequest('POST', 'http://waimaieapi.meituan.com/api/order/cancel/v3', 'utf-8', postdic)
     if not isinstance(html, str):
         newlog = RunTimeLog()
         newlog.content = '美团服务器未返回响应'
         newlog.status = False
         newlog.err_message = str(html)
         newlog.ltype = 30
         newlog.merchant = curmet
         newlog.save()
         return None
     res_json = simplejson.loads(html)
     res_code = str(res_json['code'])
     res_msg = str(res_json['msg'])
     if res_code != '0':
         curmet.mei_message = res_msg
         curmet.save()
         newlog = RunTimeLog()
         newlog.content = '美团拒绝订单失败'
         newlog.merchant = curmet
         newlog.err_message = html
         newlog.ltype = 37
         newlog.save()
         return self.errhandle(res_code, curmet)
     return True
Esempio n. 4
0
 def ensureorder(self, order):
     curmet_list = Merchant.objects.filter(id=self.merchantid)
     if curmet_list.count() == 0:
         return None
     if not self.iflogin():
         res = self.loginele()
         if res is None or res is False:
             return None
     requrl = 'http://napos.ele.me/order/processOrder/id/' + str(
         order) + '/category/1'
     html = self.net.GetResFromRequest('GET', requrl, 'utf-8')
     if str(html) == '0':
         newlog = RunTimeLog()
         newlog.content = '确认饿了没订单失败'
         newlog.status = False
         newlog.err_message = str(order)
         newlog.ltype = 16
         newlog.merchant = curmet_list[0]
         newlog.save()
         return False
     return True
Esempio n. 5
0
 def get_token(self, curmet):
     user = str(curmet.mei_account)
     passwd = str(curmet.mei_passwd)
     de_pass = Decrypt(passwd)
     cache_list = CatcheData.objects.filter(merchant = curmet)
     sid = None
     verify_code = ''
     if cache_list.count() > 0:
         sid = cache_list[0].mei_sid
         if cache_list[0].mei_need_verify is True:
             if cache_list[0].mei_verify is not None:
                 verify_code = cache_list[0].mei_verify
             else:
                 return None
     sid = createMeiId()
     postdic = {'utm_medium': 'android',
                'sid': sid,
                'dVersion': '16_4.1.1',
                'utm_source': '1',
                'utm_content': '863077020776914',
                'appName': '美团外卖商家版',
                'dType': 'MI 2S',
                'appCode': '59',
                'validCode': verify_code,
                'uuid': sid,
                'utm_term': '2.3.8',
                'appType': '4',
                'logType': 'C',
                 'userName': user,
                'password': de_pass}
     if self.__use_proxy:
         print 'use proxy login m'
         html = self.net.GetResFromRequest('POST', 'http://waimaieapi.meituan.com/api/poi/logon/app/v4', 'utf-8', postdic, use_proxy=True)
     else:
         html = self.net.GetResFromRequest('POST', 'http://waimaieapi.meituan.com/api/poi/logon/app/v4', 'utf-8', postdic)
     if not isinstance(html, str):
         newlog = RunTimeLog()
         newlog.content = '美团服务器未返回响应'
         newlog.status = False
         newlog.err_message = str(html)
         newlog.ltype = 30
         newlog.merchant = curmet
         newlog.save()
         if self.__use_proxy:
             check_proxy_times(str(self.net.Proxy))
         return None
     if self.__use_proxy:
         reset_proxy_times(str(self.net.Proxy))
     res_json = simplejson.loads(html)
     login_status = str(res_json['code'])
     login_msg = str(res_json['msg'])
     if login_msg != 'ok' or login_status != '0':
         if cache_list.count() == 0:
             newcache = CatcheData()
             newcache.merchant = curmet
             newcache.mei_sid = str(sid)
             newcache.save()
         curmet.mei_message = login_msg
         curmet.mei_status = False
         curmet.save()
         newlog = RunTimeLog()
         newlog.content = '美团登陆失败'
         newlog.merchant = curmet
         newlog.err_message = html
         newlog.ltype = 35
         newlog.save()
         self.errhandle(login_status, curmet)
         return False
     login_data = res_json['data']
     if cache_list.count > 0:
         cache = cache_list[0]
         cache.mei_token = str(login_data['accessToken'])
         cache.mei_acctid = str(login_data['acctId'])
         cache.mei_sid = str(sid)
         cache.mei_need_verify = False
         cache.mei_verify = ''
         poidata = login_data['poiLists'][0]
         cache.mei_id = str(poidata['id'])
         cache.mei_lastorderid = createMeiOrder(str(poidata['id']))
         print 'mei login'
         cache.save()
     else:
         cache = CatcheData()
         cache.merchant = curmet
         cache.mei_sid = str(sid)
         cache.mei_token = str(login_data['accessToken'])
         cache.mei_acctid = str(login_data['acctId'])
         poidata = login_data['poiLists'][0]
         cache.mei_id = str(poidata['id'])
         cache.mei_lastorderid = createMeiOrder(str(poidata['id']))
         cache.save()
     curmet.mei_status = True
     curmet.save()
     return True
Esempio n. 6
0
 def getOrder(self, curmet):
     print 'getOrder'
     cache = None
     cache_list = CatcheData.objects.filter(merchant = curmet)
     if cache_list.count > 0:
         if cache_list[0].mei_token == '' or cache_list[0].mei_token is None:
             res = self.get_token(curmet)
             if res is False:
                 return None
     else:
         res = self.get_token(curmet)
         if res is False:
             return None
     cache_list = CatcheData.objects.filter(merchant = curmet)
     cache = cache_list[0]
     postdic = {'utm_medium': 'android',
                'wmPoiId': cache.mei_id,
                'dVersion': '16_4.1.1',
                'utm_source': '1',
                'utm_content': '863077020776914',
                'appName': '美团外卖商家版',
                'dType': 'MI 2S',
                'appCode': '59',
                'uuid': cache.mei_sid,
                'lastOrderId': cache.mei_lastorderid,
                'utm_term': '2.3.8',
                'appType': '4',
                'logType': 'C',
                 'acctId': cache.mei_acctid,
                'token': cache.mei_token}
     if self.__use_proxy:
         print 'use proxy get order m'
         html = self.net.GetResFromRequest('POST', 'http://waimaieapi.meituan.com/api/order/new/orderList/v3', 'utf-8', postdic, use_proxy=True)
     else:
         html = self.net.GetResFromRequest('POST', 'http://waimaieapi.meituan.com/api/order/new/orderList/v3', 'utf-8', postdic)
     if not isinstance(html, str):
         newlog = RunTimeLog()
         newlog.content = '美团服务器未返回响应'
         newlog.status = False
         newlog.err_message = str(html)
         newlog.ltype = 30
         newlog.merchant = curmet
         newlog.save()
         if self.__use_proxy:
             check_proxy_times(str(self.net.Proxy))
         return None
     if self.__use_proxy:
         reset_proxy_times(str(self.net.Proxy))
     res_json = simplejson.loads(html)
     res_code = str(res_json['code'])
     res_msg = str(res_json['msg'])
     if res_code != '0':
         curmet.mei_message = res_msg
         curmet.mei_status = False
         curmet.save()
         newlog = RunTimeLog()
         newlog.content = '美团抓单失败'
         newlog.merchant = curmet
         newlog.err_message = html
         newlog.ltype = 38
         newlog.save()
         self.errhandle(res_code, curmet)
         return False
     res_data = res_json['data']
     thiscount = int(DayOrder.objects.filter(merchant=curmet, platform=3).count()) + 1
     totalcount = int(DayOrder.objects.filter(merchant=curmet).count()) + 1
     for itm in res_data:
         mei_order_id = str(itm['wm_order_id_view'])
         print mei_order_id
         ifhave = DayOrder.objects.filter(order_id_old = str(mei_order_id))
         if ifhave.count() > 0:
             continue
         onpay = False
         pay_status = str(itm['pay_status'])
         if pay_status != '0':
             onpay = True
         newdayorder = DayOrder()
         newid = createAlinOrderNum(3, curmet.id, thiscount)
         newdayorder.order_id_alin = newid
         newdayorder.qr_path = createqr(1, newid)
         newdayorder.address = str(itm['recipient_address'])
         newdayorder.phone = str(itm['recipient_phone'])
         newdayorder.real_price = str(itm['total_after'])
         if int(str(itm['delivery_btime'])) == 0:
             newdayorder.send_time = timestampToDatetime(str(itm['order_time']))
         else:
             newdayorder.send_time = timestampToDatetime(str(itm['delivery_btime']))
         newdayorder.order_time = timestampToDatetime(str(itm['order_time']))
         newdayorder.order_id_old = str(itm['wm_order_id_view'])
         newdayorder.note = str(itm['remark'])
         newdayorder.merchant = curmet
         newdayorder.status = 1
         newdayorder.platform = 3
         newdayorder.plat_num = str(itm['wm_poi_order_dayseq'])
         newdayorder.origin_price = str(itm['total_before'])
         newdayorder.pay = onpay
         newdayorder.day_num = str(totalcount)
         newdayorder.save()
         dish_list = itm['details']
         for item in dish_list:
             newdish = Dish()
             newdish.dish_name = str(item['food_name'])
             newdish.dish_count = str(item['count'])
             newdish.dish_price = str(item['food_price'])
             newdish.order = DayOrder.objects.get(order_id_alin = newid)
             newdish.save()
         thiscount += 1
     curmet.mei_status = True
     curmet.save()
     return True
Esempio n. 7
0
 def catcheorder(self):
     curmet = None
     thiscount = 0
     self.net.Referer = 'http://napos.ele.me/'
     if not self.iflogin():
         res = self.loginele()
         if res is None or res is False:
             return None
     cur_list = Merchant.objects.filter(id=self.merchantid)
     if cur_list.count() == 0:
         return None
     curmet = cur_list[0]
     tn = str(time.time())[0:10] + '000'
     requrl = 'http://napos.ele.me/order/list?list=unprocessed_waimai&t=' + tn
     html = self.net.GetResFromRequest('GET', requrl, 'utf-8')
     # print html
     # html = ''
     # with open('abc.txt', 'r') as f1:
     #     line = f1.readline()
     #     while line:
     #         html += line
     #         line = f1.readline()
     # print html
     if html is None:
         newlog = RunTimeLog()
         newlog.content = '爬虫抓单失败'
         newlog.status = False
         newlog.ltype = 18
         newlog.merchant = curmet
         newlog.save()
         return None
     soup = BeautifulSoup(html)
     intro = soup.find('ul', attrs={'id': 'list_items'})
     if intro is None:
         notlogin = soup.find('label', attrs={'for': 'tab1'})
         if notlogin is not None:
             self.cookie = None
         print 'no new orders, login failed'
         return False
     res = intro.findAll('li')
     if len(res) == 0:
         print 'no new orders'
         return False
     thiscount = int(
         DayOrder.objects.filter(merchant=curmet, platform=2).count()) + 1
     # print 'start: ' + str(thiscount)
     for item in res:
         neworder = DayOrder()
         onpay = False
         yu_time = item.find('a', attrs={'rel': 'tipsy'})
         process_num = item.find('span', attrs={'class': 'process_num'})
         detail = item.find('p', attrs={'class': 'list_addr'})
         note = item.find('p', attrs={'class': 'list_description'})
         price = item.find('p', attrs={'class': 'list_price'})
         menu = item.find('div', attrs={'class': 'menu_content'})
         orderid = item['orderid']
         print orderid
         ifhave = DayOrder.objects.filter(order_id_old=str(orderid))
         if ifhave.count() > 0:
             # print 'break'
             # print auto_id + 1
             continue
         timestamp = item['createdat']
         online = item['online-pay']
         if note is not None:
             note = note.string[2:]
         if online != '':
             onpay = True
         phone = item.find('p', attrs={'class': 'list_customer'})
         formattime = time.localtime(float(timestamp))
         datetimee = datetime.datetime(*formattime[:6])
         timee = time.strftime('%Y-%m-%d %H:%M:%S', formattime)
         newid = createAlinOrderNum(2, self.merchantid, thiscount)
         thiscount += 1
         qrres = createqr(1, newid)
         time.sleep(1)
         if yu_time.has_attr('explain'):
             res = re.findall(r'([0-9,:, ,-]+)', str(yu_time['explain']))
             if len(res) > 0:
                 yutt = time.strptime(str(res[0]), "%Y-%m-%d %H:%M:%S")
                 yudatetime = datetime.datetime(*yutt[:6])
                 neworder.send_time = yudatetime
             else:
                 neworder.send_time = datetimee
         else:
             neworder.send_time = datetimee
         address = detail.string[3:]
         neworder.address = address[1:]
         neworder.order_id_alin = newid
         neworder.order_id_old = orderid
         neworder.origin_price = price.string[1:]
         neworder.real_price = price.string[1:]
         neworder.phone = phone.string[3:]
         neworder.order_time = datetimee
         neworder.status = 1
         neworder.promotion = 'nothing'
         neworder.pay = onpay
         neworder.note = note
         neworder.platform = 2
         neworder.qr_path = qrres
         neworder.merchant = curmet
         neworder.plat_num = str(process_num.string)
         neworder.save()
         print timee
         print orderid
         print phone.string[3:]
         print price.string[1:]
         print address
         dishname = menu.findAll(attrs={'class': 'dishname'})
         dishcount = menu.findAll(attrs={'class': 'item_quantity'})
         dishprice = menu.findAll(attrs={'class': 'item_price'})
         for i in range(0, len(dishname)):
             newdish = Dish()
             newdish.dish_name = str(dishname[i].string)
             newdish.dish_count = int(dishcount[i].string)
             newdish.dish_price = float(dishprice[i].string)
             newdish.order = DayOrder.objects.get(order_id_alin=newid)
             newdish.save()
     return True
Esempio n. 8
0
 def loginele(self):
     self.net.Referer = 'http://napos.ele.me/login'
     dip = distriproxy(self.merchantid)
     if dip is None:
         newlog = RunTimeLog()
         newlog.merchant = Merchant.objects.get(id=self.merchantid)
         newlog.content = '无可用代理ip,爬虫停止'
         newlog.ltype = 10
         newlog.status = False
         newlog.save()
         return None
     self.net.Proxy = dip
     curmet_list = Merchant.objects.filter(id=self.merchantid)
     if curmet_list.count() == 0:
         print 'no mer'
         newlog = RunTimeLog()
         newlog.content = '无商家信息,爬虫停止'
         newlog.err_message = self.merchantid
         newlog.ltype = 11
         newlog.status = False
         newlog.save()
         return None
     curmet = curmet_list[0]
     user = str(curmet.ele_account)
     passwd = str(curmet.ele_passwd)
     de_pass = Decrypt(passwd)
     postdic = {'username': user, 'password': de_pass}
     # print postdic
     self.account = user
     self.password = de_pass
     html = self.net.GetResFromRequest('POST',
                                       'http://napos.ele.me/auth/doLogin',
                                       'utf-8', postdic)
     if html is None:
         check_proxy_times(self.net.Proxy)
         res = self.loginele()
         return res
     else:
         reset_proxy_times(self.net.Proxy)
         res_json = simplejson.loads(html)
         if res_json['success'] is False:
             errmsg = str(res_json['message']).decode('unicode_escape')
             curmet.ele_message = errmsg
             curmet.ele_status = False
             curmet.save()
             newlog = RunTimeLog()
             newlog.content = '饿了么登陆失败'
             newlog.merchant = curmet
             newlog.err_message = errmsg
             newlog.ltype = 15
             newlog.save()
             return False
         elif res_json['success'] is True:
             if self.net.SearchCookie('SSID') == 'nothing find':
                 res = self.loginele()
                 return res
             curmet.ele_status = True
             curmet.save()
             self.cookie = self.net.CookieList
             print self.cookie
             print 'succ'
             return True
Esempio n. 9
0
 def refuseorder(self, curmet, order):
     # self.set_proxy(curmet)
     if not self.iflogin(curmet):
         res = self.loginele(curmet)
         if res is None or res is False:
             return None
     cache_list = CatcheData.objects.filter(merchant = curmet)
     cache = cache_list[0]
     self.net.SetCookie(str(cache.ele_cookie))
     requrl = 'http://napos.ele.me/order/setInvalid/id/' + str(order) + '/category/1?type=6&remark='
     if self.__use_proxy:
         print 'use proxy refuse'
         html = self.net.GetResFromRequest('GET', requrl, 'utf-8', use_proxy=True)
     else:
         html = self.net.GetResFromRequest('GET', requrl, 'utf-8')
     if not isinstance(html, str):
         newlog = RunTimeLog()
         newlog.content = '拒绝饿了么订单失败'
         newlog.status = False
         newlog.err_message = str(html)
         newlog.ltype = 17
         newlog.merchant = curmet
         newlog.save()
         return False
     if str(html) == '0':
         newlog = RunTimeLog()
         newlog.content = '拒绝饿了么订单失败'
         newlog.ltype = 17
         newlog.err_message = str(order) + html
         newlog.status = False
         newlog.merchant = curmet
         newlog.save()
         return False
     return True
Esempio n. 10
0
 def ensureorder(self, curmet, order):
     # curmet_list = Merchant.objects.filter(id = self.merchantid)
     # if curmet_list.count() == 0:
     #     return None
     # self.set_proxy(curmet)
     if not self.iflogin(curmet):
         res = self.loginele(curmet)
         if res is None or res is False:
             return None
     cache_list = CatcheData.objects.filter(merchant = curmet)
     cache = cache_list[0]
     self.net.SetCookie(str(cache.ele_cookie))
     requrl = 'http://napos.ele.me/order/processOrder/id/' + str(order) + '/category/1'
     if self.__use_proxy:
         print 'use proxy ensure'
         html = self.net.GetResFromRequest('GET', requrl, 'utf-8', use_proxy=True)
     else:
         html = self.net.GetResFromRequest('GET', requrl, 'utf-8')
     if not isinstance(html, str):
         newlog = RunTimeLog()
         newlog.content = '确认饿了没订单失败'
         newlog.status = False
         newlog.err_message = str(html)
         newlog.ltype = 16
         newlog.merchant = curmet
         newlog.save()
         return False
     if str(html) == '0':
         newlog = RunTimeLog()
         newlog.content = '确认饿了没订单失败'
         newlog.status = False
         newlog.err_message = str(order) + html
         newlog.ltype = 16
         newlog.merchant = curmet
         newlog.save()
         return False
     return True
Esempio n. 11
0
 def loginele(self, curmet):
     # self.set_proxy(curmet)
     # self.net.Referer = 'http://napos.ele.me/login'
     print 'loginele'
     user = str(curmet.ele_account)
     passwd = str(curmet.ele_passwd)
     de_pass = Decrypt(passwd)
     postdic = {'username': user, 'password': de_pass}
     # print 'login ele'
     if self.__use_proxy:
         print 'use proxy login'
         html = self.net.GetResFromRequest('POST', 'http://napos.ele.me/auth/doLogin', 'utf-8', postdic, use_proxy=True)
     else:
         html = self.net.GetResFromRequest('POST', 'http://napos.ele.me/auth/doLogin', 'utf-8', postdic)
     # print 'login end'
     if not isinstance(html, str):
         newlog = RunTimeLog()
         newlog.content = '饿了么登陆失败'
         newlog.merchant = curmet
         newlog.err_message = str(html)
         newlog.ltype = 15
         newlog.status = False
         newlog.save()
         if self.__use_proxy:
             check_proxy_times(str(self.net.Proxy))
     else:
         if self.__use_proxy:
             reset_proxy_times(str(self.net.Proxy))
         res_json = simplejson.loads(html)
         if res_json['success'] is False:
             errmsg = unicode(res_json['message'])
             curmet.ele_message = errmsg.encode('unicode_escape').decode('unicode_escape')
             curmet.ele_status = False
             curmet.save()
             newlog = RunTimeLog()
             newlog.content = '饿了么登陆失败'
             newlog.merchant = curmet
             newlog.err_message = errmsg
             newlog.ltype = 15
             newlog.save()
             return False
         elif res_json['success'] is True:
             # if self.net.Cookies['SSID'] == 'nothing find':
             #     res = self.loginele()
             #     return res
             curmet.ele_status = True
             curmet.save()
             cache_list = CatcheData.objects.filter(merchant = curmet)
             print str(self.net.OutPutCookie())
             if cache_list.count() > 0:
                 cache = cache_list[0]
                 cache.ele_cookie = str(self.net.OutPutCookie())
                 cache.save()
             else:
                 cache = CatcheData()
                 cache.ele_cookie = str(self.net.OutPutCookie())
                 cache.merchant = curmet
                 cache.save()
             return True
Esempio n. 12
0
 def catcheorder(self, merchant):
     thiscount = 0
     # self.net.Referer = 'http://napos.ele.me/'
     print 'catcheele'
     # self.set_proxy(merchant)
     if not self.iflogin(merchant):
         res = self.loginele(merchant)
         if res is None or res is False:
             return None
     # cur_list = Merchant.objects.filter(id = self.merchantid)
     cache_list = CatcheData.objects.filter(merchant=merchant)
     cache = cache_list[0]
     self.net.SetCookie(str(cache.ele_cookie))
     tn = str(time.time())[0:10] + '000'
     requrl = 'http://napos.ele.me/order/list?list=unprocessed_waimai&t=' + tn
     if self.__use_proxy:
         print 'use proxy catche'
         html = self.net.GetResFromRequest('GET', requrl, 'utf-8', use_proxy=True)
     else:
         html = self.net.GetResFromRequest('GET', requrl, 'utf-8')
     print html
     # with open('abc.txt', 'r') as f1:
     #     line = f1.readline()
     #     while line:
     #         html += line
     #         line = f1.readline()
     if not isinstance(html, str):
         newlog = RunTimeLog()
         newlog.content = '爬虫抓单失败'
         newlog.err_message = str(html)
         newlog.status = False
         newlog.ltype = 18
         newlog.merchant = merchant
         newlog.save()
         if self.__use_proxy:
             check_proxy_times(str(self.net.Proxy))
         return None
     if self.__use_proxy:
         reset_proxy_times(str(self.net.Proxy))
     soup = BeautifulSoup(html)
     intro = soup.find('ul', attrs={'id': 'list_items'})
     if intro is None:
         notlogin = soup.find('label', attrs={'for': 'tab1'})
         if notlogin is not None:
             self.cookie = None
         print 'no new orders, login failed'
         return False
     res = soup.findAll('div', attrs={'id': 'order_json_data'})
     if len(res) == 0:
         print 'no new orders'
         return False
     thiscount = int(DayOrder.objects.filter(merchant=merchant, platform=2).count()) + 1
     totalcount = int(DayOrder.objects.filter(merchant=merchant).count()) + 1
     # print 'start: ' + str(thiscount)
     print len(res)
     for item in res:
         # print item
         onpay = False
         jsonres = simplejson.loads(item.string)
         if_book = jsonres['isBook']
         delivertime = jsonres['deliverTime']
         orderid = jsonres['id']
         price = jsonres['total']
         ordertime = jsonres['createdAt']
         phone = str(jsonres['phone']).split(',')
         plat_num = jsonres['restaurantNumber']
         address = unicode(jsonres['address']).encode('unicode_escape').decode('unicode_escape')
         note = jsonres['description']
         onpay = jsonres['isOnlinePayment']
         print orderid
         ifhave = DayOrder.objects.filter(order_id_old = str(orderid))
         if ifhave.count() > 0:
             continue
         if len(phone) > 1:
             note += '应急电话:%s' % phone[1]
             phone = phone[0]
         else:
             phone = phone[0]
         newid = createAlinOrderNum(2, merchant.id, thiscount)
         thiscount += 1
         qrres = createqr(1, newid)
         ordertime_t = time.strptime(str(ordertime), "%Y-%m-%d %H:%M:%S")
         ordertime_datetime = datetime.datetime(*ordertime_t[:6])
         neworder = DayOrder(order_id_alin=newid, order_id_old=orderid, phone=phone, pay=onpay, address=address, platform=2, plat_num=plat_num, note=note, origin_price=price, real_price=price, status=1, order_time=ordertime_datetime, day_num=totalcount, qr_path=qrres)
         if if_book:
             # print len(delivertime)
             deliver_u  = str(delivertime).replace('分', '').replace('点', '')
             deliver_s = deliver_u.encode('unicode_escape').decode('unicode_escape')
             # print deliver_s
             date = datetime.datetime.now().strftime("%Y-%m-%d")
             date += ' ' + deliver_s
             deliver_t = time.strptime(str(date), "%Y-%m-%d %H%M")
             deliver_datetime = datetime.datetime(*deliver_t[:6])
             neworder.send_time = deliver_datetime
         else:
             neworder.send_time = ordertime_datetime
         neworder.merchant = merchant
         neworder.save()
         dishs = jsonres['items']
         norder = DayOrder.objects.get(order_id_alin=newid)
         for itm in dishs:
             dishname = unicode(itm['name']).encode('unicode_escape').decode('unicode_escape')
             dishcount = itm['quantity']
             dishprice = float(itm['price'])
             newdish = Dish(dish_name=dishname, dish_price=dishprice, dish_count=dishcount)
             newdish.order = norder
             newdish.save()
     return True
Esempio n. 13
0
 def catcheorder(self):
     curmet = None
     thiscount = 0
     self.net.Referer = 'http://napos.ele.me/'
     if not self.iflogin():
         res = self.loginele()
         if res is None or res is False:
             return None
     cur_list = Merchant.objects.filter(id = self.merchantid)
     if cur_list.count() == 0:
         return None
     curmet = cur_list[0]
     tn = str(time.time())[0:10] + '000'
     requrl = 'http://napos.ele.me/order/list?list=unprocessed_waimai&t=' + tn
     html = self.net.GetResFromRequest('GET', requrl, 'utf-8')
     # print html
     # html = ''
     # with open('abc.txt', 'r') as f1:
     #     line = f1.readline()
     #     while line:
     #         html += line
     #         line = f1.readline()
     # print html
     if html is None:
         newlog = RunTimeLog()
         newlog.content = '爬虫抓单失败'
         newlog.status = False
         newlog.ltype = 18
         newlog.merchant = curmet
         newlog.save()
         return None
     soup = BeautifulSoup(html)
     intro = soup.find('ul', attrs={'id': 'list_items'})
     if intro is None:
         notlogin = soup.find('label', attrs={'for': 'tab1'})
         if notlogin is not None:
             self.cookie = None
         print 'no new orders, login failed'
         return False
     res = intro.findAll('li')
     if len(res) == 0:
         print 'no new orders'
         return False
     thiscount = int(DayOrder.objects.filter(merchant=curmet, platform=2).count()) + 1
     # print 'start: ' + str(thiscount)
     for item in res:
         neworder = DayOrder()
         onpay = False
         yu_time = item.find('a', attrs={'rel': 'tipsy'})
         process_num = item.find('span', attrs={'class': 'process_num'})
         detail = item.find('p', attrs={'class': 'list_addr'})
         note = item.find('p', attrs={'class': 'list_description'})
         price = item.find('p', attrs={'class': 'list_price'})
         menu = item.find('div', attrs={'class': 'menu_content'})
         orderid = item['orderid']
         print orderid
         ifhave = DayOrder.objects.filter(order_id_old = str(orderid))
         if ifhave.count() > 0:
         # print 'break'
         # print auto_id + 1
             continue
         timestamp = item['createdat']
         online = item['online-pay']
         if note is not None:
             note = note.string[2:]
         if online != '':
             onpay = True
         phone = item.find('p', attrs={'class': 'list_customer'})
         formattime = time.localtime(float(timestamp))
         datetimee = datetime.datetime(*formattime[:6])
         timee = time.strftime('%Y-%m-%d %H:%M:%S', formattime)
         newid = createAlinOrderNum(2, self.merchantid, thiscount)
         thiscount += 1
         qrres = createqr(1, newid)
         time.sleep(1)
         if yu_time.has_attr('explain'):
             res = re.findall(r'([0-9,:, ,-]+)', str(yu_time['explain']))
             if len(res) > 0:
                 yutt = time.strptime(str(res[0]), "%Y-%m-%d %H:%M:%S")
                 yudatetime = datetime.datetime(*yutt[:6])
                 neworder.send_time = yudatetime
             else:
                 neworder.send_time = datetimee
         else:
             neworder.send_time = datetimee
         address = detail.string[3:]
         neworder.address = address[1:]
         neworder.order_id_alin = newid
         neworder.order_id_old = orderid
         neworder.origin_price = price.string[1:]
         neworder.real_price = price.string[1:]
         neworder.phone = phone.string[3:]
         neworder.order_time = datetimee
         neworder.status = 1
         neworder.promotion = 'nothing'
         neworder.pay = onpay
         neworder.note = note
         neworder.platform = 2
         neworder.qr_path = qrres
         neworder.merchant = curmet
         neworder.plat_num = str(process_num.string)
         neworder.save()
         print timee
         print orderid
         print phone.string[3:]
         print price.string[1:]
         print address
         dishname = menu.findAll(attrs = {'class': 'dishname'})
         dishcount = menu.findAll(attrs = {'class': 'item_quantity'})
         dishprice = menu.findAll(attrs = {'class': 'item_price'})
         for i in range(0, len(dishname)):
             newdish = Dish()
             newdish.dish_name = str(dishname[i].string)
             newdish.dish_count = int(dishcount[i].string)
             newdish.dish_price = float(dishprice[i].string)
             newdish.order = DayOrder.objects.get(order_id_alin = newid)
             newdish.save()
     return True
Esempio n. 14
0
 def loginele(self):
     self.net.Referer = 'http://napos.ele.me/login'
     dip = distriproxy(self.merchantid)
     if dip is None:
         newlog = RunTimeLog()
         newlog.merchant = Merchant.objects.get(id = self.merchantid)
         newlog.content = '无可用代理ip,爬虫停止'
         newlog.ltype = 10
         newlog.status = False
         newlog.save()
         return None
     self.net.Proxy = dip
     curmet_list = Merchant.objects.filter(id = self.merchantid)
     if curmet_list.count() == 0:
         print 'no mer'
         newlog = RunTimeLog()
         newlog.content = '无商家信息,爬虫停止'
         newlog.err_message = self.merchantid
         newlog.ltype = 11
         newlog.status = False
         newlog.save()
         return None
     curmet = curmet_list[0]
     user = str(curmet.ele_account)
     passwd = str(curmet.ele_passwd)
     de_pass = Decrypt(passwd)
     postdic = {'username': user, 'password': de_pass}
     # print postdic
     self.account = user
     self.password = de_pass
     html = self.net.GetResFromRequest('POST', 'http://napos.ele.me/auth/doLogin', 'utf-8', postdic)
     if html is None:
         check_proxy_times(self.net.Proxy)
         res = self.loginele()
         return res
     else:
         reset_proxy_times(self.net.Proxy)
         res_json = simplejson.loads(html)
         if res_json['success'] is False:
             errmsg = str(res_json['message']).decode('unicode_escape')
             curmet.ele_message = errmsg
             curmet.ele_status = False
             curmet.save()
             newlog = RunTimeLog()
             newlog.content = '饿了么登陆失败'
             newlog.merchant = curmet
             newlog.err_message = errmsg
             newlog.ltype = 15
             newlog.save()
             return False
         elif res_json['success'] is True:
             if self.net.SearchCookie('SSID') == 'nothing find':
                 res = self.loginele()
                 return res
             curmet.ele_status = True
             curmet.save()
             self.cookie = self.net.CookieList
             print self.cookie
             print 'succ'
             return True