wanzhuangXiaochengxun.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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,Document
  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. #
  21. #
  22. # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs.testing")
  23. #
  24. # from script.base import init_env
  25. #
  26. # init_env(interactive = False)
  27. import pyautogui
  28. import qrcode
  29. IS_PY3 = sys.version_info.major == 3
  30. if IS_PY3:
  31. from urllib.request import urlopen
  32. from urllib.request import Request
  33. from urllib.error import URLError
  34. from urllib.parse import urlencode
  35. from urllib.parse import quote_plus
  36. else:
  37. import urllib2
  38. from urllib import quote_plus
  39. from urllib2 import urlopen
  40. from urllib2 import Request
  41. from urllib2 import URLError
  42. from urllib import urlencode
  43. # 防止https证书校验不正确
  44. import ssl
  45. import pytesseract
  46. from PIL import Image
  47. # while True:
  48. # a = pyautogui.getWindowsWithTitle(u'图片查看')
  49. # time.sleep(1)
  50. ssl._create_default_https_context = ssl._create_unverified_context
  51. # 利用百度APP,直接解析截图中的地址,以及端口编号。
  52. API_KEY = 'OVcN78LP40CBEwWk5REF2Hyu'
  53. SECRET_KEY = 'a7luZBdbzjsfU9oE2GD3yPeTBgPty03t'
  54. OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
  55. TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token'
  56. register_connection(alias = 'spider',
  57. name = 'spider',
  58. host = '211.159.224.10',
  59. port = 27119,
  60. username = 'service',
  61. password = 'oOzjoQcO5DyyiN97AY0NpzJ6vztjNpx5',
  62. authentication_source = 'admin')
  63. class wangzhuang(Document):
  64. devNo = StringField(default = '')
  65. phone = StringField(default = '')
  66. meta = {
  67. 'collection': 'wangzhuangXiaochengxu',
  68. 'db_alias': 'spider',
  69. 'unique_together': {'devNo'}
  70. }
  71. def fetch_token():
  72. params = {'grant_type': 'client_credentials',
  73. 'client_id': API_KEY,
  74. 'client_secret': SECRET_KEY}
  75. post_data = urlencode(params)
  76. if (IS_PY3):
  77. post_data = post_data.encode('utf-8')
  78. req = Request(TOKEN_URL, post_data)
  79. try:
  80. f = urlopen(req, timeout=5)
  81. result_str = f.read()
  82. except URLError as err:
  83. print(err)
  84. if (IS_PY3):
  85. result_str = result_str.decode()
  86. result = json.loads(result_str)
  87. if ('access_token' in result.keys() and 'scope' in result.keys()):
  88. if not 'brain_all_scope' in result['scope'].split(' '):
  89. print ('please ensure has check the ability')
  90. exit()
  91. return result['access_token']
  92. else:
  93. print ('please overwrite the correct API_KEY and SECRET_KEY')
  94. exit()
  95. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" # accurate_basic
  96. def read_file(image_path):
  97. f = None
  98. try:
  99. f = open(image_path, 'rb')
  100. return f.read()
  101. except:
  102. print('read image file fail')
  103. return None
  104. finally:
  105. if f:
  106. f.close()
  107. def request(url, data):
  108. req = Request(url, data.encode('utf-8'))
  109. has_error = False
  110. try:
  111. f = urlopen(req)
  112. result_str = f.read()
  113. if (IS_PY3):
  114. result_str = result_str.decode()
  115. return result_str
  116. except URLError as err:
  117. print(err)
  118. def get_zone_text(region):
  119. file1 = pyautogui.screenshot(region=region)
  120. picPath = u'Q:/temp_chaoxiang/' + '%s.jpg' % int(time.time()*1000)
  121. file1.save(picPath)
  122. string = pytesseract.image_to_string(Image.open(picPath), lang='chi_sim')
  123. print picPath,string
  124. return string.replace(' ','')
  125. def get_text_from_baidu(region):
  126. try:
  127. file1 = pyautogui.screenshot(region=region)
  128. picPath = u'Q:/temp_chaoxiang/' + '%s.jpg' % int(time.time()*1000)
  129. file1.save(picPath)
  130. f = open(picPath, 'rb')
  131. img = base64.b64encode(f.read())
  132. if f:
  133. f.close()
  134. # 调用文字识别服务
  135. params = {"image":img}
  136. request_url1 = request_url + "?access_token=" + token
  137. headers = {'content-type': 'application/x-www-form-urlencoded'}
  138. response = requests.post(request_url1, data=params, headers=headers)
  139. # 解析返回结果
  140. result_json = response.json()
  141. for words_result in result_json["words_result"]:
  142. text = words_result["words"]
  143. return text
  144. return ''
  145. except Exception,e:
  146. print u"解析图片报错了!!!!",e
  147. # 二进制方式打开图片文件
  148. # 获取access token
  149. token = fetch_token()
  150. def back_to_message():
  151. stringTitle = get_zone_text(region=(886,180,170,35))
  152. stringLeft = get_zone_text(region=(782,183,228,27))
  153. if u'陈军' in stringTitle:
  154. return True
  155. elif u'充电支付' in stringLeft:
  156. pyautogui.click(x=1167, y=198) # 直接点击关闭按钮
  157. time.sleep(1)
  158. elif u'通话' in stringLeft:
  159. pyautogui.click(x=1265, y=436)
  160. time.sleep(1)
  161. pyautogui.click(x=1265, y=436)
  162. time.sleep(1)
  163. stringTitle = get_zone_text(region=(886,180,170,35))
  164. if u'陈军' in stringTitle:
  165. return True
  166. else:
  167. return False
  168. # 从30万启动开始
  169. for ii in range(0,300000):
  170. jj = 300000 - ii
  171. print jj
  172. qrStr = 'https://www.ctiot.info/d/18%06d' % jj
  173. img =qrcode.make(qrStr)
  174. filePath = "Q:/temp/%s" % jj
  175. img.save(filePath)
  176. time.sleep(0.2)
  177. weixin = pyautogui.getWindowsWithTitle(u'微信')[0]
  178. if not weixin:
  179. break
  180. weixin.activate()
  181. time.sleep(3)
  182. # 点击文件按钮
  183. pyautogui.click(x=761, y=777)
  184. time.sleep(0.2)
  185. # 输入二维码文件名称
  186. pyautogui.click(x=1083, y=928)
  187. pyautogui.write("%s" % jj)
  188. time.sleep(0.2)
  189. # 选择该文件,传到编辑框
  190. with pyautogui.hold('alt'):
  191. pyautogui.press('o')
  192. time.sleep(0.1)
  193. # 发消息
  194. with pyautogui.hold('alt'):
  195. pyautogui.press('s')
  196. time.sleep(0.1)
  197. # --------------------------------- 手机这边识别二维码-------------------------
  198. shouji = pyautogui.getWindowsWithTitle(u'手机管理器')[0]
  199. if not shouji:
  200. break
  201. shouji.activate()
  202. time.sleep(3)
  203. if not back_to_message():
  204. print u'无法定位到消息接受窗口'
  205. break
  206. # 点击图片
  207. pyautogui.click(x=1067, y=782)
  208. time.sleep(1)
  209. # 点击图片的二维码识别按钮
  210. pyautogui.click(x=1025, y=864)
  211. time.sleep(3)
  212. string = get_zone_text(region=(782,183,228,27))
  213. if u'充电支付' not in string:
  214. print u'没有启动小程序,需要等等吧'
  215. time.sleep(1800)
  216. string = get_zone_text(region=(847,568,82,30))
  217. if u'消' in string:
  218. pyautogui.click(x=847, y=568)
  219. time.sleep(0.2)
  220. # devNo = get_text_from_baidu(region=(855,278,68,21))
  221. # if devNo and devNo != '18%06d' % jj:
  222. # print u'设备编号错误,可能是二维码没有发过来',devNo,jj
  223. # break
  224. string = get_text_from_baidu(region=(1122,262,72,40))
  225. if not u'电话' in string :
  226. continue
  227. # 点击联系方式
  228. pyautogui.click(x=1154, y=278)
  229. time.sleep(1)
  230. #解析电话
  231. string = get_zone_text(region=(813,431,331,46))
  232. if not string:
  233. continue
  234. try:
  235. newObj = wangzhuang(str(jj),string)
  236. newObj.save()
  237. print u'搞到一条信息!!!',string
  238. except Exception,e:
  239. continue
  240. print 'OK'