Skip to content

Commit

Permalink
Refactor almost complete
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigoTcarmo committed Jan 16, 2022
1 parent d87cb1d commit 90afb1c
Show file tree
Hide file tree
Showing 36 changed files with 186 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import textwrap
import os
from Lists.img_list import PHILOSOPHERS_LIST
from Templates.New_Img_Manipulation.reference import TEMPLATES_PATH
from Templates.New_Img_Manipulation.path_reference import TEMPLATES_PATH

# loads .env
dotenv.load_dotenv(dotenv.find_dotenv())
Expand Down Expand Up @@ -182,7 +182,7 @@ async def clear(ctx, numero):
@client.command() # função principal
async def philobot(ctx, *, mensagem):
try:
img = Image.open(f'{TEMPLATES_PATH}/layer_1.png')
img = Image.open(f'{TEMPLATES_PATH}/background_image.png')
font = ImageFont.truetype(os.getenv('myriad_font'), 50)
drawing = ImageDraw.Draw(img)

Expand Down
Binary file added Font/OpenSansEmoji.ttf
Binary file not shown.
Binary file added Font/twitterchirp.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes
3 changes: 3 additions & 0 deletions Templates/New_Img_Manipulation/path_reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

template_folder_reference = os.path.dirname(os.path.abspath(__file__))
3 changes: 0 additions & 3 deletions Templates/New_Img_Manipulation/reference.py

This file was deleted.

File renamed without changes
Binary file added Test/ipre_final_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Text/new_img_treatment/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tweepy
import os
from Credentials.Twitter.Test import API_MAIN_TEST
from Templates.New_Img_Manipulation.reference import TEMPLATES_PATH
from Templates.New_Img_Manipulation.path_reference import TEMPLATES_PATH

api = API_MAIN_TEST

Expand Down
2 changes: 1 addition & 1 deletion Twitter/Hashtag/philobot_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from PIL import Image, ImageFont
from Twitter.Hashtag.functionalities import Functionalities
from Lists.img_list import PHILOSOPHERS_LIST
from Templates.New_Img_Manipulation.reference import TEMPLATES_PATH
from Templates.New_Img_Manipulation.path_reference import TEMPLATES_PATH
# from Logs.Twitter.log_engine import log_hashtag


Expand Down
2 changes: 1 addition & 1 deletion Twitter/Hashtag/philomaker_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from urllib import request
from PIL import Image, ImageFont
from Lists.error_img_list import PHILOMAKER_ERROR_IMAGE_COLLECTION
from Templates.New_Img_Manipulation.reference import TEMPLATES_PATH
from Templates.New_Img_Manipulation.path_reference import TEMPLATES_PATH
from Logs.Twitter.logger_hashtag import log_hashtag
import os
from Twitter.Hashtag.functionalities import Functionalities
Expand Down
185 changes: 165 additions & 20 deletions Twitter/Posting/new_posting.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,178 @@
import schedule, time, os, random, tweepy
from PIL import Image, ImageFont
import schedule, time, os, random, tweepy, re, textwrap, datetime, demoji
import emoji
from PIL import Image, ImageFont, ImageDraw
from Lists.accounts import accounts_list
from Lists.img_list import PHILOSOPHERS_LIST
from Credentials.Twitter.Test.test_credentials import API_TEST
from Templates.New_Img_Manipulation.path_reference import template_folder_reference
from Twitter.Posting.uploads.path_reference import upload_folder_reference
from dotenv import load_dotenv


class PostingClass:
def __init__(self):
self.FONT_PATH = os.getenv('myriad_font')
load_dotenv()

self.uploads_path = upload_folder_reference

self.date_today = datetime.datetime.now().strftime('%Y%m%d')
self.time_now = datetime.datetime.now().strftime('%H-%M-%S')

self.posting_image_name = self.time_now + '_posting.png'
self.finish_image = fr"{self.uploads_path}\{self.date_today}\{self.posting_image_name}"

self.philosopher_path = random.choice(PHILOSOPHERS_LIST)
self.philosopher_name_raw = os.path.basename(self.philosopher_path).replace('.png', '')
self.philosopher_name = self.clear_philosopher_name()
self.tweet_text = None
self.tweet_id = None
self.api = API_TEST
pass
self.philosopher_data = {}
self.name_font_path = r'C:\Users\rodri\Documents\GitHub\The-Philosopher-BOT\Font\times.ttf'
self.text_font_path = r'C:\Users\rodri\Documents\GitHub\The-Philosopher-BOT\Font\OpenSansEmoji.ttf'

def start_posting(self):
# 1 - coletar o texto de algum tweet
self.create_uploads_folder()

self.tweet_text = self.select_tweet()

self.emoji_check()

breakpoint()

print(self.tweet_text)
get_unicode = self.tweet_text.encode('unicode-escape').decode('ASCII')
print(get_unicode)

self.text_setting(raw_image=self.build_image())

self.upload_image()

def create_uploads_folder(self):
try:
self.uploads_path = self.uploads_path + '/'
os.mkdir(self.uploads_path + self.date_today)
print('CRIADO!')

