Beispiel #1
0
    def mb_complete_media(self, obj, mb_id, excludes=()):
        
        log = logging.getLogger('util.importer.mb_complete_media')
        log.info('complete media, m: %s | mb_id: %s' % (obj.name, mb_id))
        
        #raw_input("Press Enter to continue...")
        time.sleep(1.1)
        
        inc = ('artists', 'url-rels', 'aliases', 'tags', 'recording-rels', 'artist-rels', 'work-level-rels', 'artist-credits')
        url = 'http://%s/ws/2/recording/%s/?fmt=json&inc=%s' % (MUSICBRAINZ_HOST, mb_id, "+".join(inc))
        
        r = requests.get(url)
        result = r.json()
        
        
        print '*****************************************************************'
        print '*****************************************************************'
        print '*****************************************************************'
        
        self.pp.pprint(result)


        print '*****************************************************************'
        print '*****************************************************************'
        print '*****************************************************************'
        
        # self.pp.pprint(result)
        if 'relations' in result:
            for relation in result['relations']:
    
                
                # map artists
                if 'artist' in relation:
                    print 'artist: %s' % relation['artist']['name']
                    print 'mb_id:   %s' % relation['artist']['id']
                    print 'role:   %s' % relation['type']
                    print
                    time.sleep(0.1)
                    l_as = lookup.artist_by_mb_id(relation['artist']['id'])
                    l_a = None

                    
                    #if len(l_as) < 1 and relation['artist']['id'] not in self.mb_completed:
                    if len(l_as) < 1 and relation['artist']['id'] not in excludes:
                        self.mb_completed.append(relation['artist']['id'])
                        l_a = Artist(name=relation['artist']['name'])
                        l_a.save()
                            
                        url = 'http://musicbrainz.org/artist/%s' % relation['artist']['id']
                        print 'musicbrainz_url: %s' % url
                        rel = Relation(content_object=l_a, url=url)
                        rel.save()
                        
                        print 'artist created'
                    if len(l_as) == 1:
                        print 'got artist!'
                        l_a = l_as[0]
                        print l_as[0]
                        
                    profession = None
                    if 'type' in relation:
                        profession, created = Profession.objects.get_or_create(name=relation['type'])
                        
                        
                    """"""
                    if l_a:
                        mea, created = MediaExtraartists.objects.get_or_create(artist=l_a, media=obj, profession=profession)
                        l_a = self.mb_complete_artist(l_a, relation['artist']['id'])
                    #self.pp.pprint(relation['artist']['name'])
                    
        tags = result.get('tags', ())
        for tag in tags:
            log.debug('got tag: %s' % (tag['name']))
            Tag.objects.add_tag(obj, '"%s"' % tag['name'])
            

                    
        # add mb relation
        mb_url = 'http://musicbrainz.org/recording/%s' % (mb_id)
        try:
            rel = Relation.objects.get(object_id=obj.pk, url=mb_url)
        except:
            log.debug('relation not here yet, add it: %s' % (mb_url))
            rel = Relation(content_object=obj, url=mb_url)
            rel.save()
        
        
        return obj
Beispiel #2
0
        # try to get artist by alibrary_id
        if alibrary_artist_id and not a:
            log.debug('artist, lookup by alibrary_artist_id: %s' % alibrary_artist_id)
            try:
                a = Artist.objects.get(pk=alibrary_artist_id)
                log.debug('got artist: %s by alibrary_artist_id: %s' % (a.pk, alibrary_artist_id))
            except Exception, e:
                # print e
                log.debug('could not get artist by alibrary_artist_id: %s' % alibrary_artist_id)
            
            
        # try to get artist by mb_id
        if mb_artist_id and not a:
            log.debug('artist, lookup by mb_artist_id: %s' % mb_artist_id)
            try:
                las = lookup.artist_by_mb_id(mb_artist_id)
                a = las[0]
                log.debug('got artist: %s by mb_artist_id: %s' % (a.pk, mb_artist_id))
            except Exception, e:
                # print e
                log.debug('could not get artist by mb_artist_id: %s' % mb_artist_id)
                

        # no luck yet, so create the artist
        if not a:
            log.info('no artist yet, so create it: %s' % artist)
            a = Artist(name=artist)
            a.save()
            a_created = True
                 
            
