Skip to content

Instantly share code, notes, and snippets.

@montarion
Created September 2, 2020 23:25
Show Gist options
  • Save montarion/1a4bf8793a78694fabe53735c256f56f to your computer and use it in GitHub Desktop.
Save montarion/1a4bf8793a78694fabe53735c256f56f to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
class w2g:
def __init__(self, url = "https://www.w2g.tv"):
self.url = url
options = webdriver.ChromeOptions()
options.add_argument("--headless")
self.driver = webdriver.Chrome(chrome_options=options)
self.driver.get(self.url)
self.closed = False
def createroom(self, urllist):
print("in func")
created = False
cookiepass = False
while not (cookiepass and created):
if not cookiepass:
try:
print("trying cookies")
cookiebutton = self.driver.find_element_by_xpath('//*[@id="qc-cmp2-ui"]/div[2]/div/button[2]')
#cookiebutton.click()
self.driver.execute_script("arguments[0].click()", cookiebutton)
cookiepass = True
print("done with cookies")
sleep(0.5)
except:
print("failure!")
if not created:
try:
print("trying to create")
button = self.driver.find_element_by_xpath('//*[@id="create_room_button"]')
#button.click()
self.driver.execute_script("arguments[0].click()", button)
sleep(0.5)
created = True
print("done with creation")
except Exception as e:
print("failure!")
created = False
print(e)
print(f"Status:\nCookiepass: {cookiepass}\nCreated: {created}\nGO? {cookiepass and created}")
sleep(3)
reslist = []
for searchlink in urllist:
res = self.addvideo(searchlink)
reslist.append(res)
sleep(1)
# get room link
room = self.driver.current_url
self.room = room
# close browser
self.driver.close()
self.closed = True
return room, reslist
def addvideo(self, link):
try:
# click search
print("clicking search")
#searchtoggle = self.driver.find_element_by_css_selector(".search.icon.w2g-from-mobile.w2g-search-hide")
cssselector = ".search.icon.w2g-from-mobile.w2g-search-hide"
searchtoggle = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, cssselector)))
print(searchtoggle)
self.driver.execute_script("arguments[0].click();", searchtoggle)
#searchtoggle.click()
sleep(1)
# add link
print("adding link")
searchfield = self.driver.find_element_by_xpath('//*[@id="search-bar-input"]')
searchfield.send_keys(link)
# click search
print("entering")
searchbutton = self.driver.find_element_by_xpath('//*[@id="search-bar-form"]/div/button/i')
searchbutton.click()
sleep(2)
# click add
print("clicking add button")
addbutton = self.driver.find_element_by_xpath('//*[@id="w2g-search-results"]/div[3]/div/div[2]/div[3]/div[2]')
addbutton.click()
sleep(1)
# clear text field
searchfield.clear()
sleep(1)
# click close
print("clicking close button")
searchtoggle = self.driver.find_element_by_css_selector(".close.icon.w2g-from-mobile.w2g-search-show")
searchtoggle.click()
return "Done!"
except Exception as e:
print(e)
return f"{str(e)}"
def addtoexisting(self, urllist):
created = False
cookiepass = False
while not (cookiepass and created):
print(f"Status:\nCookiepass: {cookiepass}\nCreated: {created}\nGO? {cookiepass and created}")
if not cookiepass:
try:
print("trying cookies")
cookiebutton = self.driver.find_element_by_xpath('//*[@id="qc-cmp2-ui"]/div[2]/div/button[2]')
cookiebutton.click()
cookiepass = True
print("done with cookies")
sleep(2)
except Exception as e:
print("failure!")
created = False
print(e)
sleep(5)
if not created:
try:
# click join
print("clicking join")
joinbutton = self.driver.find_element_by_xpath("//*[@id='intro-modal']/div[2]/div")
joinbutton.click()
sleep(1)
created = True
print("done with creation")
except Exception as e:
print("failure!")
created = False
print(e)
sleep(5)
reslist = []
for searchlink in urllist:
res = self.addvideo(searchlink)
reslist.append(res)
sleep(1)
# close browser
#self.driver.close()
#self.closed = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment