Bravo List

Bravo List (http://www.bvlist.com/index.php)
-   Community Cafe (http://www.bvlist.com/forumdisplay.php?f=18)
-   -   Youtube live chat listening bot written in python (firefly) (http://www.bvlist.com/showthread.php?t=12446)

firefly007 11th November 2021 16:14

Youtube live chat listening bot written in python (firefly)
 
Hi so here is a quick script which will listen to live Youtube chat stream.

You can do a number of things with this script. At the moment the script will have all the username and their message to a database.

Remember if you wana run this script in headless mode you need to comment out driver = webdriver.Firefox() so it will look like this #driver = webdriver.Firefox() and remove the hash from #driver = webdriver.Firefox(options=options) so it will look like this driver = webdriver.Firefox(options=options).

This script runs on Python3 both on Ubuntu 18.04 64bit LTS and Windows 10 64bit

Code:

# Module Imports
import mariadb
import sys
import os
from datetime import datetime
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from time import sleep
from itertools import count


# Connect to MariaDB Platform
try:
    conn = mariadb.connect(
        user="root",
        password="",
        host="localhost",
        port=3306,
        database="ytchat"

    )
except mariadb.Error as e:
    print(f"Error connecting to MariaDB Platform: {e}")
    sys.exit(1)

# Get Cursor
cur = conn.cursor()

def ytc(user,mess):
    cur.execute(
        "INSERT INTO mess (user,mess,added) VALUES (?, ?, ?)",
        (user, mess, datetime.now()))
    conn.commit()


string = 'KZWVvBbjOuU'
options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox()
#driver = webdriver.Firefox(options=options)
driver.get('https://www.youtube.com/live_chat?is_popout=1&v='+ str(string))
driver.maximize_window()
sleep(4)

messages = driver.find_elements_by_xpath('//*[@id="message"]')

last_message_text = messages[0].text



while (True):


    messages = driver.find_elements_by_xpath('//*[@id="message"]')
    driver.get('https://www.youtube.com/live_chat?is_popout=1&v='+ str(string))
    try:
        number =1
        for number in count(1):
            user = driver.find_elements_by_xpath('//*[@id="author-name"]')[number].text
            mess = driver.find_elements_by_xpath('//*[@id="message"]')[number].text
            print(user)
            print(mess)
            ytc(user,mess)
            sleep(5)
    except NoSuchElementException:
        sleep(10)
        number =1
        for number in count(1):
            user = driver.find_elements_by_xpath('//*[@id="author-name"]')[number].text
            mess = driver.find_elements_by_xpath('//*[@id="message"]')[number].text
            print(user)
            print(mess)
            ytc(user,mess)
            sleep(5)



All times are GMT +2. The time now is 13:59.

Powered by vBulletin® Version 3.8.11 Beta 3
Copyright ©2000 - 2024, vBulletin Solutions Inc.