Skip to content

Commit

Permalink
1. add log file
Browse files Browse the repository at this point in the history
2. add image scale
3. add sougou
4. use multipleprocess
  • Loading branch information
smileboywtu committed Jan 21, 2018
1 parent aa3a032 commit 6ea5de3
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 410 deletions.
3 changes: 3 additions & 0 deletions config.py
Expand Up @@ -36,3 +36,6 @@

### 是否使用夜神模拟器
use_monitor = config["use_monitor"]

### 是否图片缩放
enable_scale = config["enable_scale"]
5 changes: 4 additions & 1 deletion config.yaml
Expand Up @@ -51,7 +51,10 @@ prefer:
- ocrspace

### enable chrome
enable_chrome: true
enable_chrome: false

### 是否使用夜神模拟器
use_monitor: false

### 是否图片缩放
enable_scale: true
25 changes: 6 additions & 19 deletions core/android.py
Expand Up @@ -17,6 +17,8 @@

from PIL import Image

from config import enable_scale

# SCREENSHOT_WAY 是截图方法,
# 经过 check_screenshot 后,会自动递
# 不需手动修改
Expand Down Expand Up @@ -59,13 +61,12 @@ def check_screenshot(filename, directory):
check_screenshot(filename=filename, directory=directory)


def analyze_current_screen_text(crop_area, directory=".", compress_level=1, use_monitor=False):
def analyze_current_screen_text(crop_area, directory=".", compress_level=1):
"""
capture the android screen now
:return:
"""
print("capture time: ", datetime.now().strftime("%H:%M:%S"))
screenshot_filename = "screenshot.png"
save_text_area = os.path.join(directory, "text_area.png")
capture_screen_v2(screenshot_filename, directory)
Expand All @@ -74,19 +75,6 @@ def analyze_current_screen_text(crop_area, directory=".", compress_level=1, use_
return get_area_data(save_text_area)


def analyze_stored_screen_text(screenshot_filename="screenshot.png", directory=".", compress_level=1):
"""
reload screen from stored picture to store
:param directory:
:param compress_level:
:return:
"""
save_text_area = os.path.join(directory, "text_area.png")
parse_answer_area(os.path.join(
directory, screenshot_filename), save_text_area, compress_level)
return get_area_data(save_text_area)


def capture_screen_v2(filename="screenshot.png", directory="."):
"""
can't use general fast way
Expand Down Expand Up @@ -152,10 +140,9 @@ def parse_answer_area(source_file, text_area_file, compress_level, crop_area):
image = image.convert("1")

width, height = image.size[0], image.size[1]
print("screen width: {0}, screen height: {1}".format(width, height))

region = image.crop(
(width * crop_area[0], height * crop_area[1], width * crop_area[2], height * crop_area[3]))
region = image.crop((width * crop_area[0], height * crop_area[1], width * crop_area[2], height * crop_area[3]))
if enable_scale:
region = region.resize((int(1080 / 3), int(1920 / 5)), Image.BILINEAR)
region.save(text_area_file)


Expand Down
27 changes: 13 additions & 14 deletions core/crawler/baiduzhidao.py
Expand Up @@ -5,19 +5,23 @@
Baidu zhidao searcher
"""
import logging
import operator
import random

import requests

from core.crawler import text_process as T
from utils import stdout_template

Agents = (
"Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0",
"Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.71 Safari/537.36"
)

logger = logging.getLogger("assistant")


def count_key_words(text, keywords):
total_count = 0
Expand Down Expand Up @@ -49,15 +53,15 @@ def baidu_count(keyword, answers, timeout=5):
"Connection": "keep-alive",
"Accept-Language": "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Upgrade-Insecure-Requests": "1",
# "User-Agent": random.choice(Agents)
"User-Agent": random.choice(Agents)
}
params = {
"wd": keyword,
"ie": "utf-8"
}
resp = requests.get("http://www.baidu.com/s", params=params, headers=headers, timeout=timeout)
resp = requests.get("http://www.baidu.com/s", params=params, headers=headers)
if not resp.ok:
print("baidu search error")
logger.error("Get BaiDu HTTP ERROR")
return {
ans: 0
for ans in answers
Expand Down Expand Up @@ -93,7 +97,6 @@ def baidu_count_daemon(exchage_queue, outputqueue, timeout=5):
:return:
"""

while True:
question, answers, true_flag = exchage_queue.get()
try:
Expand All @@ -107,13 +110,9 @@ def baidu_count_daemon(exchage_queue, outputqueue, timeout=5):
recommend = "{0}\n{1}".format(
"肯定回答( ): {0}".format(summary_li[0][0]),
"否定回答(**): {0}".format(summary_li[-1][0]))
outputqueue.put({
"type": 1,
"data": "{0}\n{1}".format(
"\n".join(map(lambda item: "{0}: {1}".format(item[0], item[1]), summary_li)),
recommend
)
})
except:
import traceback
traceback.print_exc()
outputqueue.put(stdout_template.BAIDU_TPL.format(
"\n".join(map(lambda item: "{0}: {1}".format(item[0], item[1]), summary_li)),
recommend
))
except Exception as e:
logger.error(str(e), exc_info=True)

0 comments on commit 6ea5de3

Please sign in to comment.