File size: 2,093 Bytes
d94a479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import csv
import requests
from bs4 import BeautifulSoup
import pandas as pd
import pyperclip

def scrape_trends(url):
    """
    This function scrapes the Twitter Trends webpage and returns a sorted
    dataframe of the top hashtags along with their tweet count.
    """
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')

    data = [] # list to store scraped data

    for li in soup.find_all('li'):
        a_tag = li.find('a')
        title = a_tag.text
        tweet_count_tag = li.find('span', class_='tweet-count')
        if tweet_count_tag:
            tweet_count = tweet_count_tag.text
        else:
            tweet_count = ''
        if title.startswith('#'):
            data.append({'Title': title, 'Tweet Count': tweet_count})

    # write data to CSV file
    with open('trends.csv', mode='w', encoding='utf-8', newline='') as file:
        writer = csv.DictWriter(file, fieldnames=['Title', 'Tweet Count'])
        writer.writeheader()
        writer.writerows(data)

    df = pd.read_csv("trends.csv")
    df_sorted = df.sort_values('Tweet Count', ascending=False)
    df_sorted['Tweet Count'] = df_sorted['Tweet Count'].fillna(1)
    df_sorted = df_sorted.reset_index(drop=True)

    return df_sorted

def main():
    st.title("Twitter Trends")

    url = st.text_input("Enter the URL of Twitter Trends page:")

    if not url:
        st.warning("Please enter a valid URL!")
        return

    num_title = st.slider("How many titles to include in the output?", 1, 40, 5)

    df_sorted = scrape_trends(url)

    titles = df_sorted.iloc[:num_title, 0].tolist()

    user_text = st.text_input("Enter some text:")

    if not user_text:
        st.warning("Please enter some text!")
        return

    for i in range(num_title):
        output = user_text + ' ' + ' '.join(titles[:i+1])
        st.write(output)
        if st.button("Copy Output {}".format(i+1)):
            pyperclip.copy(output)
            st.success("Output {} copied to clipboard!".format(i+1))

if __name__ == "__main__":
    main()