Skip to content

Commit

Permalink
Merge pull request #1 from FabienBounoir/refractor
Browse files Browse the repository at this point in the history
Change of script operation, now run once a day with channel rotation
  • Loading branch information
FabienBounoir authored Mar 28, 2023
2 parents 6c3ad7a + e06c75e commit f341ac5
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ node_modules
word_cloud
backup
tchatTranscript
image
image_backup
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Twouns_ est un projet passionnant qui se concentre sur le développement. Il gé




## Démarrer le projet

Pour demarrer le transcript Twitch
Expand All @@ -22,7 +21,9 @@ Pour générer l'image
python3 pythonScript/ImageGenerator.py
```

## Automatisation de l'exécution du script Python

Si vous voulez automatiser l'exécution d'un script Python sur une base quotidienne, vous pouvez utiliser le planificateur de tâches "Cron" disponible sur les systèmes d'exploitation Linux.


## Demo
Expand Down
File renamed without changes.
File renamed without changes.
Empty file added image/.gitkeep
Empty file.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const client = new tmi.Client({
// Enregistrement des messages de tchat dans un fichier
client.on('message', (channel, tags, message, self) => {
if (self) return; // ignore les messages envoyés par le bot lui-même
let date = new Date(); // récupération de la date

if (filteredBot.includes(tags.username) || (tags.username == (channel.replace("#", "")))) return; // ignore les messages envoyés par les bots filtrés
fs.appendFileSync(`./tchatTranscript/chat-${channel}-${date.getMonth() + 1}-${date.getFullYear()}.txt`, `${message}\n`); // enregistrement du message dans un fichier
fs.appendFileSync(`./tchatUser/pseudo-${channel}-${date.getMonth()}-${date.getFullYear()}.txt`, `${tags.username}\n`); // enregistrement du nom d'utilisateur dans un fichier
let channel = channel.replace("#", "")
if (filteredBot.includes(tags.username) || (tags.username == channel)) return; // ignore les messages envoyés par les bots filtrés
fs.appendFileSync(`./tchat/${channel}.txt`, `${message}\n`); // enregistrement du message dans un fichier
fs.appendFileSync(`./user/${channel}.txt`, `${tags.username}\n`); // enregistrement du nom d'utilisateur dans un fichier
});

// Connexion au client
Expand Down
Binary file added logoChannel/doigby-Doigby.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logoChannel/jltomy-JLTomy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logoChannel/lestream-LeStreamFR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logoChannel/littlebigwhale-LittleBigWhale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logoChannel/mastu-mastuu_.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions pythonScript/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Credential Twitter API -> https://developer.twitter.com/en/portal/products/elevated
API_KEY =
API_SECRET_KEY =
ACCESS_TOKEN =
ACCESS_TOKEN_SECRET =
133 changes: 71 additions & 62 deletions pythonScript/ImageGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@
import tweepy
import numpy as np
import matplotlib.pyplot as plt
import json
import shutil


from PIL import Image
from datetime import datetime, timedelta
from scipy.ndimage import gaussian_gradient_magnitude
from wordcloud import WordCloud, ImageColorGenerator

months = [
"janvier",
"février",
"mars",
"avril",
"mai",
"juin",
"juillet",
"août",
"septembre",
"octobre",
"novembre",
"décembre",
]

def nextDayList():
print("📝 Update l'ordre des channels")

# mettre la liste à la fin de la liste
with open("channel-order.json", "r") as f:
liste_arrays = json.load(f)

# Récupérer la première array et la supprimer de la liste
premiere_array = liste_arrays.pop(0)

# Ajouter la première array à la fin de la liste
liste_arrays.append(premiere_array)

# Ouvrir le fichier en mode écriture et écrire la liste mise à jour
with open("channel-order.json", "w") as f:
json.dump(liste_arrays, f)


# load env variables
print(" Load env variables")
print("🔄 Load env variables")
load_dotenv()

# Twitter API credentials
Expand All @@ -41,48 +47,47 @@
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print("✅ Authentification Twitter")


d = os.path.dirname(__file__) if "__file__" in locals() else os.getcwd()

transcript_path = os.path.join(d, "./../tchatTranscript/")
files = os.listdir(transcript_path)
print("🔓 Authentification Twitter")

now = datetime.now()
# Date du jour
date = datetime.now()
dateFormated = date.strftime("%d-%m-%Y_%H:%M:%S")

month_ago = now - timedelta(days=now.day)
# get the channel order
with open('channel-order.json', 'r') as f:
data = json.load(f)

month = month_ago.strftime("%m").lstrip('0')
year = month_ago.strftime("%Y")

print("✅ Generate Image for " + month + "_" + year)
# extraire la liste du jour
channels_of_the_day = data[0]

for file in files:
print("_________________________________________________________")
# check if array is empty
if not channels_of_the_day:
print("❌ No channel found for this date")
nextDayList()
exit()

# get the month and year in chat-#kaatsup-12-2022.txt
match = re.search(r"chat-#(.*)-(\d+)-(\d+)", file)

if match == None:
print("❌ File " + file + " is not a chat transcript file")
continue
d = os.path.dirname(__file__) if "__file__" in locals() else os.getcwd()

channel_name = match.group(1)
file_month = match.group(2)
file_year = match.group(3)
print("🎆 Generate Image for this channels:")
print(channels_of_the_day)

if (file_month != month or file_year != year):
continue
for channel_name in channels_of_the_day:
print("_________________________________________________________")

twitter_name = ""

print(" Generate Image for " + channel_name + " chat...")
print("🌌 Generate Image for " + channel_name + " chat...")

# load chat transcript text file
text = open(
os.path.join(d, "./../tchatTranscript/" + file), encoding="utf-8"
).read()
try:
text = open(
os.path.join(d, "./../tchat/" + channel_name + ".txt"), encoding="utf-8"
).read()
except:
print("❌ No chat transcript found for " + channel_name)
continue

logo_path = os.path.join(d, "./../logoChannel/")
logo_files = os.listdir(logo_path)
Expand All @@ -97,7 +102,7 @@

for file_name in os.listdir(logo_path):
if file_name.startswith(channel_name):
print(" Logo found: " + file_name)
print("🔎 Logo found: " + file_name)
file_logo = file_name
break

Expand All @@ -106,15 +111,15 @@
continue

logo_color = np.array(Image.open(os.path.join(d, logo_path + file_logo)))
print(" Logo color generated for " + channel_name)
print("🎨 Logo color generated for " + channel_name)

# subsample by factor of 3. Very lossy but for a wordcloud we don't really care.
logo_color = logo_color[::3, ::3]

# create mask white is "masked out"
logo_mask = logo_color.copy()
logo_mask[logo_mask.sum(axis=2) == 0] = 255
print(" Logo mask generated for " + channel_name)
print("🖍️ Logo mask generated for " + channel_name)

# some finesse: we enforce boundaries between colors so they get less washed out.
# For that we do some edge detection in the image
Expand All @@ -123,7 +128,7 @@
logo_color[:, :, i] / 255.0, 2) for i in range(3)],
axis=0,
)
print(" Logo edges generated for " + channel_name)
print("✍️ Logo edges generated for " + channel_name)

logo_mask[edges > 0.08] = 255

Expand All @@ -137,7 +142,7 @@
random_state=42,
relative_scaling=0,
)
print(" Wordcloud generated for " + channel_name)
print("🔠 Wordcloud generated for " + channel_name)

# generate word cloud
wc.generate(text)
Expand All @@ -150,36 +155,40 @@
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
wc.to_file("./../image/" + channel_name +
"-" + month + "_" + year + ".png")
print(" Image saved for " + channel_name)
"_" + dateFormated + ".png")
print("💾 Image saved for " + channel_name)

if channel_name == "":
continue

# Envoi d'un tweet
api.update_status_with_media(
"Voici le récapitulatif du mois de "
+ months[int(month) - 1]
+ " "
+ year
+ " sur le chat de "
"@"
"Voici le récapitulatif des 30 derniers jours sur le chat de @"
+ twitter_name +
" !\n#Twouns_ #Stats #" +
channel_name
+ " #Twitch",
channel_name+".png",
file=open("./../image/" + channel_name + "-" +
month + "_" + year + ".png", "rb"),
file=open("./../image/" + channel_name +
"_" + dateFormated + ".png", "rb"),
)

print("✅ Tweet sent for " + channel_name)
print("🐧 Tweet sent for " + channel_name)

# Deplacer le fichier dans un dossier archive
shutil.move("./../tchat/" + channel_name + ".txt",
"./../archive-tchat/" + channel_name + "_" + dateFormated + ".txt")


print("🚀 All images generated")
nextDayList()


# if os.path.exists("./../tchatTranscript/" + file):
# os.remove("./../tchatTranscript/" + file)
# if os.path.exists("./../tchat/" + file):
# os.remove("./../tchat/" + file)
# print("✅ File " + file + " deleted")
# else:
# print(f"{'./../tchatTranscript/'+file} n'existe pas")
# print(f"{'./../tchat/'+file} n'existe pas")

# plt.figure(figsize=(10, 10))
# plt.title("Original Image")
Expand Down
67 changes: 67 additions & 0 deletions pythonScript/channel-order.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[
[
"squeezie"
],
[],
[
"locklear"
],
[],
[
"juliettearz"
],
[],
[
"gotaga"
],
[],
[
"bastiui"
],
[],
[
"zerator"
],
[],
[
"skyrroztv"
],
[],
[
"kaatsup"
],
[],
[
"antoinedaniel"
],
[],
[
"kameto"
],
[],
[
"domingo"
],
[],
[
"ponce"
],
[],
[
"doigby"
],
[],
[
"jltomy"
],
[
"mastu"
],
[
"lestream"
],
[],
[
"littlebigwhale"
]
]
Empty file added tchat/.gitkeep
Empty file.
Empty file added user/.gitkeep
Empty file.

0 comments on commit f341ac5

Please sign in to comment.