# -*- coding: utf-8 -*- # !/usr/bin/env python import os, sys,time,datetime import urllib import requests from mongoengine import register_connection, PointField, DynamicDocument, StringField import simplejson as json import simplejson as json import base64 import sys import json import base64 import urllib2 from urllib import quote_plus from urllib2 import urlopen from urllib2 import Request from urllib2 import URLError from urllib import urlencode from django.db.models.fields import DateTimeField PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..') sys.path.insert(0, PROJECT_ROOT) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs.testing") from script.base import init_env init_env(interactive = False) from apps.web.core.db import Searchable import pyautogui import qrcode IS_PY3 = sys.version_info.major == 3 if IS_PY3: from urllib.request import urlopen from urllib.request import Request from urllib.error import URLError from urllib.parse import urlencode from urllib.parse import quote_plus else: import urllib2 from urllib import quote_plus from urllib2 import urlopen from urllib2 import Request from urllib2 import URLError from urllib import urlencode # 防止https证书校验不正确 import ssl # while True: # a = pyautogui.getWindowsWithTitle(u'图片查看') # time.sleep(1) ssl._create_default_https_context = ssl._create_unverified_context # 利用百度APP,直接解析截图中的地址,以及端口编号。 API_KEY = 'OVcN78LP40CBEwWk5REF2Hyu' SECRET_KEY = 'a7luZBdbzjsfU9oE2GD3yPeTBgPty03t' OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic" TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token' register_connection(alias = 'spider', name = 'spider', host = '211.159.224.10', port = 27119, username = 'service', password = 'oOzjoQcO5DyyiN97AY0NpzJ6vztjNpx5', authentication_source = 'admin') class yongxing(Searchable): devNo = StringField(default = '') phone = StringField(default = '') meta = { 'collection': 'yongxing', 'db_alias': 'spider', 'unique_together': {'devNo'} } def fetch_token(): params = {'grant_type': 'client_credentials', 'client_id': API_KEY, 'client_secret': SECRET_KEY} post_data = urlencode(params) if (IS_PY3): post_data = post_data.encode('utf-8') req = Request(TOKEN_URL, post_data) try: f = urlopen(req, timeout=5) result_str = f.read() except URLError as err: print(err) if (IS_PY3): result_str = result_str.decode() result = json.loads(result_str) if ('access_token' in result.keys() and 'scope' in result.keys()): if not 'brain_all_scope' in result['scope'].split(' '): print ('please ensure has check the ability') exit() return result['access_token'] else: print ('please overwrite the correct API_KEY and SECRET_KEY') exit() request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" # accurate_basic def read_file(image_path): f = None try: f = open(image_path, 'rb') return f.read() except: print('read image file fail') return None finally: if f: f.close() def request(url, data): req = Request(url, data.encode('utf-8')) has_error = False try: f = urlopen(req) result_str = f.read() if (IS_PY3): result_str = result_str.decode() return result_str except URLError as err: print(err) # 二进制方式打开图片文件 # 获取access token token = fetch_token() # 拼接通用文字识别高精度url # def get_phone(devNo,path): try: f = open(path, 'rb') img = base64.b64encode(f.read()) if f: f.close() # 调用文字识别服务 params = {"image":img} request_url1 = request_url + "?access_token=" + token headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url1, data=params, headers=headers) # 解析返回结果 result_json = response.json() for words_result in result_json["words_result"]: text = words_result["words"] if u'打' in text and len(text)>7: phone = text.replace(u'打',"") print u'获取到一条电话!!',devNo,phone try: yongxing(devNo,phone).save() break except Exception,e: break except Exception,e: print u"解析图片报错了!!!!",e moniqi = pyautogui.getWindowsWithTitle(u'逍遥模拟器')[0] if not moniqi: break moniqi.activate() # 点击文件按钮 pyautogui.click(x=762, y=767) time.sleep(0.2) # 输入二维码文件名称 pyautogui.click(x=1083, y=928) pyautogui.write("%s" % jj) time.sleep(0.2) # 选择该文件,传到编辑框 with pyautogui.hold('alt'): pyautogui.press('o') time.sleep(0.1) # 双击打开二维码 pyautogui.doubleClick(x=791, y=816) time.sleep(1) # 右键打开 pyautogui.rightClick(x=983,y=468) time.sleep(0.1) # 点击识别二维码 pyautogui.click(x=1084,y=553) # 等待小程序启动界面出来.如果 time.sleep(1.7) title = pyautogui.getActiveWindowTitle() timeCount = 0 if title != u'富连e充': print u" 无法拉起来小程序" time.sleep(1800) # 截图 file1 = pyautogui.screenshot(region=(758,113,402,765)) picPath = u'Q:/友商信息/涌鑫/小程序截图/' + '%s.png' % int(time.time()*1000) file1.save(picPath) get_phone('1000%04d' % jj, picPath) # 加按几次esc,用于消掉一些提示 pyautogui.press('esc') time.sleep(0.5) # # 点击关闭小程序 # pyautogui.click(x=1135, y=124) # time.sleep(0.1) # # # 关闭截图 # pyautogui.click(x=983,y=468) # time.sleep(0.2) # pyautogui.press('esc') # time.sleep(0.2) # 删除截图 # pyautogui.click(x=791, y=816) weixin = pyautogui.getWindowsWithTitle(u'微信')[0] weixin.activate() pyautogui.click(x=1413, y=800) time.sleep(0.5) with pyautogui.hold('ctrl'): pyautogui.press('a') pyautogui.press('delete') # time.sleep(0.1) # # # while True: # a= auto.GetFocusedControl() # time.sleep(2) # print a # print a.Name # time.sleep(2) # while True: # pyautogui.moveTo(987,574) # file1 = pyautogui.screenshot(region=(766,220,412,575)) # file1.save(u'Q:/友商信息/汽车桩/星星充电/站列表截图/%s.png' % int(time.time()*1000)) # ii += 1 # print ii # pyautogui.scroll(-400) # time.sleep(0.2) print 'OK'