def post(self): name = request.form.get("username") pwd = request.form.get("password") if request.args.get("type", "") == "api": # API登陆, 返回token rds = get_redis("default") try: u = AdminUser.objects.get(username=name, password=md5(pwd), is_removed=0) tk = md5(str(time.time())) k = "token%s" % tk rds.set(k, u.username) rds.expire(k, 24*60*60) return jsonify({"code": 1, "message": "登陆成功", "token": tk}) except AdminUser.DoesNotExist: return jsonify({"code": 0, "message": "登陆失败"}) else: # 网页登陆 session["username"] = name session["password"] = pwd code = request.form.get("validcode", "") if not name.startswith("snmpay") and (not code or code != session.get("img_valid_code")): flash("验证码错误", "error") return redirect(url_for('dashboard.login')) try: u = AdminUser.objects.get(username=name, password=md5(pwd), is_removed=0) flask_login.login_user(u) return redirect(url_for('dashboard.index')) except AdminUser.DoesNotExist: flash("用户名或密码错误", "error") return redirect(url_for('dashboard.login'))
def post_data_templ(self, service_id, sdata): ts = int(time.time()) code = "car12308com" key = "car12308com201510" tmpl = { "merchantCode": code, "version": "1.4.0", "timestamp": ts, "serviceID": service_id, "data": sdata, "sign": md5("%s%s%s%s%s" % (code, service_id, ts, sdata, md5(key))), } return tmpl
def request_loader(request): token = request.headers.get('token') username = request.headers.get('username') # if token and token == TOKEN: if token: try: u = AdminUser.objects.get(username=username) except AdminUser.DoesNotExist: return None return u token = request.args.get("token", "") username = request.args.get("username") if token: # if token and token == TOKEN: try: u = AdminUser.objects.get(username=username) except AdminUser.DoesNotExist: return None return u name = request.form.get("username") pwd = request.form.get("password") if not name or not pwd: return try: u = AdminUser.objects.get(username=name, password=md5(pwd)) except AdminUser.DoesNotExist: return None u.is_authenticated = True return u
def create_user(type): import getpass from app.models import AdminUser from app.utils import md5 if type not in ["kefu", "admin"]: print "fail, the right command should like 'python manage.py create_user (kefu or admin)'" return username = raw_input("用户名:") try: u = AdminUser.objects.get(username=username) print "已存在用户, 创建失败" return except AdminUser.DoesNotExist: pass pwd1 = getpass.getpass('密码: ') pwd2 = getpass.getpass('确认密码: ') if pwd1 != pwd2: print "两次输入密码不一致, 创建用户失败" return u = AdminUser(username=username, password=md5(pwd1)) if type == "kefu": u.is_switch = 0 elif type == "admin": u.is_switch = 0 u.save() print "创建用户成功"
def admin(): if request.path.startswith("/admin") and request.method == "POST": user_email = request.form.get("email") password = request.form.get("password") admin = is_admin(user_email, password) if admin: month = 3600 * 24 * 30 cookie = ut.md5(f"__::{admin['id']}:_:{admin['mail_handle']}::__") set_admin_cookie(admin['id'], cookie) response = make_response(redirect(url_for("index_admin"))) response.set_cookie("auth", cookie, month) response.set_cookie("uid", str(admin["id"]), month) return response else: flash("The email or password are invalid, try it again.") return render_template("admin/login.html") if request.cookies.get("auth") is None: return redirect(url_for("admin_login")) else: cookie = request.cookies.get("auth") user = is_set_cookie(cookie) return redirect(url_for("index_admin")) if user else redirect(url_for("admin_login"))
def reset(): users = [ sp_user(Account='20131000669', UserName='******', Password=md5('admin'), UserType='admin'), sp_user(Account='20181000001', UserName='******', Password=md5('student'), UserType='default') ] sp_user.query.delete() for user in users: db.session.add(user) db.session.commit() return sp_user.list2dict(users)
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } now = dte.now() ua = random.choice(MOBILE_USER_AGENG) headers = {"User-Agent": ua} update_attrs = {} line_url = 'http://www.nmghyjt.com/index.php/search/getBuslist' data = { "dd_city": line.d_sta_name, "dd_code": line.d_sta_id, "ispost": '1', "orderdate": line.drv_date, "start_city": line.s_sta_name } r = requests.post(line_url, data=data, headers=headers) content = r.content if not isinstance(content, unicode): content = content.decode('utf-8') sel = etree.HTML(content) scheduleList = sel.xpath('//div[@id="visitorDataTable"]/table/tbody/tr') if scheduleList: for d in scheduleList[1:]: bus_num = d.xpath('td[1]/text()')[0] drv_time = d.xpath('td[5]/span[@class="lv_time"]/text()')[0] price = d.xpath('td[8]/span[@class="tk_price"]/text()')[0] left_tickets = d.xpath('td[9]/span/text()')[0] postdata = d.xpath('td[10]/a/@onclick')[0].split(',')[1][1:-3] drv_datetime = dte.strptime("%s %s" % (line.drv_date, drv_time), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "bus_num": bus_num, "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5("%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(bus_num)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue extra_info = {"postdata": postdata} info = { "full_price": float(price), "fee": 0, "left_tickets": int(left_tickets), "refresh_datetime": now, "extra_info": extra_info, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={"left_tickets": 0, "refresh_datetime": now}) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } line_url = "http://www.zjgsmwy.com/busticket/busticket/service/Busticket.getBusTicketList.json" params = { "AREACODE": line.s_city_id, "ONSTAION": line.s_sta_name, "OFFSTATION": line.d_city_name, "STARTDATE": line.drv_date, } headers = { "User-Agent": random.choice(BROWSER_USER_AGENT), "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", } r = requests.post(line_url, data=urllib.urlencode(params), headers=headers) res = r.json() now = dte.now() if res["rtnCode"] != "000000": result_info.update(result_msg="error response", update_attrs={"left_tickets": 0, "refresh_datetime": now}) return result_info shift_list = res["responseData"]["shiftList"] or [] update_attrs = {} for d in shift_list: drv_datetime = dte.strptime("%s %s" % (d["startdate"], d["starttime"]), "%Y%m%d %H%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": d["onstation"], "d_sta_name": d["offstation"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5("%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue info = { "full_price": float(d["price"]), "fee": 0, "left_tickets": int(d["availablenum"]), "refresh_datetime": now, "extra_info": {"startstation": d["startstation"], "terminalstation": d["terminalstation"]}, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={"left_tickets": 0, "refresh_datetime": now}) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def get_md5_sign(params): ks = params.keys() ks.sort() rlt = '' for k in ks: if params[k] == None or len(params[k]) == 0: continue rlt = rlt + "&%s=%s" % (k, params[k]) return md5(rlt[1:]).upper()
def verify_password(username_or_token, password): try: user = AdminUser.objects(username=username_or_token).first() if user.password == md5(password): # 存储g.user # g.user = user.username return True return False except: return False
def post(self): name = request.form.get("username") pwd = request.form.get("password") session["username"] = name session["password"] = pwd # code = request.form.get("validcode") # if code != session.get("img_valid_code"): # flash("验证码错误", "error") # return redirect(url_for('dashboard.login')) access_log.info('login post test') try: access_log.info('%s %s' %(name, md5(pwd))) u = AdminUser.objects.get(username=name, password=md5(pwd), is_close=False) # flash("登陆成功", "info") flask_login.login_user(u) return redirect(url_for('dashboard.index')) except AdminUser.DoesNotExist: access_log.info('login error') flash("用户名或密码错误!!!", "error") return redirect(url_for('dashboard.login'))
def get_pay_page(self, order, valid_code="", session=None, pay_channel="alipay", **kwargs): rebot = order.get_lock_rebot() is_login = rebot.test_login_status() if not is_login: is_login = (rebot.login() == "OK") if is_login: if order.status in [STATUS_LOCK_RETRY, STATUS_WAITING_LOCK]: self.lock_ticket(order) if order.status == STATUS_WAITING_ISSUE: url = "http://apppay.wisesz.mobi/payment_mobile/order/set_pay" st = str(int(time.time() * 1000)) self.refresh_issue(order) order.reload() pay_sn = order.pay_order_no params = { "sign": md5(pay_sn + st), "uid": rebot.uid, "pay_source": 3, "platform": 2, "pwd": "", "use_coupon": "", "time": st, "appVersion": "3.9.1", "order_sn": pay_sn, "use_wallet": 0, "deviceId": "", "version": "3.9.1", } headers = { "User-Agent": rebot.user_agent, "Content-Type": "application/x-www-form-urlencoded", } r = rebot.http_post(url, headers=headers, data=urllib.urlencode(params)) res = r.json() if res["errorCode"] != 0: return { "flag": "error", "content": json.dumps(res, ensure_ascii=False) } order.update(pay_channel='alipay') return {"flag": "url", "content": res["data"]["url"]} else: return {"flag": "error", "content": u"账号登录失败"}
def test_valid_login(self): with app.test_client() as client: data = {'username': '******', 'password': md5('nana')} fix = urllib.urlencode(data) response = client.get( '/ajax_login?' + fix, # follow_redirects=True, ) assert 'ok' in response.data print response.data # 这里需要登陆 response = client.get( '/news/', ) assert 'title' in response.data
def send_order_request(self, order, by="wap"): rebot = order.get_lock_rebot() if by == "wap": url = "http://m.hn96520.com/PersonCenter/OrderDetail?ordersn=%s&isSuccess=True" % order.raw_order_no cookies = json.loads(rebot.cookies) headers = {'User-Agent': rebot.user_agent} r = rebot.http_get(url, headers=headers, cookies=cookies) soup = bs(r.content, "lxml") state = soup.select(".orderTime span")[1].text sn = order.raw_order_no pcode = "" for o in soup.select(".orderFu"): lst = re.findall(u"取票密码: (\d+)", o.text.strip()) if lst: pcode = lst[0] break else: sn = order.pay_order_no userid = rebot.userid username = order.source_account password = md5(order.source_account_pass) url = 'http://61.163.88.138:8088/auth?UserName={0}&Password={1}&Sign={2}&_={3}&callback=jsonp1'.format( username, password, rebot.sign, time.time()) headers = { "User-Agent": rebot.user_agent, } r = rebot.http_get(url, headers=headers) userid = json.loads(r.content[r.content.index("(") + 1:r.content.rindex(")")]).get( 'UserId', '') ourl = 'http://61.163.88.138:8088/Order/GetMyOrders?UserId={0}&Sign={1}&_={2}&callback=jsonp1'.format( userid, rebot.sign, time.time()) r = rebot.http_get(ourl, headers=headers, cookies=r.cookies) info = json.loads(r.content[r.content.index("(") + 1:r.content.rindex(")")]).get( 'OrderList', []) for x in info: ocode = x['OrderCode'] if sn == ocode: pcode = x.get('Password', '') state = x['OrderStatus'] return { "state": state, "pick_code": pcode, 'raw_order': sn, }
def reset_password(): import getpass from app.models import AdminUser from app.utils import md5 username = raw_input("用户名:") try: u = AdminUser.objects.get(username=username) pwd1 = getpass.getpass('密码: ') pwd2 = getpass.getpass('确认密码: ') if pwd1 != pwd2: print "两次输入密码不一致, 重设密码失败" return u.modify(password=md5(pwd1)) print "重设密码成功" except AdminUser.DoesNotExist: print "不存在用户", username
def send_order_request(self, order): rebot = order.get_lock_rebot() sn = order.pay_order_no sign = SOURCE_INFO.get('hn96520').get('accounts').get( order.source_account)[-1] # sign = '90e7709954c38af7713e1a64bad2012ecd00565e016e16823032e2d465dbd14a' username = order.source_account password = md5(order.source_account_pass) url = 'http://61.163.88.138:8088/auth?UserName={0}&Password={1}&Sign={2}&_={3}&callback=jsonp1'.format( username, password, sign, time.time()) # rebot_log.info(url) headers = { "User-Agent": rebot.user_agent, } r = requests.get(url, headers=headers) # requests.get(url, headers=headers, cookies=r.cookies) userid = json.loads(r.content[r.content.index("(") + 1:r.content.rindex(")")]).get( 'UserId', '') ourl = 'http://61.163.88.138:8088/Order/GetMyOrders?UserId={0}&Sign={1}&_={2}&callback=jsonp1'.format( userid, sign, time.time()) r = requests.get(ourl, headers=headers, cookies=r.cookies, timeout=2048) # rebot_log.info(ourl) info = json.loads(r.content[r.content.index("(") + 1:r.content.rindex(")")]).get( 'OrderList', []) for x in info: ocode = x['OrderCode'] if sn == ocode: pcode = x['Password'] state = x['OrderStatus'] return { "state": state, "pick_no": pcode, "pick_code": pcode, "pick_site": '', 'raw_order': sn, "pay_money": 0.0, }
def get_user(username, password): return fetch_db('SELECT * FROM users WHERE username=? AND password=?', (username, md5(password)))
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } line_url = config.FANGBIAN_API_URL + "/Query" params = { "departure": line.s_city_name, "dptCode": line.s_city_code, "destination": line.d_city_name, "desCode": line.d_city_code, "dptTime": line.drv_date, "stationCode": "", "queryType": "1", "exParms": "" } fd = self.post_data_templ("U0103", json.dumps(params)) r = requests.post(line_url, data=urllib.urlencode(fd), headers=self.post_headers()) res = r.json() now = dte.now() if res["code"] != 1100: result_info.update(result_msg="error response: %s" % res["message"], update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info update_attrs = {} for d in res["data"]: dpt_time = d["dptTime"] lst = dpt_time.split(":") if len(lst) == 3: dpt_time = ":".join(lst[:2]) drv_datetime = dte.strptime("%s %s" % (d["dptDate"], dpt_time), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": d["dptStation"], "d_sta_name": d["arrStation"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue extra_info = {"exData1": d["exData1"], "exData2": d["exData2"]} info = { "full_price": float(d["ticketPrice"]), "fee": float(d["fee"]), "left_tickets": int(d["ticketLeft"] or 0), "refresh_datetime": now, "extra_info": extra_info, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
from flask import Flask from flask_migrate import Migrate from flask_sqlalchemy import SQLAlchemy from app.utils import md5 logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger() logger.addHandler(TimedRotatingFileHandler('logs/debug.log', 'd')) app = Flask(__name__) app.config.from_object('settings') app.config['BOT_HASHSUM'] = md5(app.config['BOT_TOKEN']) db = SQLAlchemy(app) from app.telegram import telegram from app.telegram import handlers from app.todoist import todoist app.register_blueprint(telegram) app.register_blueprint(handlers.handlers) app.register_blueprint(todoist) app.bot = handlers.MyBot(app.config['BOT_TOKEN']) migrate = Migrate(app, db)
# -*- coding: utf-8 -*- __author__ = 'ivany' # import uuid from app.utils import md5, getUuid if __name__ == "__main__": # u = uuid.uuid4() str = md5("he02834894") print(str) print(getUuid())
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } rebot = XyjtWebRebot.get_one() url = 'http://order.xuyunjt.com/wsdgbccx.aspx' rds = get_redis("line") vs_key = "xyjt:viewstate:%s" % line.s_sta_id vs = rds.get(vs_key) or "" now = dte.now() headers = { "User-Agent": random.choice(BROWSER_USER_AGENT), "Content-Type": "application/x-www-form-urlencoded" } if not vs: # VIEWSTATE 各个站都不一样 try: r = rebot.http_get("http://order.xuyunjt.com/wsdgbccx.aspx", headers=headers) soup = bs(r.content, "lxml") vs = soup.select_one("#__VIEWSTATE").get("value") data = { 'ctl00$ContentPlaceHolder1$ScriptManager1': 'ctl00$ContentPlaceHolder1$ScriptManager1|ctl00$ContentPlaceHolder1$BtnBccx', '__EVENTARGUMENT': '', '__LASTFOCUS': '', '__VIEWSTATE': vs, 'ctl00$ContentPlaceHolder1$ddlincounty': line.s_sta_id, 'ctl00$ContentPlaceHolder1$ddlsaledate': line.drv_datetime.strftime("%Y%m%d"), 'ctl00$ContentPlaceHolder1$txtstop': u"南京", 'radio': u"南京", } r = rebot.http_post(url, headers=headers, data=urllib.urlencode(data)) soup = bs(r.content, 'lxml') except: result_info.update(result_msg="exception_ok_vserror", update_attrs={"left_tickets": 5, "refresh_datetime": now}) return result_info vs = soup.select_one("#__VIEWSTATE").get("value") rds.set(vs_key, vs) rds.expire(vs_key, 30*60*60) data = { 'ctl00$ContentPlaceHolder1$ScriptManager1': 'ctl00$ContentPlaceHolder1$ScriptManager1|ctl00$ContentPlaceHolder1$BtnBccx', 'ctl00$ContentPlaceHolder1$BtnBccx': '班次查询', '__EVENTARGUMENT': '', '__LASTFOCUS': '', '__VIEWSTATE': vs, 'ctl00$ContentPlaceHolder1$ddlincounty': line.s_sta_id, 'ctl00$ContentPlaceHolder1$ddlsaledate': line.drv_datetime.strftime("%Y%m%d"), 'ctl00$ContentPlaceHolder1$txtstop': line.d_city_name, 'radio': "", } try: r = rebot.http_post(url, headers=headers, data=urllib.urlencode(data)) soup = bs(r.content, 'lxml') except: result_info.update(result_msg="exception_ok", update_attrs={"left_tickets": 5, "refresh_datetime": now}) return result_info update_attrs = {} db_lines = {x.line_id: x for x in Line.objects.filter(s_city_name=line.s_city_name,d_city_name=line.d_city_name, drv_date=line.drv_date)} for tr_o in soup.select("#ctl00_ContentPlaceHolder1_GVBccx tr")[1:]: if tr_o.get("class") and "GridViewHeaderStyle" in tr_o.get("class"): continue td_lst = tr_o.select("td") index_tr = lambda idx: td_lst[idx].text.strip() drv_date, drv_time = index_tr(0), index_tr(5) if u"流水" in drv_time: continue drv_datetime=dte.strptime("%s %s" % (drv_date, drv_time), "%Y-%m-%d %H:%M") left_tickets=int(index_tr(8)) full_price=float(index_tr(6)) line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": unicode(index_tr(1)), "d_sta_name": unicode(index_tr(3)), "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } attrs = { "left_tickets": left_tickets, "refresh_datetime": now, "full_price": full_price, "extra_info__lock_url": td_lst[12].find("a").get("href"), } line_id = md5("%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) if line_id in db_lines: db_lines[line_id].update(**attrs) if line_id == line.line_id: update_attrs = attrs if not update_attrs: result_info.update(result_msg="no line info", update_attrs={"left_tickets": 0, "refresh_datetime": now}) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): rebot = SzkyWebRebot.get_one() headers = {"User-Agent": rebot.user_agent} cookies = {} for i in range(20): try: res = rebot.query_code(headers) except: continue if res.get('status', '') == 0: cookies = res.get('cookies') valid_code = res.get('valid_code') break result_info = { "result_msg": "", "update_attrs": {}, } now = dte.now() if cookies: data = { "DstNode": line.d_sta_name, "OpAddress": "-1", "OpStation": "-1", "OperMode": '', "SchCode": '', "SchDate": line.drv_date, "SchTime": '', 'SeatType': '', 'StartStation': line.s_sta_id, 'WaitStationCode': line.extra_info['raw_info']['SchWaitStCode'], 'cmd': "MQCenterGetClass", 'txtImgCode': valid_code, } line_url = 'http://211.162.125.225/UserData/MQCenterSale.aspx' headers.update({ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Referer": "http://211.162.125.225/User/Default.aspx", "X-Requested-With": "XMLHttpRequest", }) try: r = rebot.http_post(line_url, data=urllib.urlencode(data), headers=headers, cookies=cookies) res = json.loads(trans_js_str(r.content)) except: result_info.update(result_msg="exception_ok", update_attrs={ "left_tickets": 5, "refresh_datetime": now }) line_log.info("%s\n%s", "".join(traceback.format_exc()), locals()) return result_info else: return self.do_refresh_line_by_app(line) update_attrs = {} try: for d in res["data"]: if d['SchStat'] == '1': drv_datetime = dte.strptime( "%s %s" % (d["SchDate"], d["orderbytime"]), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": d["SchWaitStName"], "d_sta_name": d["SchNodeName"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue info = { "full_price": float(d["SchStdPrice"]), "fee": 0, "left_tickets": int(d["SchTicketCount"]), "refresh_datetime": now, "extra_info": { "raw_info": d }, } if line_id == line.line_id: update_attrs = info else: if obj.left_tickets == 0: continue obj.update(**info) except: result_info.update(result_msg="exception_ok", update_attrs={ "left_tickets": 5, "refresh_datetime": now }) line_log.info("%s\n%s", "".join(traceback.format_exc()), locals()) return result_info if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } now = dte.now() # rebot = E8sAppRebot.get_one() # if not rebot.test_login_status(): # rebot.login() # rebot.reload() # headers = rebot.http_header() # cookies = json.loads(rebot.cookies) headers = { "User-Agent": "Apache-HttpClient/UNAVAILABLE (java 1.4)", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } data = { "drvDate": line.drv_date, "rowNum": "10", "page": "1", "stopId": line.d_city_id, #"131000", "carryStaId": "-1" } url = "http://m.e8s.com.cn/bwfpublicservice/stationGetSchPlan.action" try: res = requests.post(url, data=data, headers=headers) res = res.json() except: result_info.update(result_msg="timeout default 10", update_attrs={ "left_tickets": 10, "refresh_datetime": now }) return result_info if res["flag"] != "1": result_info.update(result_msg="error response", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info update_attrs = {} for d in res["detail"]: if d['carrStaName'] != u"八王坟": continue drv_datetime = dte.strptime("%s %s" % (d["drvDate"], d["drvTime"]), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "bus_num": d["scheduleId"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(bus_num)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue info = { "full_price": float(d["fullPrice"]), "fee": 0, "left_tickets": int(d["seatAmount"]), "refresh_datetime": now, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } rebot = None now = dte.now() update_attrs = {} for i in BjkyWebRebot.objects.filter( is_active=True).order_by('-last_login_time')[0:5]: if i.test_login_status(): rebot = i break bjky_flag = False if rebot: bjky_flag = True queryline_url = "http://e2go.com.cn/TicketOrder/SearchSchedule" data = { "ArrivingStop": line.d_city_name, "ArrivingStopId": line.d_city_id, "ArrivingStopJson": line.extra_info['ArrivingStopJson'], "DepartureDate": line.drv_date, "Order": "DepartureTimeASC", "RideStation": line.s_sta_name, "RideStationId": line.s_sta_id } try: r = rebot.http_post(queryline_url, data=data, headers=rebot.http_header(), cookies=json.loads(rebot.cookies)) except: result_info.update(result_msg="bjky timeout default 5", update_attrs={ "left_tickets": 5, "refresh_datetime": now }) return result_info content = r.content if not isinstance(content, unicode): content = content.decode('utf-8') sel = etree.HTML(content) scheduleList = sel.xpath( '//div[@id="scheduleList"]/table/tbody/tr') for i in range(0, len(scheduleList), 2): s = scheduleList[i] time = s.xpath('td[@class="departureTimeCell"]/span/text()')[0] scheduleIdSpan = s.xpath( 'td[@class="scheduleAndBusLicenseCes"]/span[@class="scheduleSpan"]/span[@class="scheduleIdSpan"]/text()' )[0] scheduleIdSpan = scheduleIdSpan.replace('\r\n', '').replace( '\t', '').replace(' ', '') price = s.xpath( 'td[@class="ticketPriceCell"]/span[@class="ticketPriceSpan"]/span[@class="ticketPriceValueSpan"]/text()' )[0] drv_datetime = dte.strptime("%s %s" % (line.drv_date, time), "%Y-%m-%d %H:%M") left_less = s.xpath('td[@class="memoCell"]/span/@class') left_tickets = 45 if left_less: left_tickets = 0 line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "bus_num": scheduleIdSpan, "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(bus_num)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue info = { "full_price": float(price), "fee": 0, "left_tickets": int(left_tickets), "refresh_datetime": now, } if line_id == line.line_id: update_attrs = info else: if obj.left_tickets == 0: continue obj.update(**info) if not update_attrs and not bjky_flag: s_sta_name = line.s_sta_name d_city_name = line.d_city_name if line.s_sta_name != u'首都机场站': #s_sta_name = line.s_sta_name.strip().rstrip("站") s_sta_name = s_sta_name + '客运站' ctrip_flag = True try: ctrip_line = Line.objects.get( crawl_source='ctrip', s_sta_name=s_sta_name, d_city_name=d_city_name, bus_num=line.bus_num, full_price=line.full_price, drv_date=line.drv_date, drv_time=line.drv_time, ) except: ctrip_flag = False if ctrip_flag: params = dict( param="/api/home", method="product.getBusDetail", v="1.0", ref="ctrip.h5", partner="ctrip.h5", clientType="Android--hybrid", fromCity=ctrip_line.s_city_name, toCity=ctrip_line.d_city_name, busNumber=ctrip_line.bus_num, fromStation=ctrip_line.s_sta_name, toStation=ctrip_line.d_sta_name, fromDate=ctrip_line.drv_date, fromTime=ctrip_line.drv_time, contentType="json", ) base_url = "http://m.ctrip.com/restapi/busphp/app/index.php" url = "%s?%s" % (base_url, urllib.urlencode(params)) ua = random.choice(MOBILE_USER_AGENG) try: r = requests.get(url, headers={"User-Agent": ua}) ret = r.json() except: result_info.update(result_msg="except_ok", update_attrs={ "left_tickets": 5, "refresh_datetime": now }) return result_info if ret: if ret["code"] == 1: info = ret["return"] if info: ticket_info = info["showTicketInfo"] left_tickets = 0 if ticket_info == "有票": left_tickets = 45 elif ticket_info.endswith("张"): left_tickets = int(ticket_info[:-1]) elif ticket_info in ["预约购票", "无票"]: left_tickets = 0 else: pass service_info = info["servicePackage"] fee = 0 for d in service_info: if d["type"] == "service": fee = d["amount"] break info = { "full_price": info["fullPrice"], "fee": fee, "left_tickets": int(left_tickets), "refresh_datetime": now, } update_attrs = info if not update_attrs: if not bjky_flag: result_info.update(result_msg="except_ok2", update_attrs={ "left_tickets": 5, "refresh_datetime": now }) return result_info result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): import urllib2 result_info = { "result_msg": "", "update_attrs": {}, } now = dte.now() # headers={ # "User-Agent": "Apache-HttpClient/UNAVAILABLE (java 1.4)", # "Content-Type": "application/x-www-form-urlencoded", # "Content-Length":623, # "accept": "application/json,", # "Accept-Encoding": "gzip", # "clienttype":"android", # "clienttoken": '', # "Host": "www.bus365.com", # "Connection": 'Keep-Alive' # } init_params = { "token": '{"clienttoken":"","clienttype":"android"}', "clienttype": "android", "usertoken": '' } params = { "departdate": line.drv_date, "departcityid": line.extra_info['start_info']['id'], "reachstationname": line.d_city_name } params.update(init_params) url = "http://%s/schedule/searchscheduler2/0" % line.extra_info['start_info']['netname'] line_url = "%s?%s" % (url, urllib.urlencode(params)) # proxies = { # 'http': 'http://192.168.1.33:8888', # 'https': 'http://192.168.1.33:8888', # } # r = requests.get(line_url, headers=headers) # proxy = urllib2.ProxyHandler(proxies) # opener = urllib2.build_opener(proxy) # urllib2.install_opener(opener) request = urllib2.Request(line_url) request.add_header('User-Agent', "Apache-HttpClient/UNAVAILABLE (java 1.4)") request.add_header('Content-type', "application/x-www-form-urlencoded") request.add_header('accept', "application/json,") request.add_header('clienttype', "android") request.add_header('clienttoken', "") try: response = urllib2.urlopen(request, timeout=20) except: result_info.update(result_msg="bus365 timeout default 15", update_attrs={"left_tickets": 15, "refresh_datetime": now}) return result_info res = json.loads(response.read()) update_attrs = {} for d in res['schedules']: if int(d['iscansell']) == 1: drv_datetime = dte.strptime("%s %s" % (line.drv_date, d['departtime'][0:-3]), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": d["busshortname"], "d_sta_name": d["stationname"], "bus_num": d["schedulecode"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5("%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue info = { "full_price": float(d["fullprice"]), "fee": 3, "left_tickets": int(d["residualnumber"]), "refresh_datetime": now, "shift_id": d['id'], } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={"left_tickets": 0, "refresh_datetime": now}) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def make_cache_key(*args, **kwargs): key = urllib.urlencode(request.values.to_dict()) return md5(key)
def wrapper_func(*args, **kwargs): usr = request.args.get('usr', '') pwd = request.args.get('pwd', '') if usr == USR and md5(pwd) == PWD: return func(*args, **kwargs) return '<h1>404</h1>'
def create_user(username, password, is_admin=0): query_db( 'INSERT INTO users (username, password, is_admin) ' 'VALUES (?, ?, ?)', (username, md5(password), is_admin))
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } url = "http://183.6.161.195:9000/api/TicketOrder/QuerySchedule" now = dte.now() stime, etime = self._get_query_period(line) params = { "fromcity": line.s_city_name, "schdate": line.drv_datetime.strftime("%Y%m%d"), "schtimeend": etime, "schtimestart": stime, "tocity": line.d_city_name, # "tocity": line.d_sta_name, "startstation": line.s_sta_id, } headers = { "Content-Type": "application/json; charset=UTF-8", "User-Agent": random.choice(MOBILE_USER_AGENG) } try: r = requests.post(url, data=json.dumps(params), headers=headers, timeout=10) res = r.json() except: result_info.update(result_msg="exception_ok", update_attrs={ "left_tickets": 1, "refresh_datetime": now }) return result_info if not res.get("success", False): result_info.update(result_msg="error response %s" % res.get("errmsg", ""), update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info update_attrs = {} for d in res["data"]: if not d["sell"]: continue drv_datetime = dte.strptime( "%s %s" % (d["schdate"], d["sendtime"]), "%Y%m%d %H%M") line_id_args = { "s_city_name": res["startcity"], "d_city_name": line.d_city_name, "s_sta_name": d["startstationname"], "d_sta_name": d["endstationname"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue info = { "full_price": float(d["price"]), "fee": 0, "left_tickets": int(d["lefttickets"]), "refresh_datetime": now, "extra_info": { "endnodename": d["endnodename"], "endnodecode": d["endnodecode"], "endcity": d["endcity"] }, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } now = dte.now() rebot = ShkyzzWebRebot.get_one() headers = { 'User-Agent': random.choice(BROWSER_USER_AGENT), "Upgrade-Insecure-Requests": "1", "Host": "www.zxjt.sh.cn", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", } data = { "searchForm.fromRegionName": line.s_city_name, "searchForm.arriveRegionName": line.d_city_name, "searchForm.flightDate": line.drv_date, "__multiselect_searchForm.stationIdArr": '', "searchForm.startDate": '', "searchForm.selFlightCountFlag": "true", } url = "http://www.zxjt.sh.cn/ajax/flightJsonAction!search" try: r = rebot.http_post(url, data=data, headers=headers) res = r.json() except: result_info.update(result_msg="timeout default 10", update_attrs={ "left_tickets": 10, "refresh_datetime": now }) return result_info update_attrs = {} for d in res.get('flightList', []): drv_datetime = dte.strptime( "%s %s" % (line.drv_date, d['flightTime']), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": d['arriveRegionName'], "bus_num": d['flightNo'], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(bus_num)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue info = { "full_price": float(d['price']), "fee": 0, "left_tickets": int(d['lastCount']), "refresh_datetime": now, "extra_info": { "raw_info": d }, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } now = dte.now() if (line.drv_datetime - now).total_seconds() <= 65 * 60: # 不卖一小时之内的票 result_info.update(result_msg="1小时内的票不卖", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info params = { "startPlace": line.s_city_name, "endPlace": line.d_city_name, "sbId": line.extra_info["sbId"], "stId": line.extra_info["stId"], "depotId": line.extra_info["depotId"], "busId": line.bus_num, "leaveDate": line.drv_date, "beginStationId": line.s_sta_id, "endStationId": line.d_sta_id, "endStationName": line.d_sta_name, } check_url = "http://www.wanmeibus.com/ticket/checkBuyTicket.htm" ua = random.choice(BROWSER_USER_AGENT) try: r = requests.post(check_url, data=urllib.urlencode(params), headers={ "User-Agent": ua, "Content-Type": "application/x-www-form-urlencoded" }, timeout=10) res = r.json() except: result_info.update(result_msg="exception_ok", update_attrs={ "left_tickets": 5, "refresh_datetime": now }) return result_info if not res["success"]: if res["msg"] != "查询失败": result_info.update(result_msg=res["msg"], update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info line_url = "http://s4mdata.wanmeibus.com:80/app/v3/ticket/busList.htm" params = { "content": { "pageSize": 1025, "beginCityName": line.s_city_name, "currentPage": 1, "endCityName": line.d_city_name, "leaveDate": line.drv_date, }, "common": { "pushToken": "864103026424157", "channelVer": "ahwmguanwang", "usId": "", "appId": "com.hundsun.InternetSaleTicket.wanmei", "appVer": "1.0.1", "loginStatus": "0", "imei": "864103026424157", "mobileVer": "4.4.2", "terminalType": "1" }, "key": "" } ua = random.choice(MOBILE_USER_AGENG) headers = {"User-Agent": ua} try: r = requests.post(line_url, data=json.dumps(params), headers=headers) res = r.json() except: result_info.update(result_msg="exception_ok", update_attrs={ "left_tickets": 5, "refresh_datetime": now }) return result_info if res["returnNo"] != "0000": result_info.update(result_msg="error response", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info update_attrs = {} for d in res["content"]["busList"]: drv_datetime = dte.strptime( "%s %s" % (d["leaveDate"], d["leaveTime"]), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": d["beginStation"], "d_sta_name": d["endStation"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue extra_info = { "depotName": d["depotName"], "sbId": d["sbId"], "stId": d["stId"], "depotId": d["depotId"] } info = { "full_price": float(d["fullPrice"]), "fee": 0, "left_tickets": int(d["remainCount"]), "refresh_datetime": now, "extra_info": extra_info, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } # line_url = "http://58.213.132.27:8082/nj_weixinService/2.0/queryBus" # params = { # "drive_date": line.drv_datetime.strftime("%Y%m%d"), # "rst_name": line.s_sta_name, # "dst_name": line.d_city_name, # "v_source": "a", # "v_version": "v2.2", # "v_reg_id": "" # } # req_data = { # "param_key": json.dumps(params), # "secret_key": md5("&".join(map(lambda a:"%s=%s" % (a[0], a[1]), sorted(params.items(), key=lambda i: i[0])))), # } line_url = "http://58.213.132.28/weixin/proxy/queryBus" req_data = { "ewx": "Q5YGlfiOcg6pRhpEQS5zVPwba/CIBUZTO4j6Yu7IVKiGOYlGOnIhsP94Fci1Sxd5bSrnHfQ7yCI9CbrCBxfM2LZ0/mqpGGZc6sH7RyYxqGQ=", "drive_date": line.drv_datetime.strftime("%Y%m%d"), "rst_name": line.s_sta_name, "dst_name": line.d_city_name, } now = dte.now() rebot = JsdlkyWebRebot.get_one() url = "%s?%s" % (line_url, urllib.urlencode(req_data)) for i in range(3): try: r = rebot.http_get(url, headers={"User-Agent": random.choice(MOBILE_USER_AGENG)}, timeout=10) res = r.json() except: result_info.update(result_msg="exception_ok", update_attrs={"left_tickets": 5, "refresh_datetime": now}) return result_info if res["rtn_code"] == "00": break if res["rtn_code"] != "00": result_info.update(result_msg="errorsponse_ok", update_attrs={"left_tickets": 5, "refresh_datetime": now}) return result_info update_attrs = {} for d in res["data"] or []: drv_datetime = dte.strptime("%s %s" % (d["drive_date"], d["plan_time"]), "%Y%m%d %H%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": d["rst_name"], "d_sta_name": d["dst_name"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5("%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue extra_info = {"startstation": d["sst_name"], "terminalstation": d["tst_name"], "startstationcode": d["sstcode"]} info = { "full_price": float(d["full_price"]), "fee": 0, "left_tickets": int(d["available_tickets"]), "refresh_datetime": now, "extra_info": extra_info, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={"left_tickets": 0, "refresh_datetime": now}) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): now = dte.now() pre = 'http://www.hn96520.com/placeorder.aspx?' params = { "start": line.s_city_name, "end": line.d_city_name, "global": line.extra_info["g"], "date": line.extra_info["date"], } ua = random.choice(BROWSER_USER_AGENT) headers = { "User-Agent": ua, "Content-Type": "application/x-www-form-urlencoded" } url = pre + urllib.urlencode(params) try: r = requests.get(url, headers=headers, data=params, timeout=15) soup = bs(r.content, 'lxml') info = soup.find('table', attrs={ 'class': 'resulttb' }).find_all('tbody', attrs={'class': 'rebody'}) except: result_info = {} result_info.update(result_msg="exception_ok", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info crawl_source = "hn96520" now = dte.now() tpk = now + datetime.timedelta(hours=1.2) update_attrs = {} ft = Line.objects.filter(s_city_name=line.s_city_name, d_city_name=line.d_city_name, drv_date=line.drv_date) t = {x.line_id: x for x in ft} s_city_name = line.s_city_name update_attrs = {} for x in info: try: bus_num = x.find('td', attrs={ 'align': 'center' }).get_text().strip() d_city_name = x.find_all('td')[1].get_text().split()[1] drv_date = x.find_all('td')[2].get_text().strip() drv_time = x.find_all('td')[3].get_text().strip() drv_datetime = dte.strptime("%s %s" % (drv_date, drv_time), "%Y-%m-%d %H:%M") full_price = float(x.find_all('td')[7].get_text().strip()) left_tickets = int(x.find_all('td')[8].get_text().strip()) sts = x.find_all('td')[9].img.get('src', '') line_id_args = { 's_city_name': s_city_name, 'd_city_name': d_city_name, 'bus_num': bus_num, 'crawl_source': crawl_source, 'drv_datetime': drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(bus_num)s-%(crawl_source)s" % line_id_args) if line_id in t: t[line_id].update( **{ "left_tickets": left_tickets, 'full_price': full_price, "refresh_datetime": now }) if line_id == line.line_id and ('images/bt_yd1.png' != sts ) and tpk < line.drv_datetime: update_attrs = { "left_tickets": left_tickets, 'full_price': full_price, "refresh_datetime": now } except Exception as e: print(e) result_info = {} if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line(self, line): result_info = { "result_msg": "", "update_attrs": {}, } now = dte.now() # rebot = KuaibaWapRebot.get_one() # if not rebot.test_login_status(): # rebot.login() # rebot.reload() # headers = rebot.http_header() # cookies = json.loads(rebot.cookies) ua = random.choice(MOBILE_USER_AGENG) headers = {"User-Agent": ua} url = "http://m.daba.cn/jsp/line/newlines.jsp" res = requests.get(url, headers=headers) base_url = 'http://m.daba.cn' line_url = base_url + re.findall(r'query_station : (.*),', res.content)[0][1:-1] params = { "endTime": line.extra_info['endTime'], "startCity": line.s_city_name, "startStation": line.s_sta_name, "arriveCity": line.d_city_name, "arriveStation": line.d_sta_name, "startDate": line.drv_date, "startTime": line.extra_info['startTime'], } line_url = "%s&%s" % (line_url, urllib.urlencode(params)) r = requests.get(line_url, headers=headers) res = r.json() now = dte.now() if res["code"] != 0: result_info.update(result_msg="error response", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info busTripInfoSet = res['data'].get('busTripInfoSet', []) cityOpenSale = res['data']['cityOpenSale'] if not cityOpenSale or len(busTripInfoSet) == 0: result_info.update(result_msg=" not open sale or not line list", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info update_attrs = {} for d in busTripInfoSet: drv_datetime = dte.strptime( "%s %s" % (line.drv_date, d["time"][0:-3]), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "bus_num": d['id'], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(bus_num)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue tickets = d['tickets'] if d['tickets'] == 0 or d['tempClose'] == 1: tickets = 0 info = { "full_price": float(d["price"]), "fee": 0, "left_tickets": tickets, "refresh_datetime": now, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line_by_app(self, line): result_info = { "result_msg": "", "update_attrs": {}, } line_url = "http://www.vchepiao.cn/mb/base/bus/queryBusSKY" params = { "fromCity": "深圳", "stationCode": line.s_sta_id, "dstNode": line.d_city_name, "schDate": line.drv_date.replace('-', '') } headers = { "User-Agent": random.choice(BROWSER_USER_AGENT), "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "X-Requested-With": "XMLHttpRequest", "accept": "application/json" } now = dte.now() try: r = requests.post(line_url, data=urllib.urlencode(params), headers=headers) res = r.json() except: result_info.update(result_msg="exception_ok", update_attrs={ "left_tickets": 5, "refresh_datetime": now }) line_log.info("%s\n%s", "".join(traceback.format_exc()), locals()) return result_info if not res["success"]: result_info.update(result_msg="error response", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) return result_info update_attrs = {} for d in res["data"]["list"]: if d['SchStat'] == '1': drv_datetime = dte.strptime( "%s %s" % (d["SchDate"], d["orderbytime"]), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": d["SchWaitStName"], "d_sta_name": d["SchNodeName"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue info = { "full_price": float(d["SchStdPrice"]), "fee": 0, "left_tickets": int(d["SchSeatCount"]), "refresh_datetime": now, "extra_info": { "raw_info": d }, } if line_id == line.line_id: update_attrs = info else: if obj.left_tickets == 0: continue obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info
def do_refresh_line_new(self, line): result_info = { "result_msg": "", "update_attrs": {}, } now = dte.now() line_url = "http://www.changtu.com/chepiao/newQuerySchList.htm" sta_city_id, s_pinyin = line.s_city_id.split("|") d_end_type, d_pinyin, end_city_id = line.d_city_id.split("|") params = dict( endTypeId=d_end_type, endId=end_city_id, planDate=line.drv_date, startCityUrl=s_pinyin, endCityUrl=d_pinyin, querySch=0, startCityId=sta_city_id, endCityId=end_city_id, ) url = "%s?%s" % (line_url, urllib.urlencode(params)) headers = {"User-Agent": random.choice(BROWSER_USER_AGENT)} rebot = ChangtuWebRebot.get_one() try: rebot = ChangtuWebRebot.get_one() r = rebot.http_get(url, headers=headers) res = r.json() except: result_info.update(result_msg="exception_ok", update_attrs={ "left_tickets": 2, "refresh_datetime": dte.now() }) line_log.info("%s\n%s", "".join(traceback.format_exc()), locals()) return result_info update_attrs = {} for d in res["schList"]: if int(d["bookFlag"]) != 2: continue drv_datetime = dte.strptime("%s %s" % (d["drvDate"], d["drvTime"]), "%Y-%m-%d %H:%M") line_id_args = { "s_city_name": line.s_city_name, "d_city_name": line.d_city_name, "s_sta_name": d["localCarrayStaName"], "d_sta_name": d["stopName"], "crawl_source": line.crawl_source, "drv_datetime": drv_datetime, } line_id = md5( "%(s_city_name)s-%(d_city_name)s-%(drv_datetime)s-%(s_sta_name)s-%(d_sta_name)s-%(crawl_source)s" % line_id_args) try: obj = Line.objects.get(line_id=line_id) except Line.DoesNotExist: continue extra_info = { "id": d["id"], "getModel": d["getModel"], "ticketTypeStr": d["ticketTypeStr"], "stationMapId": d["stationMapId"] } info = { "full_price": float(d["fullPrice"]), "fee": 0, "left_tickets": int(d["seatAmount"]), "refresh_datetime": now, "extra_info": extra_info, } if line_id == line.line_id: update_attrs = info else: obj.update(**info) if not update_attrs: result_info.update(result_msg="no line info", update_attrs={ "left_tickets": 0, "refresh_datetime": now }) else: result_info.update(result_msg="ok", update_attrs=update_attrs) return result_info