mirror of
https://github.com/Findus23/RadioStats.git
synced 2024-09-19 16:03:48 +02:00
server
This commit is contained in:
parent
7ecc90e473
commit
f7a6cf5a54
6 changed files with 67 additions and 5 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
config.py
|
||||
__pycache__/
|
||||
.idea/
|
||||
queries.sql
|
||||
|
|
20
app.py
Normal file
20
app.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# Blog configuration values.
|
||||
|
||||
from flask import Flask
|
||||
from peewee import MySQLDatabase
|
||||
from playhouse.flask_utils import FlaskDB, get_object_or_404, object_list
|
||||
|
||||
import config
|
||||
|
||||
DATABASE = MySQLDatabase("radio", **config.db)
|
||||
DEBUG = True
|
||||
|
||||
|
||||
# Create a Flask WSGI app and configure it using values from the module.
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(__name__)
|
||||
|
||||
flask_db = FlaskDB(app)
|
||||
|
||||
db = flask_db.database
|
||||
|
|
@ -1,10 +1,13 @@
|
|||
import __main__
|
||||
from peewee import Model, MySQLDatabase
|
||||
|
||||
import config
|
||||
|
||||
db = MySQLDatabase("radio", **config.db)
|
||||
|
||||
db.connect()
|
||||
if __main__.__file__ != "server.py":
|
||||
db = MySQLDatabase("radio", **config.db)
|
||||
db.connect()
|
||||
else:
|
||||
from app import db
|
||||
|
||||
|
||||
class BaseModel(Model):
|
||||
|
|
2
fetch.py
2
fetch.py
|
@ -81,8 +81,6 @@ for channel in Channel.select():
|
|||
|
||||
add_entry(time, artist, title)
|
||||
else:
|
||||
# if channel.shortname != "noe":
|
||||
# continue
|
||||
print(channel.streamurl + "played.html")
|
||||
r = careful_fetch(channel.streamurl + "played.html")
|
||||
soup = BeautifulSoup(r.text.encode('latin1').decode('utf8'), 'html.parser')
|
||||
|
|
40
server.py
Normal file
40
server.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
from flask import jsonify
|
||||
from playhouse.shortcuts import model_to_dict
|
||||
|
||||
from app import app
|
||||
from models import *
|
||||
|
||||
|
||||
def query_to_response(query, limit=5, **kwargs):
|
||||
"""
|
||||
|
||||
:param limit: int|boolean
|
||||
:param **kwargs
|
||||
:type query: peewee.ModelSelect
|
||||
"""
|
||||
if limit:
|
||||
query = query.limit(limit)
|
||||
print(query.sql())
|
||||
data = []
|
||||
for i in query:
|
||||
data.append(model_to_dict(i, **kwargs))
|
||||
return jsonify(data)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return query_to_response(Channel.select(), limit=False)
|
||||
|
||||
|
||||
@app.route('/<channel>')
|
||||
def popular(channel):
|
||||
# range = request.args.get('')
|
||||
get = Play.select(Play.song, fn.Count(SQL('*')).alias("magnitude")) \
|
||||
.join(Channel).switch(Play).join(Song) \
|
||||
.where((Song.show == 0) & (Channel.shortname == channel)) \
|
||||
.group_by(Play.song).order_by(SQL('magnitude').desc())
|
||||
return query_to_response(get, extra_attrs=["magnitude"])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
Loading…
Reference in a new issue