def _update_trackweek(self, page=1): print 'INSIDE _UPDATE_TRACKWEEK' resp = Api.user_get_toptracks_week(self.lfm_username, page) if page == 1: from lfm.tasks import update_trackweek_task attr = resp['toptracks'].get('@attr') if attr is None: maxpages = 2 else: maxpages = int(attr.get('totalPages'))+1 for page_num in range(2, maxpages): update_trackweek_task.apply_async([self.id, page_num]) tracks = resp.get('toptracks').get('track') if tracks is None: return elif isinstance(tracks, list): for track in tracks: UserTrackWeek.create(self, track) elif isinstance(tracks, dict): Track.make_trackweek(self, tracks) #TODO: add this classmethod to the track class if int(resp['toptracks']['@attr']['page']) == int(resp['toptracks']['@attr']['totalPages']): self.updating_track_week = False self.tracks_week_updated_at = datetime.datetime.now() self.save()
def _get_addtl_info(self): resp = Api.track_getinfo(self.name, self.artist.name) track_dict = resp.get('track') self.album = None self.lfmid = track_dict.get('id') self.url = track_dict.get('ur') self.mbid = track_dict.get('mbid') self.listeners = track_dict.get('listeners') self.global_playcount = int(track_dict.get('playcount')) self.save() return True
def get_or_create(self, name): try: artist = Artist.objects.get(name=name) except ObjectDoesNotExist: resp = Api.artist_getinfo(name) artist_dict = resp.get('artist') artist = Artist(name=name) artist.lfm_url = artist_dict.get('url') artist.mbid = artist_dict.get('mbid') artist.listeners = artist_dict.get('stats').get('listeners') artist.global_playcount = artist_dict.get('stats').get('playcount') artist.save() return artist
def _update_friends(self, page=1): '''user is a UserProfile object''' print 'getting friends page for %s'%self from lfm.tasks import update_friends_task resp = Api.user_getfriends(self.lfm_username, page=page) for friend_data in resp.get('friends').get('user'): (friend,created) = UserProfile.objects.get_or_create(lfm_username=friend_data.get('name')) self.friends.add(friend) print 'added friend',friend if page == 1: total_pages = int(resp.get('friends').get('@attr').get('totalPages')) for page_num in range(2,total_pages+1): update_friends_task.wait([self.id, page_num]) self.update_friends_data()
import datetime from django.db import models from django.contrib.auth.models import User from django.dispatch import receiver from django.db.models.signals import post_save from lfm.helpers import Api from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned Api = Api() # Create your models here. class AbstractContent(models.Model): objects = models.Manager() created_at = models.DateTimeField(auto_now_add=True) modified_at = models.DateTimeField(auto_now=True) class Meta: abstract = True app_label = 'lfm' class AbstractSimilar(AbstractContent): match = models.FloatField() def __unicode__(self): return '<%s> (%s%)'%(str(self.from_id),self.match) class Meta: abstract = True app_label = 'lfm' class LFMContent(AbstractContent): tags = models.ManyToManyField("Tag") name = models.CharField(max_length=255, null=True, blank=True) tag_count = models.IntegerField(max_length=10, default=0) url = models.URLField(verify_exists=False, null=True, blank=True, max_length=500)