chat.py 5.47 KB
Newer Older
1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Tocho Tochev's avatar
Tocho Tochev committed
3
import sys, argparse, time, subprocess, logging, os, redis
4
from bigbluebutton_api_python import BigBlueButton
5
from bigbluebutton_api_python import util as bbbUtil 
6
7
8
9
10
11
12
13
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

browser = None
14
selenium_timeout = 30
15
16
17
18
19
20
21
22
connect_timeout = 5

logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))

parser = argparse.ArgumentParser()
parser.add_argument("-s","--server", help="Big Blue Button Server URL")
parser.add_argument("-p","--secret", help="Big Blue Button Secret")
parser.add_argument("-i","--id", help="Big Blue Button Meeting ID")
23
24
25
26
parser.add_argument("-S","--startMeeting", help="start the meeting if not running",action="store_true")
parser.add_argument("-A","--attendeePassword", help="attendee password (required to create meetings)")
parser.add_argument("-M","--moderatorPassword", help="moderator password (required to create a meeting)")
parser.add_argument("-T","--meetingTitle", help="meeting title (required to create a meeting)")
27
28
29
parser.add_argument("-u","--user", help="Name to join the meeting",default="Live")
parser.add_argument("-r","--redis", help="Redis hostname",default="redis")
parser.add_argument("-c","--channel", help="Redis channel",default="chat")
Tocho Tochev's avatar
Tocho Tochev committed
30
31
32
33
parser.add_argument(
   '--browser-disable-dev-shm-usage', action='store_true', default=False,
   help='do not use /dev/shm',
)
34
35
36
args = parser.parse_args()

bbb = BigBlueButton(args.server,args.secret)
37
bbbUB = bbbUtil.UrlBuilder(args.server,args.secret)
38
39
40
41
42
43
44
45

def set_up():
    global browser

    options = Options()
    options.add_argument('--disable-infobars')
    options.add_argument('--no-sandbox')
    options.add_argument('--kiosk')
46
    options.add_argument('--window-size=1280,720')  # we do not need a big window for the chat
47
48
49
50
    options.add_argument('--window-position=0,0')
    options.add_experimental_option("excludeSwitches", ['enable-automation'])
    options.add_argument('--incognito')
    options.add_argument('--start-fullscreen')
Tocho Tochev's avatar
Tocho Tochev committed
51
52
53
54
    if args.browser_disable_dev_shm_usage:
        options.add_argument('--disable-dev-shm-usage')
    else:
        dev_shm_size = int(subprocess.run('df /dev/shm/ --block-size=1M --output=size | tail -n 1', shell=True, stdout=subprocess.PIPE).stdout or '0')
Tocho Tochev's avatar
Tocho Tochev committed
55
56
57
58
59
60
61
62
63
        required_dev_shm_size = 500  # in MB, 1024MB is recommended
        if dev_shm_size < required_dev_shm_size:
            logging.error(
                'The size of /dev/shm/ is %sMB (minimum recommended is %sMB), '
                'consider increasing the size of /dev/shm/ (shm-size docker parameter) or disabling /dev/shm usage '
                '(see --browser-disable-dev-shm-usage or BROWSER_DISABLE_DEV_SHM_USAGE env variable).',
                dev_shm_size, required_dev_shm_size
            )
            sys.exit(2)
64
65
66
67
68
69
70
71
72

    logging.info('Starting browser to chat!!')

    browser = webdriver.Chrome(executable_path='./chromedriver',options=options)

def bbb_browser():
    global browser

    logging.info('Open BBB for chat!!')
73
74
75
76
77
78
79
    if args.startMeeting is True:
        try:
            logging.info("create_meeting...")
            create_meeting()
        except exception.bbbexception.BBBException as ERR:
            logging.info(ERR)

80
81
82
    join_url = get_join_url()
    logging.info(join_url)
    browser.get(join_url)
83

84
    time.sleep(6)
85

86
87
    # wait for message input:
    element = EC.presence_of_element_located((By.ID, 'message-input'))
88
    WebDriverWait(browser, selenium_timeout).until(element)
89

90
    browser.find_element_by_id('message-input').send_keys("Viewers of the live stream can now send messages to this meeting")
91
92
    browser.find_elements_by_css_selector('[aria-label="Send message"]')[0].click()

93
94
95
96
97
98
99
100
101
102
103
    redis_r = redis.Redis(host=args.redis,charset="utf-8", decode_responses=True)
    redis_s = redis_r.pubsub()
    redis_s.psubscribe(**{args.channel:chat_handler})
    thread = redis_s.run_in_thread(sleep_time=0.001)

def chat_handler(message):
    global browser
    browser.find_element_by_id('message-input').send_keys(message['data'])
    browser.find_elements_by_css_selector('[aria-label="Send message"]')[0].click()
    logging.info(message['data'])

104
105
106
107
108
109
110
111
112
113
def create_meeting():
    create_params = {}
    if args.moderatorPassword:
        create_params['moderatorPW'] = args.moderatorPassword
    if args.attendeePassword:
        create_params['attendeePW'] = args.attendeePassword
    if args.meetingTitle:
        create_params['name'] = args.meetingTitle
    return bbb.create_meeting(args.id, params=create_params)

114
115
def get_join_url():
    minfo = bbb.get_meeting_info(args.id)
116
117
118
119
120
    pwd = minfo.get_meetinginfo().get_attendeepw()
    joinParams = {}
    joinParams['meetingID'] = args.id
    joinParams['fullName'] = args.user
    joinParams['password'] = pwd
121
    joinParams['userdata-bbb_auto_join_audio'] = "false"
122
123
    joinParams['userdata-bbb_enable_video'] = 'false' 
    joinParams['userdata-bbb_listen_only_mode'] = "true" 
124
    joinParams['userdata-bbb_force_listen_only'] = "true" 
125
    joinParams['userdata-bbb_skip_check_audio'] = 'true' 
126
    joinParams['joinViaHtml5'] = 'true'
Petr Pražák's avatar
typo    
Petr Pražák committed
127
    return bbbUB.buildUrl("join", params=joinParams) 
128

129
130
131
132
133
134
135
136
137
138
139

def chat():
    while True:
        time.sleep(60)


while bbb.is_meeting_running(args.id).is_meeting_running() != True:
    time.sleep(connect_timeout)
set_up()
bbb_browser()
chat()
140
browser.quit()