0% found this document useful (0 votes)
32 views7 pages

Import Requests

Uploaded by

Hans Landry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

Import Requests

Uploaded by

Hans Landry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import requests

from bs4 import BeautifulSoup

import time

import os

TOKEN = "7277864177:AAEPr3lpIOI6buz9c59Yp-KNwEiJPyFm2Sw"

CHAT_ID = "https://t.me/serie_d_animation_americaine"

def get_new_animation_series():

url = "URL_du_site_web" # Remplacez par l'URL du site web où vous pouvez trouver les nouvelles
sorties de séries d'animation américaines

response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')

new_series = soup.find_all('div', class_='new-series')

return new_series

def send_message(token, chat_id, message, photo=None):

url = f"https://api.telegram.org/bot{token}/sendMessage"

params = {"chat_id": chat_id, "text": message}

if photo:

files = {'photo': open(photo, 'rb')}

response = requests.post(url, params=params, files=files)

else:

response = requests.get(url, params=params)

print(response.json())

def download_image_and_send_message(token, chat_id, message):

search_query = "série d'animation américaine"

url = f"https://api.telegram.org/bot{token}/sendPhoto"

params = {"chat_id": chat_id, "caption": message}

response = requests.get(url, params=params)


if response.status_code == 200:

file_id = response.json()['result']['photo'][-1]['file_id']

file_path = requests.get(f"https://api.telegram.org/bot{token}/getFile?file_id={file_id}").json()
['result']['file_path']

image_url = f"https://api.telegram.org/file/bot{token}/{file_path}"

image_response = requests.get(image_url)

with open('new_animation_series.jpg', 'wb') as f:

f.write(image_response.content)

send_message(token, chat_id, message, photo='new_animation_series.jpg')

os.remove('new_animation_series.jpg')

while True:

new_series = get_new_animation_series()

if new_series:

for series in new_series:

title = series.find('h2').text

description = series.find('p').text

send_message(TOKEN, CHAT_ID, f"Nouvelle série : {title}\nDescription : {description}")

else:

download_image_and_send_message(TOKEN, CHAT_ID, "Aucune nouvelle série d'animation


américaine trouvée.")

time.sleep(86400) #

Vérifiez les nouvelles sorties toutes les heures (1j)


import requests

from bs4 import BeautifulSoup

import time

import os

TOKEN = "7277864177:AAEPr3lpIOI6buz9c59Yp-KNwEiJPyFm2Sw "

CHAT_ID = " https://t.me/serie_d_animation_americaine "

def get_new_animation_series():

url = " https://t.me/serie_d_animation_americaine " # Remplacez par l'URL du site web où vous
pouvez trouver les nouvelles sorties de séries d'animation américaines

response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')

new_series = soup.find_all('div', class_='new-series')

return new_series

def send_message(token, chat_id, message, photo=None):

url = f"https://api.telegram.org/bot{token}/sendMessage"

params = {"chat_id": chat_id, "text": message}

if photo:

files = {'photo': open(photo, 'rb')}

response = requests.post(url, params=params, files=files)

else:

response = requests.get(url, params=params)

print(response.json())

def download_image_and_send_message(token, chat_id, message):

search_query = "série d'animation américaine"

url = f"https://api.telegram.org/bot{token}/sendPhoto"

params = {"chat_id": chat_id, "caption": message}

response = requests.get(url, params=params)


if response.status_code == 200:

file_id = response.json()['result']['photo'][-1]['file_id']

file_path = requests.get(f"https://api.telegram.org/bot{token}/getFile?file_id={file_id}").json()
['result']['file_path']

image_url = f"https://api.telegram.org/file/bot{token}/{file_path}"

image_response = requests.get(image_url)

with open('new_animation_series.jpg', 'wb') as f:

f.write(image_response.content)

send_message(token, chat_id, message, photo='new_animation_series.jpg')

os.remove('new_animation_series.jpg')

while True:

new_series = get_new_animation_series()

if new_series:

for series in new_series:

title = series.find('h2').text

description = series.find('p').text

send_message(TOKEN, CHAT_ID, f"Nouvelle série : {title}\nDescription : {description}")

else:

download_image_and_send_message(TOKEN, CHAT_ID, "Aucune nouvelle série d'animation


américaine trouvée.")

time.sleep(86400) # Vérifiez les nouvelles sorties toutes les heures (1j)

import os

import random
import telegram

from datetime import datetime, timedelta

import time

import sys

import warnings

# Réinitialiser le gestionnaire d'exceptions

sys.excepthook = sys.__excepthook__

# Réinitialiser le gestionnaire d'avertissements

warnings.resetwarnings()

warnings.simplefilter('default', RuntimeWarning)

# Informations du bot Telegram

BOT_TOKEN = '7277864177:AAEPr3lpIOI6buz9c59Yp-KNwEiJPyFm2Sw'

CHAT_ID = 'https://t.me/serie_d_animation_americaine'

# Dossiers avec les images

DOSSIERS_IMAGES = ['south_park', 'simpsons', 'family_guy']

# Vérifier et créer les dossiers s'ils n'existent pas

for dossier in DOSSIERS_IMAGES:

dossier_path = os.path.join(os.path.dirname(__file__), dossier)

if not os.path.exists(dossier_path):

os.makedirs(dossier_path)

print(f"Dossier '{dossier}' créé.")

# Initialiser le bot Telegram

bot = telegram.Bot(token=BOT_TOKEN)
# Fonction pour obtenir une liste unique de photos à envoyer

def obtenir_photos_aleatoires():

photos = []

for dossier in DOSSIERS_IMAGES:

dossier_path = os.path.join(os.path.dirname(__file__), dossier)

fichiers = os.listdir(dossier_path)

photo = random.choice(fichiers)

while photo in photos:

photo = random.choice(fichiers)

photos.append(photo)

return photos

# Envoyer les photos chaque jour à 8h00

def envoyer_photos_quotidiennes():

photos = obtenir_photos_aleatoires()

for photo in photos:

with open(os.path.join(os.path.dirname(__file__), DOSSIERS_IMAGES[0], photo), 'rb') as f:

bot.send_photo(chat_id=CHAT_ID, photo=f)

print(f"Photos envoyées le {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")

# Vérifier l'heure toutes les minutes et envoyer les photos à 8h00

while True:

now = datetime.now()

if now.hour == 8 and now.minute == 9:

envoyer_photos_quotidiennes()

else:

time.sleep(60)

You might also like