Spaces:
Build error
Build error
Danish Jameel
commited on
Commit
·
d94a479
1
Parent(s):
b49ae18
first commit
Browse files- app.py +71 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import csv
|
3 |
+
import requests
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
import pandas as pd
|
6 |
+
import pyperclip
|
7 |
+
|
8 |
+
def scrape_trends(url):
|
9 |
+
"""
|
10 |
+
This function scrapes the Twitter Trends webpage and returns a sorted
|
11 |
+
dataframe of the top hashtags along with their tweet count.
|
12 |
+
"""
|
13 |
+
response = requests.get(url)
|
14 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
15 |
+
|
16 |
+
data = [] # list to store scraped data
|
17 |
+
|
18 |
+
for li in soup.find_all('li'):
|
19 |
+
a_tag = li.find('a')
|
20 |
+
title = a_tag.text
|
21 |
+
tweet_count_tag = li.find('span', class_='tweet-count')
|
22 |
+
if tweet_count_tag:
|
23 |
+
tweet_count = tweet_count_tag.text
|
24 |
+
else:
|
25 |
+
tweet_count = ''
|
26 |
+
if title.startswith('#'):
|
27 |
+
data.append({'Title': title, 'Tweet Count': tweet_count})
|
28 |
+
|
29 |
+
# write data to CSV file
|
30 |
+
with open('trends.csv', mode='w', encoding='utf-8', newline='') as file:
|
31 |
+
writer = csv.DictWriter(file, fieldnames=['Title', 'Tweet Count'])
|
32 |
+
writer.writeheader()
|
33 |
+
writer.writerows(data)
|
34 |
+
|
35 |
+
df = pd.read_csv("trends.csv")
|
36 |
+
df_sorted = df.sort_values('Tweet Count', ascending=False)
|
37 |
+
df_sorted['Tweet Count'] = df_sorted['Tweet Count'].fillna(1)
|
38 |
+
df_sorted = df_sorted.reset_index(drop=True)
|
39 |
+
|
40 |
+
return df_sorted
|
41 |
+
|
42 |
+
def main():
|
43 |
+
st.title("Twitter Trends")
|
44 |
+
|
45 |
+
url = st.text_input("Enter the URL of Twitter Trends page:")
|
46 |
+
|
47 |
+
if not url:
|
48 |
+
st.warning("Please enter a valid URL!")
|
49 |
+
return
|
50 |
+
|
51 |
+
num_title = st.slider("How many titles to include in the output?", 1, 40, 5)
|
52 |
+
|
53 |
+
df_sorted = scrape_trends(url)
|
54 |
+
|
55 |
+
titles = df_sorted.iloc[:num_title, 0].tolist()
|
56 |
+
|
57 |
+
user_text = st.text_input("Enter some text:")
|
58 |
+
|
59 |
+
if not user_text:
|
60 |
+
st.warning("Please enter some text!")
|
61 |
+
return
|
62 |
+
|
63 |
+
for i in range(num_title):
|
64 |
+
output = user_text + ' ' + ' '.join(titles[:i+1])
|
65 |
+
st.write(output)
|
66 |
+
if st.button("Copy Output {}".format(i+1)):
|
67 |
+
pyperclip.copy(output)
|
68 |
+
st.success("Output {} copied to clipboard!".format(i+1))
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
csv
|
3 |
+
requests
|
4 |
+
BeautifulSoup
|
5 |
+
pandas
|
6 |
+
pyperclip
|