Beispiel #3
0
            log.debug('artist, lookup by alibrary_artist_id: %s' %
                      alibrary_artist_id)
            try:
                a = Artist.objects.get(pk=alibrary_artist_id)
                log.debug('got artist: %s by alibrary_artist_id: %s' %
                          (a.pk, alibrary_artist_id))
            except Exception, e:
                # print e
                log.debug('could not get artist by alibrary_artist_id: %s' %
                          alibrary_artist_id)

        # try to get artist by mb_id
        if mb_artist_id and not a:
            log.debug('artist, lookup by mb_artist_id: %s' % mb_artist_id)
            try:
                las = lookup.artist_by_mb_id(mb_artist_id)
                a = las[0]
                log.debug('got artist: %s by mb_artist_id: %s' %
                          (a.pk, mb_artist_id))
            except Exception, e:
                # print e
                log.debug('could not get artist by mb_artist_id: %s' %
                          mb_artist_id)

        # no luck yet, so create the artist
        if not a:
            log.info('no artist yet, so create it: %s' % artist)
            a = Artist(name=artist)
            a.save()
            a_created = True
Beispiel #4
0
    def mb_complete_media(self, obj, mb_id, excludes=()):

        log = logging.getLogger('util.importer.mb_complete_media')
        log.info('complete media, m: %s | mb_id: %s' % (obj.name, mb_id))

        #raw_input("Press Enter to continue...")
        time.sleep(1.1)

        inc = ('artists', 'url-rels', 'aliases', 'tags', 'recording-rels',
               'artist-rels', 'work-level-rels', 'artist-credits')
        url = 'http://%s/ws/2/recording/%s/?fmt=json&inc=%s' % (
            MUSICBRAINZ_HOST, mb_id, "+".join(inc))

        r = requests.get(url)
        result = r.json()

        print '*****************************************************************'
        print '*****************************************************************'
        print '*****************************************************************'

        self.pp.pprint(result)

        print '*****************************************************************'
        print '*****************************************************************'
        print '*****************************************************************'

        # self.pp.pprint(result)
        if 'relations' in result:
            for relation in result['relations']:

                # map artists
                if 'artist' in relation:
                    print 'artist: %s' % relation['artist']['name']
                    print 'mb_id:   %s' % relation['artist']['id']
                    print 'role:   %s' % relation['type']
                    print
                    time.sleep(0.1)
                    l_as = lookup.artist_by_mb_id(relation['artist']['id'])
                    l_a = None

                    #if len(l_as) < 1 and relation['artist']['id'] not in self.mb_completed:
                    if len(l_as
                           ) < 1 and relation['artist']['id'] not in excludes:
                        self.mb_completed.append(relation['artist']['id'])
                        l_a = Artist(name=relation['artist']['name'])
                        l_a.save()

                        url = 'http://musicbrainz.org/artist/%s' % relation[
                            'artist']['id']
                        print 'musicbrainz_url: %s' % url
                        rel = Relation(content_object=l_a, url=url)
                        rel.save()

                        print 'artist created'
                    if len(l_as) == 1:
                        print 'got artist!'
                        l_a = l_as[0]
                        print l_as[0]

                    profession = None
                    if 'type' in relation:
                        profession, created = Profession.objects.get_or_create(
                            name=relation['type'])
                    """"""
                    if l_a:
                        mea, created = MediaExtraartists.objects.get_or_create(
                            artist=l_a, media=obj, profession=profession)
                        l_a = self.mb_complete_artist(l_a,
                                                      relation['artist']['id'])
                    #self.pp.pprint(relation['artist']['name'])

        tags = result.get('tags', ())
        for tag in tags:
            log.debug('got tag: %s' % (tag['name']))
            Tag.objects.add_tag(obj, '"%s"' % tag['name'])

        # add mb relation
        mb_url = 'http://musicbrainz.org/recording/%s' % (mb_id)
        try:
            rel = Relation.objects.get(object_id=obj.pk, url=mb_url)
        except:
            log.debug('relation not here yet, add it: %s' % (mb_url))
            rel = Relation(content_object=obj, url=mb_url)
            rel.save()

        return obj