except FileExistsError:
print('Arquivo ja existe')

except Exception as create_uploads_folder_err:
print(create_uploads_folder_err)

def get_tweet(self):
tweet_data = self.api.user_timeline(screen_name='syscat_13',
count=1,
tweet_mode='extended',
contributor_details=True,
include_entities=True,
include_rts=False,
trim_user=True,
exclude_replies=True)

for tweet in tweet_data:
self.tweet_text = re.sub(r'http\S+', '', tweet.full_text).replace('\n', ' ')
self.tweet_id = tweet.id
time.sleep(2)

def select_tweet(self):
while True:
self.get_tweet()

if self.tweet_text:
return self.tweet_text

# time.sleep(10)
# implementar funcionalidade de mudar de conta escolhida caso o tweet venha em branco

def emoji_check(self):
if emoji.emoji_count(self.tweet_text) > 0:
print(emoji.demojize(self.tweet_text))















def select_name_font(self):
if len(self.philosopher_name) <= 20:
return ImageFont.truetype(self.name_font_path, size=35)

else:
return ImageFont.truetype(self.name_font_path, size=30)

def select_text_font(self):
if len(self.tweet_text) <= 140:
wrapper_limit = 21
print('menos que 140')
return [ImageFont.truetype(self.text_font_path, size=45), wrapper_limit]

else:
wrapper_limit = 30
return [ImageFont.truetype(self.text_font_path, size=32), wrapper_limit]

def text_setting(self, raw_image):
text_adjust = self.select_text_font()

raw_image_draw = ImageDraw.Draw(raw_image)

raw_image_draw.text(xy=(43, 500), text=textwrap.fill(str('- ' + self.philosopher_name), 30),
fill=(255, 255, 255), font=self.select_name_font())

print(self.tweet_text)
# breakpoint()
# raw_image_draw.text(xy=(68, 120), text=textwrap.fill(self.tweet_text, text_adjust[1]), fill=(255, 255, 255),
# font=text_adjust[0])

raw_image_draw.text(xy=(68, 120), text=self.tweet_text, fill=(255, 255, 255),
font=text_adjust[0])

raw_image.save(self.finish_image)

def clear_philosopher_name(self):
if '(2)' in self.philosopher_name_raw:
return self.philosopher_name_raw.replace('(2)', '')

else:
return self.philosopher_name_raw

def build_image(self):
raw_image = Image.open(f'{template_folder_reference}/background_image.png')

philosopher_image = Image.open(self.philosopher_path).resize((449, 584))
raw_image.paste(philosopher_image, (629, 0))

smooth_template = Image.open(f'{template_folder_reference}/smooth_background.png')
raw_image.paste(smooth_template, (0, 0), smooth_template)

# build_image.save(r'C:\Users\rodri\Documents\GitHub\The-Philosopher-BOT\Test\image_smooth_test.png')

return raw_image

def get_tweets(self):
return self.api.user_timeline(screen_name='rodrigoblock4',
count=1,
tweet_mode='extended',
contributor_details=True,
include_entities=True,
include_rts=False,
trim_user=True,
exclude_replies=True)
def upload_image(self):
try:
upload_image = self.api.update_with_media(self.finish_image)

def post_engine(self):
# new_image = Image.new('RGB', (269, 194))
# font = ImageFont.truetype(self.FONT_PATH, 1)
v1 = random.choice(PHILOSOPHERS_LIST)
v2 = os.path.basename(v1).replace('.png', '')
tweet_author = self.api.update_status(
f'Tweet original: twitter.com/syscat_13/status/{self.tweet_id}',
upload_image.id,
include_entities=True)

except Exception as upload_err:
print('error')
print(upload_err)

print(v2)
print('Tweet enviado!')

def start_timer_test(self):
schedule.every(2).seconds.do(self.post_engine)
Expand All @@ -42,4 +186,5 @@ def select_account():


if __name__ == '__main__':
PostingClass().post_engine()
p = PostingClass()
p.start_posting()
2 changes: 1 addition & 1 deletion Twitter/Posting/posting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from PIL import Image, ImageDraw, ImageFont
from Lists.accounts import accounts_list
from Lists.img_list import PHILOSOPHERS_LIST
from Templates.New_Img_Manipulation.reference import TEMPLATES_PATH
from Templates.New_Img_Manipulation.path_reference import TEMPLATES_PATH

dotenv.load_dotenv(dotenv.find_dotenv())

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
3 changes: 3 additions & 0 deletions Twitter/Posting/uploads/path_reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

upload_folder_reference = os.path.dirname(os.path.abspath(__file__))
9 changes: 9 additions & 0 deletions teste.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
emoji_folder = r'C:\Users\rodri\Documents\The-Philosopher-BOT-2.5.3\TWITTER\v2.5.2\Emojis'
import os

files = os.listdir(emoji_folder)

# print(u'\U0001f603')
for e in files:
new_e = e.replace('.png', '')
print("\" + ffU000{new_e})

0 comments on commit 90afb1c

Please sign in to comment.