Update app.py
Browse files
app.py
CHANGED
|
@@ -27,18 +27,18 @@ def load_data():
|
|
| 27 |
data = json.load(file)
|
| 28 |
if not isinstance(data, dict):
|
| 29 |
logging.warning("Data is not in dict format, initializing empty database")
|
| 30 |
-
return {'posts': [], 'users': {}, '
|
| 31 |
if 'posts' not in data:
|
| 32 |
data['posts'] = []
|
| 33 |
if 'users' not in data:
|
| 34 |
data['users'] = {}
|
| 35 |
-
if '
|
| 36 |
-
data['
|
| 37 |
logging.info("Data loaded successfully")
|
| 38 |
return data
|
| 39 |
except Exception as e:
|
| 40 |
logging.error(f"Error loading data: {e}")
|
| 41 |
-
return {'posts': [], 'users': {}, '
|
| 42 |
|
| 43 |
def save_data(data):
|
| 44 |
try:
|
|
@@ -80,7 +80,7 @@ def download_db_from_hf():
|
|
| 80 |
logging.error(f"Error downloading database: {e}")
|
| 81 |
if not os.path.exists(DATA_FILE):
|
| 82 |
with open(DATA_FILE, 'w', encoding='utf-8') as f:
|
| 83 |
-
json.dump({'posts': [], 'users': {}, '
|
| 84 |
|
| 85 |
def periodic_backup():
|
| 86 |
while True:
|
|
@@ -1500,83 +1500,60 @@ def upload():
|
|
| 1500 |
'''
|
| 1501 |
return render_template_string(html, username=username, is_authenticated=is_authenticated)
|
| 1502 |
|
| 1503 |
-
# Chat
|
| 1504 |
@app.route('/chat', methods=['GET', 'POST'])
|
| 1505 |
def chat():
|
| 1506 |
data = load_data()
|
| 1507 |
is_authenticated = 'username' in session
|
| 1508 |
username = session.get('username', None)
|
| 1509 |
-
|
| 1510 |
-
# Get user's chats if authenticated, otherwise show all chats
|
| 1511 |
-
if is_authenticated:
|
| 1512 |
-
user_chats = {chat_id: chat for chat_id, chat in data['chats'].items()
|
| 1513 |
-
if username in chat['participants']}
|
| 1514 |
-
else:
|
| 1515 |
-
user_chats = data['chats']
|
| 1516 |
|
| 1517 |
if request.method == 'POST' and is_authenticated:
|
| 1518 |
-
|
| 1519 |
-
|
| 1520 |
-
|
| 1521 |
-
|
| 1522 |
-
|
| 1523 |
-
|
| 1524 |
-
|
| 1525 |
-
|
| 1526 |
-
save_data(data)
|
| 1527 |
-
return redirect(url_for('chat', chat_id=chat_id))
|
| 1528 |
|
| 1529 |
-
|
| 1530 |
-
|
| 1531 |
-
|
| 1532 |
-
|
| 1533 |
-
|
| 1534 |
-
|
| 1535 |
-
|
| 1536 |
-
|
| 1537 |
-
'sender': username,
|
| 1538 |
-
'time': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 1539 |
-
}
|
| 1540 |
-
|
| 1541 |
-
if message:
|
| 1542 |
-
message_data['text'] = message
|
| 1543 |
-
|
| 1544 |
-
if file and file.filename:
|
| 1545 |
-
filename = secure_filename(file.filename)
|
| 1546 |
-
temp_path = os.path.join('uploads', filename)
|
| 1547 |
-
os.makedirs('uploads', exist_ok=True)
|
| 1548 |
-
file.save(temp_path)
|
| 1549 |
-
|
| 1550 |
-
file_type = 'video' if filename.lower().endswith(('.mp4', '.mov', '.avi')) else 'photo'
|
| 1551 |
-
api = HfApi()
|
| 1552 |
-
file_path = f"chat_files/{chat_id}/{filename}"
|
| 1553 |
-
api.upload_file(
|
| 1554 |
-
path_or_fileobj=temp_path,
|
| 1555 |
-
path_in_repo=file_path,
|
| 1556 |
-
repo_id=REPO_ID,
|
| 1557 |
-
repo_type="dataset",
|
| 1558 |
-
token=HF_TOKEN_WRITE,
|
| 1559 |
-
commit_message=f"Uploaded chat file by {username}"
|
| 1560 |
-
)
|
| 1561 |
-
message_data['file'] = file_path
|
| 1562 |
-
message_data['file_type'] = file_type
|
| 1563 |
-
if os.path.exists(temp_path):
|
| 1564 |
-
os.remove(temp_path)
|
| 1565 |
-
|
| 1566 |
-
if post_id:
|
| 1567 |
-
post = next((p for p in data['posts'] if p['id'] == post_id and p['uploader'] == username), None)
|
| 1568 |
-
if post:
|
| 1569 |
-
message_data['post_id'] = post_id
|
| 1570 |
-
|
| 1571 |
-
if 'text' in message_data or 'file' in message_data or 'post_id' in message_data:
|
| 1572 |
-
data['chats'][chat_id]['messages'].append(message_data)
|
| 1573 |
-
save_data(data)
|
| 1574 |
|
| 1575 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1576 |
|
| 1577 |
-
selected_chat_id = request.args.get('chat_id')
|
| 1578 |
-
selected_chat = user_chats.get(selected_chat_id, None)
|
| 1579 |
-
|
| 1580 |
html = '''
|
| 1581 |
<!DOCTYPE html>
|
| 1582 |
<html lang="en">
|
|
@@ -1584,56 +1561,29 @@ def chat():
|
|
| 1584 |
<meta charset="UTF-8">
|
| 1585 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 1586 |
<meta name="keywords" content="interracial porn, bbc porn, qos, queen of spades, big black cock">
|
| 1587 |
-
<title>Chat - Content Hub</title>
|
| 1588 |
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&display=swap" rel="stylesheet">
|
| 1589 |
<style>
|
| 1590 |
''' + BASE_STYLE + '''
|
| 1591 |
.container {
|
| 1592 |
-
|
| 1593 |
-
max-width: 1200px;
|
| 1594 |
-
gap: 30px;
|
| 1595 |
-
}
|
| 1596 |
-
.chat-list {
|
| 1597 |
-
width: 300px;
|
| 1598 |
-
background: var(--card-bg-light);
|
| 1599 |
-
backdrop-filter: blur(20px);
|
| 1600 |
-
padding: 25px;
|
| 1601 |
-
border-radius: 20px;
|
| 1602 |
-
box-shadow: var(--shadow);
|
| 1603 |
-
}
|
| 1604 |
-
body.dark .chat-list {
|
| 1605 |
-
background: var(--card-bg-dark);
|
| 1606 |
-
}
|
| 1607 |
-
.chat-area {
|
| 1608 |
-
flex: 1;
|
| 1609 |
background: var(--card-bg-light);
|
| 1610 |
backdrop-filter: blur(20px);
|
| 1611 |
-
padding:
|
| 1612 |
-
border-radius:
|
| 1613 |
box-shadow: var(--shadow);
|
| 1614 |
}
|
| 1615 |
-
body.dark .
|
| 1616 |
background: var(--card-bg-dark);
|
| 1617 |
}
|
| 1618 |
h1 {
|
| 1619 |
-
font-size: 2.
|
|
|
|
| 1620 |
margin-bottom: 30px;
|
| 1621 |
background: linear-gradient(45deg, var(--primary), var(--secondary));
|
| 1622 |
-webkit-background-clip: text;
|
| 1623 |
color: transparent;
|
| 1624 |
}
|
| 1625 |
-
.chat-item {
|
| 1626 |
-
padding: 15px;
|
| 1627 |
-
background: var(--glass-bg);
|
| 1628 |
-
border-radius: 15px;
|
| 1629 |
-
margin-bottom: 15px;
|
| 1630 |
-
cursor: pointer;
|
| 1631 |
-
transition: var(--transition);
|
| 1632 |
-
}
|
| 1633 |
-
.chat-item:hover {
|
| 1634 |
-
background: var(--primary);
|
| 1635 |
-
color: white;
|
| 1636 |
-
}
|
| 1637 |
.message {
|
| 1638 |
padding: 15px;
|
| 1639 |
background: var(--glass-bg);
|
|
@@ -1674,69 +1624,48 @@ def chat():
|
|
| 1674 |
''' + NAV_HTML + '''
|
| 1675 |
<button class="theme-toggle" onclick="toggleTheme()">🌙</button>
|
| 1676 |
<div class="container">
|
| 1677 |
-
<
|
| 1678 |
-
|
| 1679 |
-
{%
|
| 1680 |
-
<
|
| 1681 |
-
<
|
| 1682 |
-
|
| 1683 |
-
|
| 1684 |
-
|
| 1685 |
-
|
| 1686 |
-
|
| 1687 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1688 |
</div>
|
| 1689 |
{% endfor %}
|
| 1690 |
</div>
|
| 1691 |
-
|
| 1692 |
-
|
| 1693 |
-
<
|
| 1694 |
-
<
|
| 1695 |
-
|
| 1696 |
-
|
| 1697 |
-
|
| 1698 |
-
|
| 1699 |
-
<p>{{ message['text'] }}</p>
|
| 1700 |
-
{% endif %}
|
| 1701 |
-
{% if 'file' in message %}
|
| 1702 |
-
{% if message['file_type'] == 'video' %}
|
| 1703 |
-
<video controls>
|
| 1704 |
-
<source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" type="video/mp4">
|
| 1705 |
-
</video>
|
| 1706 |
-
{% else %}
|
| 1707 |
-
<img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" alt="Chat file" onclick="openModal(this.src)">
|
| 1708 |
-
{% endif %}
|
| 1709 |
-
{% endif %}
|
| 1710 |
-
{% if 'post_id' in message %}
|
| 1711 |
-
{% with post = posts|selectattr('id', 'equalto', message['post_id'])|first %}
|
| 1712 |
-
{% if post %}
|
| 1713 |
-
<p>Shared post: <a href="{{ url_for('post_page', post_id=message['post_id']) }}">{{ post['title'] }}</a></p>
|
| 1714 |
-
{% endif %}
|
| 1715 |
-
{% endwith %}
|
| 1716 |
-
{% endif %}
|
| 1717 |
-
</div>
|
| 1718 |
{% endfor %}
|
| 1719 |
-
</
|
| 1720 |
-
|
| 1721 |
-
|
| 1722 |
-
|
| 1723 |
-
|
| 1724 |
-
|
| 1725 |
-
<select name="post_id" class="post-select">
|
| 1726 |
-
<option value="">Share a post (optional)</option>
|
| 1727 |
-
{% for post in user_posts %}
|
| 1728 |
-
<option value="{{ post['id'] }}">{{ post['title'] }}</option>
|
| 1729 |
-
{% endfor %}
|
| 1730 |
-
</select>
|
| 1731 |
-
<button type="submit" name="send_message" class="btn">Send</button>
|
| 1732 |
-
</form>
|
| 1733 |
-
{% else %}
|
| 1734 |
-
<p style="margin-top: 20px;"><a href="{{ url_for('login') }}">Login</a> to send messages.</p>
|
| 1735 |
-
{% endif %}
|
| 1736 |
-
{% else %}
|
| 1737 |
-
<h1>Select a chat to view messages</h1>
|
| 1738 |
-
{% endif %}
|
| 1739 |
-
</div>
|
| 1740 |
</div>
|
| 1741 |
<div class="modal" id="imageModal" onclick="closeModal(event)">
|
| 1742 |
<img id="modalImage" src="">
|
|
@@ -1769,9 +1698,7 @@ def chat():
|
|
| 1769 |
'''
|
| 1770 |
user_posts = [p for p in data['posts'] if p['uploader'] == username] if is_authenticated else []
|
| 1771 |
return render_template_string(html,
|
| 1772 |
-
|
| 1773 |
-
selected_chat=selected_chat,
|
| 1774 |
-
selected_chat_id=selected_chat_id,
|
| 1775 |
username=username,
|
| 1776 |
is_authenticated=is_authenticated,
|
| 1777 |
repo_id=REPO_ID,
|
|
|
|
| 27 |
data = json.load(file)
|
| 28 |
if not isinstance(data, dict):
|
| 29 |
logging.warning("Data is not in dict format, initializing empty database")
|
| 30 |
+
return {'posts': [], 'users': {}, 'general_chat': []}
|
| 31 |
if 'posts' not in data:
|
| 32 |
data['posts'] = []
|
| 33 |
if 'users' not in data:
|
| 34 |
data['users'] = {}
|
| 35 |
+
if 'general_chat' not in data:
|
| 36 |
+
data['general_chat'] = []
|
| 37 |
logging.info("Data loaded successfully")
|
| 38 |
return data
|
| 39 |
except Exception as e:
|
| 40 |
logging.error(f"Error loading data: {e}")
|
| 41 |
+
return {'posts': [], 'users': {}, 'general_chat': []}
|
| 42 |
|
| 43 |
def save_data(data):
|
| 44 |
try:
|
|
|
|
| 80 |
logging.error(f"Error downloading database: {e}")
|
| 81 |
if not os.path.exists(DATA_FILE):
|
| 82 |
with open(DATA_FILE, 'w', encoding='utf-8') as f:
|
| 83 |
+
json.dump({'posts': [], 'users': {}, 'general_chat': []}, f)
|
| 84 |
|
| 85 |
def periodic_backup():
|
| 86 |
while True:
|
|
|
|
| 1500 |
'''
|
| 1501 |
return render_template_string(html, username=username, is_authenticated=is_authenticated)
|
| 1502 |
|
| 1503 |
+
# General Chat
|
| 1504 |
@app.route('/chat', methods=['GET', 'POST'])
|
| 1505 |
def chat():
|
| 1506 |
data = load_data()
|
| 1507 |
is_authenticated = 'username' in session
|
| 1508 |
username = session.get('username', None)
|
| 1509 |
+
chat_messages = data['general_chat']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1510 |
|
| 1511 |
if request.method == 'POST' and is_authenticated:
|
| 1512 |
+
message = request.form.get('message')
|
| 1513 |
+
file = request.files.get('file')
|
| 1514 |
+
post_id = request.form.get('post_id')
|
| 1515 |
+
|
| 1516 |
+
message_data = {
|
| 1517 |
+
'sender': username,
|
| 1518 |
+
'time': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 1519 |
+
}
|
|
|
|
|
|
|
| 1520 |
|
| 1521 |
+
if message:
|
| 1522 |
+
message_data['text'] = message
|
| 1523 |
+
|
| 1524 |
+
if file and file.filename:
|
| 1525 |
+
filename = secure_filename(file.filename)
|
| 1526 |
+
temp_path = os.path.join('uploads', filename)
|
| 1527 |
+
os.makedirs('uploads', exist_ok=True)
|
| 1528 |
+
file.save(temp_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1529 |
|
| 1530 |
+
file_type = 'video' if filename.lower().endswith(('.mp4', '.mov', '.avi')) else 'photo'
|
| 1531 |
+
api = HfApi()
|
| 1532 |
+
file_path = f"chat_files/general/{filename}"
|
| 1533 |
+
api.upload_file(
|
| 1534 |
+
path_or_fileobj=temp_path,
|
| 1535 |
+
path_in_repo=file_path,
|
| 1536 |
+
repo_id=REPO_ID,
|
| 1537 |
+
repo_type="dataset",
|
| 1538 |
+
token=HF_TOKEN_WRITE,
|
| 1539 |
+
commit_message=f"Uploaded chat file by {username}"
|
| 1540 |
+
)
|
| 1541 |
+
message_data['file'] = file_path
|
| 1542 |
+
message_data['file_type'] = file_type
|
| 1543 |
+
if os.path.exists(temp_path):
|
| 1544 |
+
os.remove(temp_path)
|
| 1545 |
+
|
| 1546 |
+
if post_id:
|
| 1547 |
+
post = next((p for p in data['posts'] if p['id'] == post_id and p['uploader'] == username), None)
|
| 1548 |
+
if post:
|
| 1549 |
+
message_data['post_id'] = post_id
|
| 1550 |
+
|
| 1551 |
+
if 'text' in message_data or 'file' in message_data or 'post_id' in message_data:
|
| 1552 |
+
data['general_chat'].append(message_data)
|
| 1553 |
+
save_data(data)
|
| 1554 |
+
|
| 1555 |
+
return redirect(url_for('chat'))
|
| 1556 |
|
|
|
|
|
|
|
|
|
|
| 1557 |
html = '''
|
| 1558 |
<!DOCTYPE html>
|
| 1559 |
<html lang="en">
|
|
|
|
| 1561 |
<meta charset="UTF-8">
|
| 1562 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 1563 |
<meta name="keywords" content="interracial porn, bbc porn, qos, queen of spades, big black cock">
|
| 1564 |
+
<title>General Chat - Content Hub</title>
|
| 1565 |
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&display=swap" rel="stylesheet">
|
| 1566 |
<style>
|
| 1567 |
''' + BASE_STYLE + '''
|
| 1568 |
.container {
|
| 1569 |
+
max-width: 950px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1570 |
background: var(--card-bg-light);
|
| 1571 |
backdrop-filter: blur(20px);
|
| 1572 |
+
padding: 50px;
|
| 1573 |
+
border-radius: 25px;
|
| 1574 |
box-shadow: var(--shadow);
|
| 1575 |
}
|
| 1576 |
+
body.dark .container {
|
| 1577 |
background: var(--card-bg-dark);
|
| 1578 |
}
|
| 1579 |
h1 {
|
| 1580 |
+
font-size: 2.5em;
|
| 1581 |
+
font-weight: 700;
|
| 1582 |
margin-bottom: 30px;
|
| 1583 |
background: linear-gradient(45deg, var(--primary), var(--secondary));
|
| 1584 |
-webkit-background-clip: text;
|
| 1585 |
color: transparent;
|
| 1586 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1587 |
.message {
|
| 1588 |
padding: 15px;
|
| 1589 |
background: var(--glass-bg);
|
|
|
|
| 1624 |
''' + NAV_HTML + '''
|
| 1625 |
<button class="theme-toggle" onclick="toggleTheme()">🌙</button>
|
| 1626 |
<div class="container">
|
| 1627 |
+
<h1>General Chat</h1>
|
| 1628 |
+
<div class="messages">
|
| 1629 |
+
{% for message in chat_messages %}
|
| 1630 |
+
<div class="message">
|
| 1631 |
+
<strong>{{ message['sender'] }}</strong> ({{ message['time'] }}):
|
| 1632 |
+
{% if 'text' in message %}
|
| 1633 |
+
<p>{{ message['text'] }}</p>
|
| 1634 |
+
{% endif %}
|
| 1635 |
+
{% if 'file' in message %}
|
| 1636 |
+
{% if message['file_type'] == 'video' %}
|
| 1637 |
+
<video controls>
|
| 1638 |
+
<source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" type="video/mp4">
|
| 1639 |
+
</video>
|
| 1640 |
+
{% else %}
|
| 1641 |
+
<img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" alt="Chat file" onclick="openModal(this.src)">
|
| 1642 |
+
{% endif %}
|
| 1643 |
+
{% endif %}
|
| 1644 |
+
{% if 'post_id' in message %}
|
| 1645 |
+
{% with post = posts|selectattr('id', 'equalto', message['post_id'])|first %}
|
| 1646 |
+
{% if post %}
|
| 1647 |
+
<p>Shared post: <a href="{{ url_for('post_page', post_id=message['post_id']) }}">{{ post['title'] }}</a></p>
|
| 1648 |
+
{% endif %}
|
| 1649 |
+
{% endwith %}
|
| 1650 |
+
{% endif %}
|
| 1651 |
</div>
|
| 1652 |
{% endfor %}
|
| 1653 |
</div>
|
| 1654 |
+
{% if is_authenticated %}
|
| 1655 |
+
<form method="POST" enctype="multipart/form-data" class="message-form">
|
| 1656 |
+
<textarea name="message" placeholder="Type a message" rows="3"></textarea>
|
| 1657 |
+
<input type="file" name="file" accept="video/*,image/*">
|
| 1658 |
+
<select name="post_id" class="post-select">
|
| 1659 |
+
<option value="">Share a post (optional)</option>
|
| 1660 |
+
{% for post in user_posts %}
|
| 1661 |
+
<option value="{{ post['id'] }}">{{ post['title'] }}</option>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1662 |
{% endfor %}
|
| 1663 |
+
</select>
|
| 1664 |
+
<button type="submit" class="btn">Send</button>
|
| 1665 |
+
</form>
|
| 1666 |
+
{% else %}
|
| 1667 |
+
<p style="margin-top: 20px;"><a href="{{ url_for('login') }}">Login</a> to send messages.</p>
|
| 1668 |
+
{% endif %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1669 |
</div>
|
| 1670 |
<div class="modal" id="imageModal" onclick="closeModal(event)">
|
| 1671 |
<img id="modalImage" src="">
|
|
|
|
| 1698 |
'''
|
| 1699 |
user_posts = [p for p in data['posts'] if p['uploader'] == username] if is_authenticated else []
|
| 1700 |
return render_template_string(html,
|
| 1701 |
+
chat_messages=chat_messages,
|
|
|
|
|
|
|
| 1702 |
username=username,
|
| 1703 |
is_authenticated=is_authenticated,
|
| 1704 |
repo_id=REPO_ID,
|