Beispiel #5
0
def mb_complete_media_task(obj, mb_id, mb_release_id, excludes=()):

    log = logging.getLogger('util.importer.mb_complete_media')
    log.info('complete media, m: %s | mb_id: %s' % (obj.name, mb_id))

    #raw_input("Press Enter to continue...")
    time.sleep(1.1)

    inc = ('artists', 'url-rels', 'aliases', 'tags', 'recording-rels', 'artist-rels', 'work-level-rels', 'artist-credits')
    url = 'http://%s/ws/2/recording/%s/?fmt=json&inc=%s' % (MUSICBRAINZ_HOST, mb_id, "+".join(inc))

    r = requests.get(url)
    result = r.json()

    print '*****************************************************************'
    print url
    print '*****************************************************************'

    # get release based information (to map track- and disc-number)
    inc = ('recordings',)
    url = 'http://%s/ws/2/release/%s/?fmt=json&inc=%s' % (MUSICBRAINZ_HOST, mb_release_id, "+".join(inc))

    r = requests.get(url)
    result_release = r.json()
    print '*****************************************************************'
    print url
    print '*****************************************************************'

    print(result)

    print

    print(result_release)


    print '*****************************************************************'

    if DEBUG_WAIT:
        raw_input("Press Enter to continue...")



    # loop release recordings, trying to get our track...
    if 'media' in result_release:
        disc_index = 0
        media_index = 0
        media_offset = 0
        for disc in result_release['media']:

            for m in disc['tracks']:

                x_mb_id = m['recording']['id']
                x_pos = m['number']

                if x_mb_id == mb_id:
                    """
                    print 'id:  %s' % x_mb_id
                    print 'pos: %s' % x_pos
                    print 'disc_index: %s' % disc_index
                    print 'media_offset: %s' % media_offset
                    print 'final pos: %s' % (int(media_offset) + int(x_pos))
                    """

                    try:
                        obj.tracknumber = (int(media_offset) + int(x_pos))
                    except:
                        pass

                    try:
                        obj.mediamumber = int(disc_index)
                    except:
                        pass

                media_index =+ 1

            disc_index += 1
            media_offset += int(disc['track-count'])



    if DEBUG_WAIT:
        raw_input("Press Enter to continue...")


    if 'relations' in result:
        for relation in result['relations']:


            # map artists
            if 'artist' in relation:
                print 'artist: %s' % relation['artist']['name']
                print 'mb_id:   %s' % relation['artist']['id']
                print 'role:   %s' % relation['type']
                print
                time.sleep(0.1)
                l_as = lookup.artist_by_mb_id(relation['artist']['id'])
                l_a = None


                if len(l_as) < 1 and relation['artist']['id'] not in excludes:
                    #instance.mb_completed.append(relation['artist']['id'])
                    l_a = Artist(name=relation['artist']['name'])
                    l_a.save()

                    url = 'http://musicbrainz.org/artist/%s' % relation['artist']['id']
                    print 'musicbrainz_url: %s' % url
                    rel = Relation(content_object=l_a, url=url)
                    rel.save()

                    print 'artist created'
                if len(l_as) == 1:
                    print 'got artist!'
                    l_a = l_as[0]
                    print l_as[0]

                profession = None
                if 'type' in relation:
                    profession, created = Profession.objects.get_or_create(name=relation['type'])


                """"""
                if l_a:
                    mea, created = MediaExtraartists.objects.get_or_create(artist=l_a, media=obj, profession=profession)

                    if USE_CELERYD:
                        mb_complete_artist_task.delay(l_a, relation['artist']['id'])
                    else:
                        mb_complete_artist_task(l_a, relation['artist']['id'])


    tags = result.get('tags', ())
    for tag in tags:
        log.debug('got tag: %s' % (tag['name']))
        Tag.objects.add_tag(obj, '"%s"' % tag['name'])

    # add mb relation
    mb_url = 'http://musicbrainz.org/recording/%s' % (mb_id)
    try:
        rel = Relation.objects.get(object_id=obj.pk, url=mb_url)
    except:
        log.debug('relation not here yet, add it: %s' % (mb_url))
        rel = Relation(content_object=obj, url=mb_url)
        rel.save()


    return obj