Skip to content

Commit

Permalink
Add action to force LastFM Update
Browse files Browse the repository at this point in the history
  • Loading branch information
DocMarty84 committed May 11, 2018
1 parent c8e496f commit 93c2dae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
13 changes: 13 additions & 0 deletions data/oomusic_artist_data.xml
@@ -1,5 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="oomusic_artist_reload_info" model="ir.actions.server">
<field name="name">Update LastFM Info</field>
<field name="model_id" ref="model_oomusic_artist"/>
<field name="binding_model_id" ref="model_oomusic_artist"/>
<field name="state">code</field>
<field name="code">
if env.context.get('active_ids'):
Artist = model.browse(env.context['active_ids'])
Artist.action_reload_info()
</field>
</record>
</data>
<data noupdate="1">
<!-- Cron to build image cache -->
<record id="oomusic_build_artists_image_cache" model="ir.cron">
Expand Down
19 changes: 13 additions & 6 deletions models/oomusic_artist.py
Expand Up @@ -146,6 +146,13 @@ def action_add_to_playlist(self):
for artist in self:
playlist._add_tracks(artist.track_ids)

def action_reload_info(self):
for artist in self:
artist._lastfm_artist_getinfo(force=True)
artist._lastfm_artist_getsimilar(force=True)
artist._lastfm_artist_gettoptracks(force=True)
artist.with_context(build_cache=True)._compute_fm_image()

@api.model
def cron_build_image_cache(self):
# Build a random sample of 500 artists to compute the images for. Every 50, the cache is
Expand All @@ -160,17 +167,17 @@ def cron_build_image_cache(self):
artist._compute_fm_image()
self.invalidate_cache()

def _lastfm_artist_getinfo(self):
def _lastfm_artist_getinfo(self, force=False):
self.ensure_one()
url = 'https://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=' + self.name
return json.loads(self.env['oomusic.lastfm'].get_query(url))
return json.loads(self.env['oomusic.lastfm'].get_query(url, force=force))

def _lastfm_artist_getsimilar(self):
def _lastfm_artist_getsimilar(self, force=False):
self.ensure_one()
url = 'https://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=' + self.name
return json.loads(self.env['oomusic.lastfm'].get_query(url))
return json.loads(self.env['oomusic.lastfm'].get_query(url, force=force))

def _lastfm_artist_gettoptracks(self):
def _lastfm_artist_gettoptracks(self, force=False):
self.ensure_one()
url = 'https://ws.audioscrobbler.com/2.0/?method=artist.gettoptracks&artist=' + self.name
return json.loads(self.env['oomusic.lastfm'].get_query(url))
return json.loads(self.env['oomusic.lastfm'].get_query(url, force=force))
4 changes: 2 additions & 2 deletions models/oomusic_lastfm.py
Expand Up @@ -29,7 +29,7 @@ class MusicLastfm(models.Model):
('oomusic_lastfm_name_uniq', 'unique(name)', 'URL hash must be unique!'),
]

def get_query(self, url, sleep=0.0):
def get_query(self, url, sleep=0.0, force=False):
# Get LastFM key and cache duration
ConfigParam = self.env['ir.config_parameter'].sudo()
fm_key = ConfigParam.get_param('oomusic.lastfm_key')
Expand All @@ -42,7 +42,7 @@ def get_query(self, url, sleep=0.0):

new_cr = self.pool.cursor()
Lastfm = self.with_env(self.env(cr=new_cr)).search([('name', '=', url_hash)])
if not Lastfm or Lastfm.expiry_date < fields.Datetime.now():
if force or not Lastfm or Lastfm.expiry_date < fields.Datetime.now():
content = '{}'
try:
time.sleep(sleep)
Expand Down

0 comments on commit 93c2dae

Please sign in to comment.