chaoxiang_get_deviceno_from_list.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import os, sys,time,datetime
  4. import urllib
  5. import requests
  6. from mongoengine import register_connection, PointField, DynamicDocument, StringField
  7. import simplejson as json
  8. import simplejson as json
  9. import base64
  10. import sys
  11. import json
  12. import base64
  13. import urllib2
  14. from urllib import quote_plus
  15. from urllib2 import urlopen
  16. from urllib2 import Request
  17. from urllib2 import URLError
  18. from urllib import urlencode
  19. from django.db.models.fields import DateTimeField
  20. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  21. sys.path.insert(0, PROJECT_ROOT)
  22. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs.testing")
  23. from script.base import init_env
  24. init_env(interactive = False)
  25. from apps.web.core.db import Searchable
  26. import pyautogui
  27. import qrcode
  28. IS_PY3 = sys.version_info.major == 3
  29. if IS_PY3:
  30. from urllib.request import urlopen
  31. from urllib.request import Request
  32. from urllib.error import URLError
  33. from urllib.parse import urlencode
  34. from urllib.parse import quote_plus
  35. else:
  36. import urllib2
  37. from urllib import quote_plus
  38. from urllib2 import urlopen
  39. from urllib2 import Request
  40. from urllib2 import URLError
  41. from urllib import urlencode
  42. # 防止https证书校验不正确
  43. import ssl
  44. # while True:
  45. # a = pyautogui.getWindowsWithTitle(u'图片查看')
  46. # time.sleep(1)
  47. ssl._create_default_https_context = ssl._create_unverified_context
  48. # 利用百度APP,直接解析截图中的地址,以及端口编号。
  49. API_KEY = 'OVcN78LP40CBEwWk5REF2Hyu'
  50. SECRET_KEY = 'a7luZBdbzjsfU9oE2GD3yPeTBgPty03t'
  51. OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
  52. TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token'
  53. register_connection(alias = 'spider',
  54. name = 'spider',
  55. host = '211.159.224.10',
  56. port = 27119,
  57. username = 'service',
  58. password = 'oOzjoQcO5DyyiN97AY0NpzJ6vztjNpx5',
  59. authentication_source = 'admin')
  60. class yongxing(Searchable):
  61. devNo = StringField(default = '')
  62. phone = StringField(default = '')
  63. meta = {
  64. 'collection': 'yongxing',
  65. 'db_alias': 'spider',
  66. 'unique_together': {'devNo'}
  67. }
  68. def fetch_token():
  69. params = {'grant_type': 'client_credentials',
  70. 'client_id': API_KEY,
  71. 'client_secret': SECRET_KEY}
  72. post_data = urlencode(params)
  73. if (IS_PY3):
  74. post_data = post_data.encode('utf-8')
  75. req = Request(TOKEN_URL, post_data)
  76. try:
  77. f = urlopen(req, timeout=5)
  78. result_str = f.read()
  79. except URLError as err:
  80. print(err)
  81. if (IS_PY3):
  82. result_str = result_str.decode()
  83. result = json.loads(result_str)
  84. if ('access_token' in result.keys() and 'scope' in result.keys()):
  85. if not 'brain_all_scope' in result['scope'].split(' '):
  86. print ('please ensure has check the ability')
  87. exit()
  88. return result['access_token']
  89. else:
  90. print ('please overwrite the correct API_KEY and SECRET_KEY')
  91. exit()
  92. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" # accurate_basic
  93. def read_file(image_path):
  94. f = None
  95. try:
  96. f = open(image_path, 'rb')
  97. return f.read()
  98. except:
  99. print('read image file fail')
  100. return None
  101. finally:
  102. if f:
  103. f.close()
  104. def request(url, data):
  105. req = Request(url, data.encode('utf-8'))
  106. has_error = False
  107. try:
  108. f = urlopen(req)
  109. result_str = f.read()
  110. if (IS_PY3):
  111. result_str = result_str.decode()
  112. return result_str
  113. except URLError as err:
  114. print(err)
  115. # 二进制方式打开图片文件
  116. # 获取access token
  117. token = fetch_token()
  118. # 拼接通用文字识别高精度url
  119. #
  120. def get_phone(devNo,path):
  121. try:
  122. f = open(path, 'rb')
  123. img = base64.b64encode(f.read())
  124. if f:
  125. f.close()
  126. # 调用文字识别服务
  127. params = {"image":img}
  128. request_url1 = request_url + "?access_token=" + token
  129. headers = {'content-type': 'application/x-www-form-urlencoded'}
  130. response = requests.post(request_url1, data=params, headers=headers)
  131. # 解析返回结果
  132. result_json = response.json()
  133. for words_result in result_json["words_result"]:
  134. text = words_result["words"]
  135. if u'打' in text and len(text)>7:
  136. phone = text.replace(u'打',"")
  137. print u'获取到一条电话!!',devNo,phone
  138. try:
  139. yongxing(devNo,phone).save()
  140. break
  141. except Exception,e:
  142. break
  143. except Exception,e:
  144. print u"解析图片报错了!!!!",e
  145. moniqi = pyautogui.getWindowsWithTitle(u'逍遥模拟器')[0]
  146. if not moniqi:
  147. break
  148. moniqi.activate()
  149. # 点击文件按钮
  150. pyautogui.click(x=762, y=767)
  151. time.sleep(0.2)
  152. # 输入二维码文件名称
  153. pyautogui.click(x=1083, y=928)
  154. pyautogui.write("%s" % jj)
  155. time.sleep(0.2)
  156. # 选择该文件,传到编辑框
  157. with pyautogui.hold('alt'):
  158. pyautogui.press('o')
  159. time.sleep(0.1)
  160. # 双击打开二维码
  161. pyautogui.doubleClick(x=791, y=816)
  162. time.sleep(1)
  163. # 右键打开
  164. pyautogui.rightClick(x=983,y=468)
  165. time.sleep(0.1)
  166. # 点击识别二维码
  167. pyautogui.click(x=1084,y=553)
  168. # 等待小程序启动界面出来.如果
  169. time.sleep(1.7)
  170. title = pyautogui.getActiveWindowTitle()
  171. timeCount = 0
  172. if title != u'富连e充':
  173. print u" 无法拉起来小程序"
  174. time.sleep(1800)
  175. # 截图
  176. file1 = pyautogui.screenshot(region=(758,113,402,765))
  177. picPath = u'Q:/友商信息/涌鑫/小程序截图/' + '%s.png' % int(time.time()*1000)
  178. file1.save(picPath)
  179. get_phone('1000%04d' % jj, picPath)
  180. # 加按几次esc,用于消掉一些提示
  181. pyautogui.press('esc')
  182. time.sleep(0.5)
  183. # # 点击关闭小程序
  184. # pyautogui.click(x=1135, y=124)
  185. # time.sleep(0.1)
  186. #
  187. # # 关闭截图
  188. # pyautogui.click(x=983,y=468)
  189. # time.sleep(0.2)
  190. # pyautogui.press('esc')
  191. # time.sleep(0.2)
  192. # 删除截图
  193. # pyautogui.click(x=791, y=816)
  194. weixin = pyautogui.getWindowsWithTitle(u'微信')[0]
  195. weixin.activate()
  196. pyautogui.click(x=1413, y=800)
  197. time.sleep(0.5)
  198. with pyautogui.hold('ctrl'):
  199. pyautogui.press('a')
  200. pyautogui.press('delete')
  201. # time.sleep(0.1)
  202. #
  203. #
  204. # while True:
  205. # a= auto.GetFocusedControl()
  206. # time.sleep(2)
  207. # print a
  208. # print a.Name
  209. # time.sleep(2)
  210. # while True:
  211. # pyautogui.moveTo(987,574)
  212. # file1 = pyautogui.screenshot(region=(766,220,412,575))
  213. # file1.save(u'Q:/友商信息/汽车桩/星星充电/站列表截图/%s.png' % int(time.time()*1000))
  214. # ii += 1
  215. # print ii
  216. # pyautogui.scroll(-400)
  217. # time.sleep(0.2)
  218. print 'OK'