forked from Yash-Kavaiya/telegram-url-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
55 lines (46 loc) · 1.89 KB
/
app.py
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
from flask import Flask, render_template, request, send_file
import pandas as pd
import json
import re
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
data = json.load(file)
df = pd.DataFrame(data['messages'])
df = df[df['text'].str.len() > 6]
df.to_csv('result.csv', columns=['text'], index=False)
urls = []
for text in df['text']:
if isinstance(text, list):
# If 'text' is a list, iterate over its elements
for item in text:
if isinstance(item, dict):
# If 'item' is a dictionary, extract values and apply regex
for value in item.values():
urls.extend(re.findall(r'https?://\S+', str(value)))
else:
# If 'item' is not a dictionary, apply the regular expression directly
urls.extend(re.findall(r'https?://\S+', str(item)))
elif isinstance(text, dict):
# If 'text' is a dictionary, extract values and apply regex
for value in text.values():
urls.extend(re.findall(r'https?://\S+', str(value)))
else:
# If 'text' is not a list or dictionary, apply the regular expression directly
urls.extend(re.findall(r'https?://\S+', str(text)))
url_df = pd.DataFrame(urls, columns=['urls'])
url_df['urls'] = url_df['urls'].str.replace(r'[}{",]', '', regex=True)
url_list = url_df['urls'].tolist()
return render_template('download.html', urls=url_list)
@app.route('/download_result')
def download_result():
return send_file('result.csv', as_attachment=True)
@app.route('/download_urls')
def download_urls():
return send_file('urls.csv', as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)