-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.py
241 lines (180 loc) · 6.27 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from flask import Flask, render_template, request, url_for, flash, redirect, Response
from pygtail import Pygtail
from flask_ask import Ask, question, statement, convert_errors, audio
from youtube_dl import YoutubeDL
from werkzeug.exceptions import abort
import sqlite3
import logging
import datetime
import os
import sys
import time
import pyfiglet
ip = '0.0.0.0' # System Ip
host = '0.0.0.0' # doesn't require anything else since we're using ngrok
port = 5000 # may want to check and make sure this port isn't being used by anything else
LOG_FILE = 'app.log'
log = logging.getLogger('__name__')
logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG)
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
def get_post(post_id):
conn = get_db_connection()
post = conn.execute('SELECT * FROM posts WHERE id = ?',
(post_id,)).fetchone()
conn.close()
if post is None:
abort(404)
return post
ytdl_options = {
'format': 'bestaudio/best',
'restrictfilenames': False,
'noplaylist': False,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': ip
}
ytdl = YoutubeDL(ytdl_options)
app = Flask(__name__)
app.config["DEBUG"] = os.environ.get("FLASK_DEBUG", True)
app.config["JSON_AS_ASCII"] = False
app.config['SECRET_KEY'] = 'dev'
app.config.from_mapping(
BASE_URL="http://localhost:5000",
)
ascii_banner = pyfiglet.figlet_format("ALPHA VIDEO")
print(ascii_banner)
print("By AndrewsTech")
@app.route('/')
def index():
conn = get_db_connection()
posts = conn.execute('SELECT * FROM posts').fetchall()
conn.close()
return render_template('index.html', posts=posts)
@app.route('/progress')
def progress():
def generate():
x = 0
while x <= 100:
yield "data:" + str(x) + "\n\n"
x = x + 10
time.sleep(0.5)
return Response(generate(), mimetype= 'text/event-stream')
@app.route('/create', methods=('GET', 'POST'))
def create():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
if not title:
flash('Title is required!')
else:
conn = get_db_connection()
conn.execute('INSERT INTO posts (title, content) VALUES (?, ?)',
(title, content))
conn.commit()
conn.close()
return redirect(url_for('index'))
return render_template('create.html')
@app.route('/<int:id>/delete', methods=('GET',))
def delete(id):
post = get_post(id)
conn = get_db_connection()
conn.execute('DELETE FROM posts WHERE id = ?', (id,))
conn.commit()
conn.close()
flash('"{}" was successfully deleted!'.format(post['title']))
return redirect(url_for('index'))
@app.route('/log')
def progress_log():
def generate():
for line in Pygtail(LOG_FILE, every_n=1):
yield "data:" + str(line) + "\n\n"
time.sleep(0.5)
return Response(generate(), mimetype= 'text/event-stream')
@app.route('/url')
def url_pro():
with open("/tmp/supervisord.log", "r") as f:
content = f.read()
return render_template('url.html', content=content)
@app.route('/env')
def show_env():
log.info("route =>'/env' - hit")
env = {}
for k,v in request.environ.items():
env[k] = str(v)
log.info("route =>'/env' [env]:\n%s" % env)
return env
@app.route("/logs", methods=["GET"])
def logstream():
return render_template('logs.html')
ask = Ask(app, '/api')
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
@ask.launch
def launch():
return question('Say an artist and/or song name')
@ask.session_ended
def session_ended():
return "{}", 200
@ask.intent('AMAZON.StopIntent')
def handle_stop_intent():
return statement('Okay')
@ask.intent('AMAZON.CancelIntent')
def handle_stop_intent():
return statement('Okay')
@ask.intent('AMAZON.PauseIntent')
def handle_pause_intent():
return audio('Stopping music').stop()
@ask.intent('AMAZON.ResumeIntent')
def resume():
return audio('Resuming.').resume()
@ask.intent('AMAZON.FallbackIntent')
def handle_fallback_intent():
return question('you have to start your command with play, search, or look for')
@ask.intent('AMAZON.HelpIntent')
def handle_help_intent():
return question('you have to start your command with play, search, or look for')
@ask.intent('QueryIntent', mapping={'query': 'Query'})
def handle_query_intent(query):
if not query or 'query' in convert_errors:
return question('Say an artist and/or song name')
data = ytdl.extract_info(f"ytsearch:{query}", download=False)
search_results = data['entries']
if not search_results:
return question('no results found, try another search query')
result = search_results[0]
song_name = result['title']
channel_name = result['uploader']
for format_ in result['formats']:
if format_['ext'] == 'm4a':
mp3_url = format_['url']
return audio(f'now playing {song_name} by {channel_name}').play(mp3_url)
return question('no results found, try another search query')
@app.route('/<int:post_id>')
def post(post_id):
post = get_post(post_id)
return render_template('post.html', post=post)
@app.route('/<int:id>/edit', methods=('GET', 'POST'))
def edit(id):
post = get_post(id)
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
if not title:
flash('Title is required!')
else:
conn = get_db_connection()
conn.execute('UPDATE posts SET title = ?, content = ?'
' WHERE id = ?',
(title, content, id))
conn.commit()
conn.close()
return redirect(url_for('index'))
return render_template('edit.html', post=post)
app.run(host=host, port=port)
# Made by andrewstech https://github.com/unofficial-skills/alpha-video/