def generate_hosting_images(searchword_str, environment_str): print("# サーチワードからAnimemiruDBにあるタイトルディレクトリ郡を取得") title_dirs = get_title_dirs_from(searchword_str, environment_str) print("# title_dirが取得できなかった場合、そのtitle_dirの画像のみを比較対象とする") hosting_image_dirs = [] if len(title_dirs) == 0: print( f"### title_dir ###サーチワードからアニメを特定できませんでした。比較対象画像は全てになるため処理に時間がかかります。" ) hosting_image_dirs.append( str(rs.load_config(environment_str)["images_dir"])) else: print( f"### title_dir ### サーチワードからアニメを特定できました。比較対象画像を{title_dirs}に絞ります。") for title_dir in title_dirs: if title_dir != None: hosting_image_dirs.append( str(rs.load_config(environment_str)["images_dir"]) + title_dir + "/") print("# Animeiruが保有する画像オブジェクトを取得") hosting_images = [] for hosting_image_dir in hosting_image_dirs: for dir_path, dir_names, file_names in os.walk(hosting_image_dir): for file_name in file_names: pattern = ".*\.(jpg|png|bmp)" if re.search(pattern, file_name, re.IGNORECASE): hosting_images.append( HostingImage.HostingImage(file_name, dir_path + "/" + file_name)) return hosting_images
def delete_files(environment_str): search_image_dir = rs.load_config(environment_str)["search_image_dir"] if not os.path.exists(search_image_dir): os.makedirs(search_image_dir) for root, dirs, files in os.walk(search_image_dir, topdown=False): for name in files: os.remove(os.path.join(root, name))
def get_image_urls_from_google(searchword, environment_str): print("# Google画像検索URLを生成") url_keyword = urllib.parse.quote(searchword) search_url = 'https://www.google.com/search?hl=jp&q=' + url_keyword + '&btnG=Google+Search&tbs=0&safe=off&tbm=isch' print("# BeautifulSoupオブジェクトを生成") user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36' headers = {'User-Agent': user_agent} req = urllib.request.Request(search_url, None, headers) response = urllib.request.urlopen(req) html = response.read().decode("utf-8") soup = BeautifulSoup(html, 'html.parser') print("# BeatifulSoupオブジェクトから画像URL文字列郡を取得") how_many_images = int(rs.load_config(environment_str)["how_many_images"]) a_tags_filtered_jsname = soup.find_all( "div", attrs={"class": "rg_meta notranslate"}, limit=how_many_images) image_urls = [] for a_tag_filtered_jsname in a_tags_filtered_jsname: json_str = str(a_tag_filtered_jsname).replace( "<div class=\"rg_meta notranslate\" jsname=\"ik8THc\">", "").replace("</div>", "") json_obj = json.loads(json_str) image_urls.append(json_obj["ou"]) print(image_urls) return image_urls
def getConnection(env): configs = rs.load_config(env) try: connection = mysql.connector.connect( db=configs["db"], host=configs["host"], port=configs["port"], user=configs["user"], passwd=configs["password"], buffered=True) except: print("HOST:" + configs["host"] + " connetction error.") print(f"mysql接続エラーで処理終了しました。") # sys.exit(1) return connection
def post_slack_similar_image(environment_str, searchword): source_url = rs.load_config(environment_str)["source_url"] webhook_url = rs.load_config(environment_str)["webhook_url"] bot_name = rs.load_config(environment_str)["bot_name"] image_www_path = rs.load_config(environment_str)["image_www_path"] select_images = sim.similar_image(environment_str, searchword) for select_image in lh.unique(select_images): image_static_path = select_image.hosting_img_path.split("static/") titile_obj = Title.Title() titile_obj.title_generator(select_image.hosting_img_path, environment_str) message_text = f"""{image_www_path}/{image_static_path[1]}\n""" \ f"""```<img src=\"static/{image_static_path[1]}\" alt=\"\" width=\"480\" height=\"270\" """ \ f"""class=\"alignnone size-medium wp-image-598\" />\n""" \ f"""<a href=\"{titile_obj.title_source_url}\" target=\"_blank\" rel=\"noopener\"><div class=\"source\">""" \ f"""出典: {titile_obj.title_name} {titile_obj.title_source}</div></a>```""" post_slack_webhook(bot_name, message_text, webhook_url) print(message_text)
# -*- coding: utf-8 -*- import sys sys.path.append('..') from common import read_setting as rs # TODO ENVを実行環境に応じて動的に変更できるようにしたい。 ENV = "devel" API_TOKEN = rs.load_config(ENV)["bot_api_key"] DEFAULT_REPLY = "は?" PLUGINS = ['plugins']
def __init__(self, image_url, environment_str): self.image_url = image_url self.image_dir = rs.load_config(environment_str)["search_image_dir"] self.image_filename = os.path.basename(image_url) self.image_path = self.image_dir + self.image_filename