File size: 5,726 Bytes
dfbc8dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import streamlit as st
import random
import toml
import streamlit as st
import pandas as pd
from datetime import date
import re
import csv
from getpass import getpass
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

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
import tweetnlp



df = pd.DataFrame()
options = ''

@st.experimental_singleton
def get_driver():
    global options
    return webdriver.Chrome(
        service=Service(
            ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()
        ),
        options=options
    )





def login_twitter(username_text, password_text, topic_text):
    # global options
    # options = Options()
    # options.add_argument("--disable-gpu")
    # options.add_argument("--headless")
    # driver = get_driver()
    # options.use_chromium = True
    options = ChromeOptions()
    options.use_chromium = True
    driver = webdriver.Chrome()

    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:
        st.write(" I am in exception and didnt get 'Enter your phone number or username'")
        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(10)
        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(20)
        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(10)
            except: 
                time.sleep(10)
                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(10)


        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']))
        st.dataframe(df)

        
    except Exception as e:
        st.write(e)
        # driver.maximize_window()
        # time.sleep(10)
        # 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)
        

   

with st.sidebar:
    username = st.text_input("Username")
    password_text = st.text_input("Password", type = "password")
    topic = st.text_input("topic")
    connect = st.button("Login Twitter",\
                       on_click = login_twitter,
                       args = [username, password_text, topic]
                       )
    if('is_ready'  not in st.session_state):
        st.session_state['is_ready'] = False

    if(st.session_state['is_ready'] == True):
        st.write('Connected!')