Create ts.py
Browse files
ts.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import selenium
|
| 3 |
+
from selenium import webdriver
|
| 4 |
+
from selenium.webdriver.common.keys import Keys
|
| 5 |
+
from selenium.common.exceptions import NoSuchElementException
|
| 6 |
+
from selenium.webdriver import Chrome, ChromeOptions
|
| 7 |
+
import time
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from selenium.webdriver.support import expected_conditions as EC
|
| 10 |
+
from selenium.webdriver.common.by import By
|
| 11 |
+
from selenium.webdriver.support.ui import WebDriverWait
|
| 12 |
+
from time import sleep
|
| 13 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
global name
|
| 23 |
+
global password
|
| 24 |
+
global topic
|
| 25 |
+
|
| 26 |
+
def login_and_work(username_text, password_text, topic_text):
|
| 27 |
+
options = ChromeOptions()
|
| 28 |
+
options.use_chromium = True
|
| 29 |
+
driver = webdriver.Chrome()
|
| 30 |
+
#driver= webdriver.Chrome(ChromeDriverManager().install())
|
| 31 |
+
driver.get('https://twitter.com/i/flow/login')
|
| 32 |
+
time.sleep(5)
|
| 33 |
+
username = driver.find_element("xpath",'//input[@name="text"]')
|
| 34 |
+
username.send_keys(username_text)
|
| 35 |
+
driver.find_element("xpath",'(//*[@role="button"])[3]').click() # in prevoius block
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
time.sleep(10)
|
| 39 |
+
y = driver.find_element("xpath",'//h1//span')
|
| 40 |
+
if(y.text =='Enter your phone number or username'):
|
| 41 |
+
username2 = driver.find_element("xpath",'//input[@name="text"]')
|
| 42 |
+
username2.send_keys(username_text)
|
| 43 |
+
driver.find_element("xpath",'(//*[@role="button"])[2]').click()
|
| 44 |
+
except:
|
| 45 |
+
|
| 46 |
+
x = 10
|
| 47 |
+
|
| 48 |
+
password = driver.find_element("xpath",'//input[@name="password"]')
|
| 49 |
+
password.send_keys(password_text)
|
| 50 |
+
driver.find_element("xpath",'(//*[@role = "button"])[4]').click()
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
driver.maximize_window()
|
| 55 |
+
time.sleep(5)
|
| 56 |
+
driver.maximize_window()
|
| 57 |
+
search = driver.find_element("xpath",'//input[@placeholder="Search"]')
|
| 58 |
+
search.send_keys(Keys.CONTROL + "a")
|
| 59 |
+
search.send_keys(Keys.DELETE)
|
| 60 |
+
search.send_keys(topic_text)
|
| 61 |
+
search.send_keys(Keys.RETURN)
|
| 62 |
+
|
| 63 |
+
df = pd.DataFrame()
|
| 64 |
+
df['Name']=''
|
| 65 |
+
df['Tweet'] = ''
|
| 66 |
+
#df['Sentiment'] = ''
|
| 67 |
+
tweet_count = 6
|
| 68 |
+
i = 1
|
| 69 |
+
element = driver.find_element("xpath","//body")
|
| 70 |
+
time.sleep(5)
|
| 71 |
+
while True and i <= tweet_count:
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
tweet_name = driver.find_element("xpath",f'(//*[@data-testid="User-Name"])[{i}]')
|
| 75 |
+
df.loc[i, 'Name'] = tweet_name.text
|
| 76 |
+
|
| 77 |
+
tweet_div = driver.find_element("xpath",f'(//*[@data-testid="tweetText"])[{i}]')
|
| 78 |
+
if(tweet_div):
|
| 79 |
+
#print(driver.find_element("xpath",'(//*[@data-testid="tweetText"])[1]').getText())
|
| 80 |
+
df.loc[i, 'Tweet'] = tweet_div.text
|
| 81 |
+
i += 1
|
| 82 |
+
element.send_keys(Keys.PAGE_DOWN)
|
| 83 |
+
time.sleep(5)
|
| 84 |
+
except:
|
| 85 |
+
time.sleep(5)
|
| 86 |
+
tweet_name = driver.find_element("xpath",f'(//*[@data-testid="User-Name"])[{i}]')
|
| 87 |
+
df.loc[i, 'Name'] = tweet_name.text
|
| 88 |
+
|
| 89 |
+
tweet_div = driver.find_element("xpath",f'(//*[@data-testid="tweetText"])[{i}]')
|
| 90 |
+
if(tweet_div):
|
| 91 |
+
#print(driver.find_element("xpath",'(//*[@data-testid="tweetText"])[1]').getText())
|
| 92 |
+
df.loc[i, 'Tweet'] = tweet_div.text
|
| 93 |
+
i += 1
|
| 94 |
+
element.send_keys(Keys.PAGE_DOWN)
|
| 95 |
+
time.sleep(5)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
#model = tweetnlp.load_model('sentiment', multilingual=True) # Or `model = tweetnlp.Sentiment()`
|
| 99 |
+
|
| 100 |
+
# for i in range(1,len(df)+1):
|
| 101 |
+
# print("text is...", df.loc[i,'Tweet'])
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# #y = sentiment_pipeline(df.loc[i, 'Tweet'])
|
| 105 |
+
# #print('label is..', y[0]['label'])
|
| 106 |
+
# print('model sentiment is..', model.sentiment(df.loc[i, 'Tweet']))
|
| 107 |
+
# df.loc[i, 'Sentiment'] = str(model.sentiment(df.loc[i, 'Tweet']))
|
| 108 |
+
|
| 109 |
+
return df
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
x = 10
|
| 113 |
+
|
| 114 |
+
with gr.Blocks() as demo:
|
| 115 |
+
|
| 116 |
+
name = gr.Textbox(label="Username")
|
| 117 |
+
password = gr.Textbox(label="Password", type = 'password')
|
| 118 |
+
topic= gr.Textbox(label="Topic")
|
| 119 |
+
|
| 120 |
+
btn = gr.Button(value="Submit")
|
| 121 |
+
btn.click(login_and_work, inputs=[name, password, topic], outputs= gr.Dataframe(headers=['Name', 'Tweet']))
|
| 122 |
+
|
| 123 |
+
demo.launch(share=True)
|