-
Notifications
You must be signed in to change notification settings - Fork 6
/
manage.py
118 lines (86 loc) · 3.3 KB
/
manage.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
from datetime import datetime, timedelta
import os
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.script import Command, Manager, Option
from sqlalchemy import exists
from dotify import app
from dotify.database import Base, session
from dotify.models import Country, Operator, TopSong, CountryVector, SongVector
from dotify.resources.countries import countries
from dotify.resources.operators import OPERATORS
from dotify.top_songs import TopSongsGenerator, logger
from dotify.recommendation.implicit_mf.ratings_matrix import RatingsMatrix
from dotify.recommendation.implicit_mf.implicit_mf import ImplicitMF
from dotify.recommendation.implicit_mf.pipeline import ImplicitMFPipeline
manager = Manager(app)
F = 30
ALPHA = 10e0
LAMBDA = 25e1
@manager.command
def run():
port = int(os.environ.get('PORT', 8080))
app.run(host='0.0.0.0', port=port)
dotify.routes.refresh_latent_vectors()
@manager.command
def insert_countries():
for country_name in countries.keys():
country = Country(
id=countries[country_name]['id'],
name=country_name,
value=country_name
)
session.add(country)
session.commit()
@manager.command
def insert_operators():
for operator_id in OPERATORS.keys():
operator = Operator(
id=operator_id,
name=OPERATORS[operator_id]['name'],
value=OPERATORS[operator_id]['value']
)
session.add(operator)
session.commit()
@manager.option('-d', '--start-date', help='Backfill to this date')
def backfill_top_songs(start_date):
start_date = datetime.strptime(start_date, '%Y-%m-%d')
end_date = datetime.now()
current_date = start_date
while current_date < end_date:
current_date_string = current_date.strftime('%Y-%m-%d')
InsertTopSongs().run(date=current_date_string)
logger.info( current_date_string + ' √' )
current_date += timedelta(days=1)
@manager.command
def compute_latent_vectors():
implicit_mf = ImplicitMF(ratings_matrix=RatingsMatrix(), f=F, alpha=ALPHA, lmbda=LAMBDA)
ImplicitMFPipeline(implicit_mf).run()
class InsertTopSongs(Command):
option_list = (
Option('-d', '--date', help='If no date passed, today\'s date is used'),
)
def run(self, date):
for country_name in countries.keys():
top_songs_generator = TopSongsGenerator(country_name=country_name, date=date)
if not top_songs_generator.daily_chart.dataframe.empty:
for top_song in top_songs_generator:
if not self._top_song_exists(top_song):
session.add(top_song)
session.commit()
@staticmethod
def _top_song_exists(top_song):
return session.query(exists()\
.where(TopSong.song_id==top_song.song_id)\
.where(TopSong.country_id==top_song.country_id)\
.where(TopSong.rank==top_song.rank)\
.where(TopSong.streams==top_song.streams)\
.where(TopSong.date==top_song.date)
).scalar()
manager.add_command('insert_top_songs', InsertTopSongs())
class DB:
def __init__(self, metadata):
self.metadata = metadata
migrate = Migrate(app, DB(Base.metadata))
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()