File size: 4,464 Bytes
883f19c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gradio as gr
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Chrome, ChromeOptions
import time
import pandas as pd
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager








global name
global password
global topic

def login_and_work(username_text, password_text, topic_text):
    options = ChromeOptions()
    options.use_chromium = True
    driver = webdriver.Chrome()
    #driver= webdriver.Chrome(ChromeDriverManager().install())
    driver.get('https://twitter.com/i/flow/login')
    time.sleep(5)
    username = driver.find_element("xpath",'//input[@name="text"]')
    username.send_keys(username_text)
    driver.find_element("xpath",'(//*[@role="button"])[3]').click() # in prevoius block
    
    try:
        time.sleep(10)
        y = driver.find_element("xpath",'//h1//span')
        if(y.text =='Enter your phone number or username'):
            username2 = driver.find_element("xpath",'//input[@name="text"]')
            username2.send_keys(username_text)
            driver.find_element("xpath",'(//*[@role="button"])[2]').click()
    except:
        
        x = 10

    password =  driver.find_element("xpath",'//input[@name="password"]')
    password.send_keys(password_text)
    driver.find_element("xpath",'(//*[@role = "button"])[4]').click()


    try:
        driver.maximize_window()
        time.sleep(5)
        driver.maximize_window()
        search = driver.find_element("xpath",'//input[@placeholder="Search"]')
        search.send_keys(Keys.CONTROL + "a")
        search.send_keys(Keys.DELETE)
        search.send_keys(topic_text)
        search.send_keys(Keys.RETURN)

        df = pd.DataFrame()
        df['Name']=''
        df['Tweet'] = ''
        #df['Sentiment'] = ''
        tweet_count = 6
        i = 1
        element = driver.find_element("xpath","//body")
        time.sleep(5)
        while True and i <= tweet_count:
            
            try:
                tweet_name =  driver.find_element("xpath",f'(//*[@data-testid="User-Name"])[{i}]')
                df.loc[i, 'Name'] = tweet_name.text
                    
                tweet_div = driver.find_element("xpath",f'(//*[@data-testid="tweetText"])[{i}]')
                if(tweet_div):
                    #print(driver.find_element("xpath",'(//*[@data-testid="tweetText"])[1]').getText())
                    df.loc[i, 'Tweet'] = tweet_div.text
                    i += 1
                element.send_keys(Keys.PAGE_DOWN)
                time.sleep(5)
            except: 
                time.sleep(5)
                tweet_name =  driver.find_element("xpath",f'(//*[@data-testid="User-Name"])[{i}]')
                df.loc[i, 'Name'] = tweet_name.text
                    
                tweet_div = driver.find_element("xpath",f'(//*[@data-testid="tweetText"])[{i}]')
                if(tweet_div):
                    #print(driver.find_element("xpath",'(//*[@data-testid="tweetText"])[1]').getText())
                    df.loc[i, 'Tweet'] = tweet_div.text
                    i += 1
                element.send_keys(Keys.PAGE_DOWN)
                time.sleep(5)


        #model = tweetnlp.load_model('sentiment', multilingual=True)  # Or `model = tweetnlp.Sentiment()` 

        # for i in range(1,len(df)+1):
        #     print("text is...", df.loc[i,'Tweet'])
            
            
        #     #y = sentiment_pipeline(df.loc[i, 'Tweet'])
        #     #print('label is..', y[0]['label'])
        #     print('model sentiment is..', model.sentiment(df.loc[i, 'Tweet']))
        #     df.loc[i, 'Sentiment'] = str(model.sentiment(df.loc[i, 'Tweet']))
        
        return df
        
    except Exception as e:
        x = 10

with gr.Blocks() as demo:

    name = gr.Textbox(label="Username")
    password = gr.Textbox(label="Password", type = 'password')
    topic= gr.Textbox(label="Topic")
    
    btn = gr.Button(value="Submit")
    btn.click(login_and_work, inputs=[name, password, topic], outputs= gr.Dataframe(headers=['Name', 'Tweet']))

demo.launch(share=True)