forked from DevMehta13/NGO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
111 lines (89 loc) · 2.89 KB
/
helpers.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
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
import os
from flask import redirect, render_template, request, session, send_file, url_for
from functools import wraps
import pandas as pd
import re
import asyncio
import aiohttp
from pyembed.core import PyEmbed
from pyembed.core.consumer import PyEmbedConsumerError
pyembed_instance = PyEmbed()
ADMIN_USERNAME = os.getenv('ADMIN_USERNAME', 'admin')
def apology(message, code=400):
"""Render message as an apology to user."""
def escape(s):
"""
Escape special characters.
https://github.com/jacebrowning/memegen#special-characters
"""
for old, new in [
("-", "--"),
(" ", "-"),
("_", "__"),
("?", "~q"),
("%", "~p"),
("#", "~h"),
("/", "~s"),
('"', "''"),
]:
s = s.replace(old, new)
return s
return render_template("apology.html", error_code=code, error_message=escape(message)), code
def admin_login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("admin") != ADMIN_USERNAME:
return redirect(url_for('admin_login'))
return f(*args, **kwargs)
return decorated_function
def upload_to_excel(path: str, data, sheet_name):
# Convert the list of dictionaries to a DataFrame
df = pd.DataFrame(data)
# Write the combined DataFrame back to the Excel file
df.to_excel(path, sheet_name=sheet_name, index=False)
print(f"Data saved to {path}")
# Function to extract URLs from the text
def extract_urls(text):
import re
url_pattern = re.compile(r'https?://\S+')
return url_pattern.findall(text)
async def fetch_embed(session, url):
try:
# Using PyEmbed to fetch embed HTML asynchronously
embed_html = pyembed_instance.embed(url)
if embed_html:
return {
'url': url,
'html': embed_html,
'is_embed': True
}
except PyEmbedConsumerError as e:
print(f"Error embedding {url}: {e}")
except Exception as e:
print(f"Error embedding {url}: {e}")
return {
'url': url,
'html': f'<a href="{url}" target="_blank">{url}</a>',
'is_embed': False
}
async def generate_previews(urls):
async with aiohttp.ClientSession() as session:
tasks = [fetch_embed(session, url) for url in urls]
return await asyncio.gather(*tasks)
def embed_link(link_text):
urls = extract_urls(link_text)
if urls:
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
previews = loop.run_until_complete(generate_previews(urls))
preview = previews[0] if previews else None
else:
preview = None
post = {
'content': link_text,
'preview': preview
}
return post