DmitrMakeev commited on
Commit
2920ac3
·
verified ·
1 Parent(s): 2b9c46f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -109
app.py CHANGED
@@ -1,114 +1,39 @@
1
- from flask import Flask, request, send_from_directory, render_template_string
2
- from flask_socketio import SocketIO, send, emit
3
  import os
4
 
5
- UPLOAD_FOLDER = 'static'
6
- IMAGE_FILENAME = 'latest_image.jpg'
7
-
8
  app = Flask(__name__)
9
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
10
- socketio = SocketIO(app)
11
-
12
- # Создание директории, если она не существует
13
- if not os.path.exists(UPLOAD_FOLDER):
14
- os.makedirs(UPLOAD_FOLDER)
15
-
16
- @app.route('/upload', methods=['POST'])
17
- def upload_file():
18
- if 'photo' not in request.files:
19
- return "No file part", 400
20
- file = request.files['photo']
21
- if file.filename == '':
22
- return "No selected file", 400
23
- save_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
24
- file.save(save_path)
25
- return f"File uploaded successfully and saved to {save_path}", 200
26
-
27
- @app.route('/image', methods=['GET'])
28
- def get_image():
29
- return send_from_directory(UPLOAD_FOLDER, IMAGE_FILENAME)
30
-
31
- @app.route('/')
32
- def index():
33
- html = '''
34
- <!DOCTYPE html>
35
- <html lang="en">
36
- <head>
37
- <meta charset="UTF-8">
38
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
39
- <title>Camera Image</title>
40
- </head>
41
- <body>
42
- <h1>Latest Image</h1>
43
- <img id="cameraImage" src="/image" alt="Image" style="width:100%;">
44
- <script>
45
- setInterval(function(){
46
- var image = document.getElementById("cameraImage");
47
- image.src = "/image?" + new Date().getTime();
48
- }, 10000); // обновление каждые 10 секунд
49
- </script>
50
- </body>
51
- </html>
52
- '''
53
- return render_template_string(html)
54
-
55
- @app.route('/chat')
56
- def chat():
57
- return flask.render_template('chat.html')
58
-
59
-
60
-
61
-
62
-
63
-
64
- @app.route('/chat2')
65
- def chat2():
66
- return flask.render_template('chat2.html')
67
-
68
-
69
-
70
-
71
-
72
-
73
- @socketio.on('message')
74
- def handle_message(message):
75
- print('Received message: ' + message)
76
- send(message, broadcast=True)
77
-
78
-
79
-
80
-
81
- @app.route('/upload_form')
82
- def upload_form():
83
- html = '''
84
- <!DOCTYPE html>
85
- <html lang="en">
86
- <head>
87
- <meta charset="UTF-8">
88
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
89
- <title>Upload Image</title>
90
- </head>
91
- <body>
92
- <h1>Upload Image</h1>
93
- <form action="/upload" method="post" enctype="multipart/form-data">
94
- <input type="file" name="photo" accept="image/*">
95
- <button type="submit">Upload</button>
96
- </form>
97
- <div id="message"></div>
98
- </body>
99
- </html>
100
- '''
101
- return render_template_string(html)
102
-
103
- @socketio.on('message')
104
- def handle_message(msg):
105
- print('Message: ' + msg)
106
- send(msg, broadcast=True)
107
-
108
- @socketio.on('json')
109
- def handle_json(json):
110
- print('JSON: ' + str(json))
111
- send(json, json=True, broadcast=True)
112
 
113
  if __name__ == '__main__':
114
- socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)
 
1
+ from flask import Flask, request, redirect, session
2
+ import requests
3
  import os
4
 
 
 
 
5
  app = Flask(__name__)
6
+ app.secret_key = 'YOUR_SECRET_KEY' # Замените на ваш секретный ключ
7
+
8
+ def get_access_token(code):
9
+ client_id = 'YOUR_APP_ID'
10
+ client_secret = 'YOUR_APP_SECRET'
11
+ redirect_uri = 'https://example.com/login'
12
+
13
+ url = f'https://oauth.vk.com/access_token?client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}&code={code}'
14
+ response = requests.get(url)
15
+ data = response.json()
16
+
17
+ if 'access_token' in data:
18
+ access_token = data['access_token']
19
+ user_id = data['user_id']
20
+ return access_token, user_id
21
+ else:
22
+ return None, None
23
+
24
+ @app.route('/login')
25
+ def login():
26
+ code = request.args.get('code')
27
+ if code:
28
+ access_token, user_id = get_access_token(code)
29
+ if access_token and user_id:
30
+ session['access_token'] = access_token
31
+ session['user_id'] = user_id
32
+ return redirect('/')
33
+ else:
34
+ return "Ошибка при получении access_token", 400
35
+ else:
36
+ return "Код авторизации не найден", 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  if __name__ == '__main__':
39
+ app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))