-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
107 lines (91 loc) · 3.47 KB
/
app.py
File metadata and controls
107 lines (91 loc) · 3.47 KB
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
import os
import pymongo
from flask import Flask, request, jsonify, Response, send_from_directory
app = Flask(__name__)
# MongoDB connection
MONGO_URI = os.environ.get('MONGO_URI', '')
client = pymongo.MongoClient(MONGO_URI)
db = client['vcfdb']
contacts_col = db['contacts']
settings_col = db['settings']
@app.route('/')
def user_page():
return send_from_directory('.', 'user.html')
@app.route('/admin')
def admin_page():
return send_from_directory('.', 'admin.html')
@app.route('/stats')
def stats():
total_users = contacts_col.count_documents({})
config = settings_col.find_one({'_id': 'config'})
target = config['target'] if config else 500
return jsonify({'total_users': total_users, 'target': target})
@app.route('/submit', methods=['POST'])
def submit():
data = request.json
if not data or 'phone' not in data:
return jsonify({'success': False, 'message': 'Phone number is required'}), 400
existing_contact = contacts_col.find_one({'phone': data['phone']})
if existing_contact:
return jsonify({'success': False, 'message': 'This phone number is already registered'}), 400
contacts_col.insert_one(data)
return jsonify({'success': True})
@app.route('/get_contacts')
def get_contacts():
page = request.args.get('page', default=0, type=int)
limit = request.args.get('limit', default=10, type=int)
skip = page * limit
users = list(contacts_col.find({}, {'_id': 0}).skip(skip).limit(limit))
return jsonify(users)
@app.route('/update_target', methods=['POST'])
def update_target():
data = request.json
settings_col.update_one(
{'_id': 'config'},
{'$set': {'target': data['target']}},
upsert=True
)
return jsonify({'success': True})
@app.route('/get_prefix')
def get_prefix():
config = settings_col.find_one({'_id': 'config'})
prefix = config.get('prefix', '') if config else ''
return jsonify({'prefix': prefix})
@app.route('/update_prefix', methods=['POST'])
def update_prefix():
data = request.json
settings_col.update_one(
{'_id': 'config'},
{'$set': {'prefix': data['prefix']}},
upsert=True
)
return jsonify({'success': True})
@app.route('/generate_vcf')
def generate_vcf():
users = list(contacts_col.find({}, {'_id': 0}))
config = settings_col.find_one({'_id': 'config'})
prefix = config.get('prefix', '') if config else ''
vcf_content = ''
for user in users:
full_name = f"{prefix}{user.get('name', '')}" if user.get('name') else ''
vcf_content += f"""BEGIN:VCARD\r
VERSION:3.0\r
FN:{full_name}\r
NICKNAME:{user.get('username', '')}\r
TEL:{user.get('phone', '')}\r
END:VCARD\r
"""
resp = Response(vcf_content.encode('utf-8'), mimetype='text/vcard; charset=utf-8')
resp.headers['Content-Disposition'] = 'attachment; filename=contacts.vcf'
return resp
@app.route('/clear_contacts', methods=['POST'])
def clear_contacts():
try:
result = contacts_col.delete_many({})
if result.deleted_count > 0:
return jsonify({'success': True, 'message': f'Cleared {result.deleted_count} contacts!'})
return jsonify({'success': True, 'message': 'No contacts to clear.'})
except Exception as e:
return jsonify({'success': False, 'message': f'Error clearing contacts: {str(e)}'}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)