在gayhub上clone了别人的辅助,由跳一跳改动而来的,效果不太好,自己做了debug和改进并PR到作者。
代码分析 全部代码见gayhub:
不同分辨率适配 config采用json格式,使用起来非常方便。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 def open_accordant_config (): """ 调用配置文件 """ screen_size = _get_screen_size() print (screen_size) config_file = "{path}/config/{screen_size}/config.json" .format ( path=sys.path[0 ], screen_size=screen_size ) if os.path.exists(config_file): with open (config_file, 'r' ) as f: print ("Load config file from {}" .format (config_file)) return json.load(f) else : with open ('{}/config/default.json' .format (sys.path[0 ]), 'r' ) as f: print ("Load default config" ) return json.load(f)def _get_screen_size (): """ 获取手机屏幕大小 """ size_str = os.popen('adb shell wm size' ).read() if not size_str: print ('请安装 ADB 及驱动并配置环境变量' ) sys.exit() m = re.search(r'(\d+)x(\d+)' , size_str) if m: return "{width}x{height}" .format (height=m.group(2 ), width=m.group(1 )) return "1080x1920"
换用百度OCR 使用了百度aip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 get_text_from_image = partial(bai_get_text, app_id=config['app_id' ], app_key=config['app_key' ], app_secret=config['app_secret' ], api_version=config['api_version' ][0 ], timeout=5 ) im = Image.open (r"./screenshot.png" ) screen_end = time.time() print ('截图用时:' + str (screen_end - start) + '秒' ) region = (tuple (region_kind)) region = im.crop(region) region.save("./crop_test1.png" ) f = open ('./crop_test1.png' , 'rb' ) img_data = f.read() f.close() ocr_start =time.time() keyword = get_text_from_image( image_data=img_data, ) print (keyword)
网页抓取流程优化 省去了不需要的内容,提升速度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 def page (html ): soup = BeautifulSoup(html, 'lxml' ) results = [] result_set = soup.find(id ='content_left' ) if result_set is None : print ("抓取失败,跳过" ) else : result_set = result_set.find_all('div' , class_='c-container' ) for i in range (len (result_set)): result = result_set[i] if 'result-op' not in result['class' ]: r_from = result.find(class_='c-abstract' ) if not r_from: return None for em in r_from.find_all('em' ): em.unwrap() c_abstract = r_from.get_text() print (c_abstract) return
主程序流程优化 加入了流程的优化,把load config的时间提前,便于节约时间。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 import urllib.request, sys,base64,json,os,time,baiduSearch,screenshot,refrom PIL import Imagefrom common import configfrom functools import partialfrom common.baiduocr import get_text_from_image as bai_get_text index = input ("input: 百万英雄-1 冲顶大会-2 芝士超人-3\n" ) config = config.open_accordant_config() region_kind = config['region_million' ]if index=='1' : region_kind = config['region_million' ]elif index=='2' : region_kind = config['region_top' ]elif index=='3' : region_kind = config['region_super' ]else : print ('输入格式错误' ) get_text_from_image = partial(bai_get_text, app_id=config['app_id' ], app_key=config['app_key' ], app_secret=config['app_secret' ], api_version=config['api_version' ][0 ], timeout=5 )def start_answer (): input ('题目出现后按回车:\n' ) start = time.time() screenshot.pull_screenshot() im = Image.open (r"./screenshot.png" ) screen_end = time.time() print ('截图用时:' + str (screen_end - start) + '秒' ) region = (tuple (region_kind)) region = im.crop(region) region.save("./crop_test1.png" ) f = open ('./crop_test1.png' , 'rb' ) img_data = f.read() f.close() ocr_start =time.time() keyword = get_text_from_image( image_data=img_data, ) print (keyword) ocr_end = time.time() print ('OCR用时:' + str (ocr_end - ocr_start) + '秒' ) try : baiduSearch.search(keyword) except : print ('error' ) pass end = time.time() print ('程序用时:' + str (end - start) + '秒' ) print (keyword)while (1 ): start_answer()
PR流程
folk该repository
clone该repository
加入远程连接
git remote -v
git remote add upstream https://github.com/**/*.git
建立新的分支
git checkout -b name
修改commit 并push到新分支
发起PR “New pull requests”
等待merge
总结 程序可改进的 加入不同搜索引擎的词频,相关性等值,可以实现自动答题,目前已经有人实现了相关功能。
程序的复制性 整个程序复制性很强,pull截图拉到python里处理可以解决大部分手机的问题。