-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (53 loc) · 1.75 KB
/
main.py
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
import warnings, time
from urllib3.exceptions import NotOpenSSLWarning
import requests
import json
from selenium import webdriver
from bs4 import BeautifulSoup
import os, sys
import tweepy
from dotenv import load_dotenv
import urllib.request
from urllib.parse import urlparse
from scraper import scrape_article
from utils import transform_url_imore, get_url, delete_image, download_image, extract_answer, log
from twitter import send_tweet
from ai import create_prompt, generate_text, translate
load_dotenv()
warnings.filterwarnings("ignore", category=UserWarning)
warnings.simplefilter('ignore', NotOpenSSLWarning)
MODE = "AI" # Customizable but it automatically chooses beteeen "AI" or "CLASSIC"
def set_mode(new_mode):
global MODE
MODE = new_mode
url = get_url()
article = scrape_article(url)
title = article["title"]
content = article["content"]
image_url = article["image_url"]
download_image(image_url, "image.jpg")
if len(translate(title) + translate(content)) < 274:
MODE="CLASSIC"
print("CLASSIC mode enabled")
if MODE == "AI":
titulo = translate(title)
contenido = translate(content)
prompt = create_prompt(titulo, contenido)
answer = None
while answer is None or len(answer) > 275:
response = generate_text(prompt)
answer = extract_answer(response)
log(translate(title), image_url, translate(content))
print("AI -> Answer as follows:")
print(answer)
send_tweet(answer, "./image.jpg")
elif MODE == "CLASSIC":
titulo = translate(title)
contenido = translate(content)
answer = "🆕 " + titulo + "\n\n" + contenido
log(titulo, image_url, contenido)
print("CLASSIC -> Answer as follows:")
print(answer)
send_tweet(answer, "./image.jpg")
delete_image("./image.jpg")
MODE = "AI"