From 3e7c2e260384a3e381f01717fd71a694b71cae6e Mon Sep 17 00:00:00 2001 From: carp <25677564+carp@users.noreply.github.com> Date: Mon, 13 Jul 2020 13:29:50 -0400 Subject: code format --- bot.py | 12 +++++++++--- provision.py | 20 +++++++++----------- utils.py | 45 ++++++++++++++++++++++++++++++--------------- 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/bot.py b/bot.py index 4f3eb63..ff27cda 100644 --- a/bot.py +++ b/bot.py @@ -13,6 +13,7 @@ import random import os from api import consumer_key, consumer_secret, access_token, access_token_secret from datetime import datetime + twitter = Twython(consumer_key, consumer_secret, access_token, access_token_secret) @@ -31,7 +32,9 @@ def post(): lines = messages_multiline(text, font, image) # split up lines for text wrapping colors = get_colors(image.filename) # get colors - (x, y,faces) = randomize_location(image, lines, font) # where to start drawing text + (x, y, faces) = randomize_location( + image, lines, font + ) # where to start drawing text for line in lines: height = font.getsize(line[1])[1] @@ -46,7 +49,10 @@ def post(): twitter.update_status(status=message, media_ids=[response["media_id"]]) photo.close() os.remove(photo.name) - with open("log","a") as f: - f.write(f"{datetime.now().strftime('%d-%m-%Y %H:%M:%S')}\t{filename}\t({image.size[0]} {image.size[1]})\t{font.size} ({max((font.size // 25), 2)})\t{text}\n") + with open("log", "a") as f: + f.write( + f"{datetime.now().strftime('%d-%m-%Y %H:%M:%S')}\t{filename}\t({image.size[0]} {image.size[1]})\t{font.size} ({max((font.size // 25), 2)})\t{text}\n" + ) + post() diff --git a/provision.py b/provision.py index b995267..6c97a3c 100644 --- a/provision.py +++ b/provision.py @@ -2,19 +2,21 @@ import pandas import os import ast import twint -import requests +import requests + def download(url): - filename = url.split('/')[-1] + filename = url.split("/")[-1] r = requests.get(url, allow_redirects=True) - with open("pics/"+filename, 'wb') as f: + with open("pics/" + filename, "wb") as f: f.write(r.content) + # archive @AceYuriBot for images/sources c = twint.Config() c.Username = "AceYuriBot" -c.Images = True +c.Images = True c.Store_csv = True c.Output = "yuribot.csv" twint.run.Search(c) @@ -22,14 +24,10 @@ twint.run.Search(c) os.makedirs("pics", exist_ok=True) df = pandas.read_csv("yuribot.csv") source = ( - df["urls"] - .apply(lambda x: ast.literal_eval(x)) - .apply(lambda x: x[0] if x else None) -) -file_location = df["photos"].apply( - lambda x: os.path.basename(ast.literal_eval(x)[0]) + df["urls"].apply(lambda x: ast.literal_eval(x)).apply(lambda x: x[0] if x else None) ) +file_location = df["photos"].apply(lambda x: os.path.basename(ast.literal_eval(x)[0])) # save to file where bot will pull data from pandas.concat([source, file_location], axis=1).to_csv("files.csv") # download images -df["photos"].apply(lambda x: download(ast.literal_eval(x)[0])) \ No newline at end of file +df["photos"].apply(lambda x: download(ast.literal_eval(x)[0])) diff --git a/utils.py b/utils.py index 55a0623..2e0e7f3 100644 --- a/utils.py +++ b/utils.py @@ -4,31 +4,46 @@ from shapely.geometry import Polygon import cv2 import os.path import random -import subprocess +import subprocess import json from colorthief import ColorThief + def draw_with_border(x, y, message, color_fill, color_border, font, draw): border_width = max((font.size // 25), 2) - - for i in range(1,border_width+1): - draw.text((x,y+i), message, fill=color_border, font=font) - draw.text((x,y-i), message, fill=color_border, font=font) - draw.text((x+i,y), message, fill=color_border, font=font) - draw.text((x-i,y), message, fill=color_border, font=font) + for i in range(1, border_width + 1): + draw.text((x, y + i), message, fill=color_border, font=font) + draw.text((x, y - i), message, fill=color_border, font=font) + draw.text((x + i, y), message, fill=color_border, font=font) + draw.text((x - i, y), message, fill=color_border, font=font) - draw.text((x-i,y-i), message, fill=color_border, font=font) - draw.text((x-i,y+i), message, fill=color_border, font=font) - draw.text((x+i,y-i), message, fill=color_border, font=font) - draw.text((x+i,y+i), message, fill=color_border, font=font) + draw.text((x - i, y - i), message, fill=color_border, font=font) + draw.text((x - i, y + i), message, fill=color_border, font=font) + draw.text((x + i, y - i), message, fill=color_border, font=font) + draw.text((x + i, y + i), message, fill=color_border, font=font) draw.text((x, y), message, fill=color_fill, font=font) def detect(filename): - result = subprocess.run(['conda',"activate","detection","&&","python", 'anime-face-detector/main.py ',"-i",filename,"-o","output.json"],shell=True,stdout=subprocess.DEVNULL) - with open("output.json","r") as f: + result = subprocess.run( + [ + "conda", + "activate", + "detection", + "&&", + "python", + "anime-face-detector/main.py ", + "-i", + filename, + "-o", + "output.json", + ], + shell=True, + stdout=subprocess.DEVNULL, + ) + with open("output.json", "r") as f: output = json.load(f) print(output) return [f["bbox"] for f in output[filename]] @@ -96,7 +111,7 @@ def randomize_location(image, messages, font): placed = False tries = 0 faces = detect(image.filename) - print("faces found:",len(faces)) + print("faces found:", len(faces)) while placed is False and tries < 20: placed = True x = random.randrange(0, image_size[0] - x_coordinate) @@ -105,7 +120,7 @@ def randomize_location(image, messages, font): if is_intersected(face, (x, y, x + x_coordinate, y + y_coordinate)): placed = False tries = tries + 1 - print("tried:",tries) + print("tried:", tries) return (x, y, len(faces)) -- cgit v1.2.3