Пример #1
0
    def update_relations(self):
        """
        relation mappings (urls)
        """

        if not self._data:
            return

        relations = self._data.get('urls')
        if relations:

            _relation_qs = self.obj.relations.all()
            _relation_urls = [strip_http(strip_query_params(r.url)) for r in _relation_qs]

            for url in relations:

                _service = get_service_by_url(url)
                # log.debug('{}: {}'.format(_service, url))

                if _service in DISCOGS_RELATION_USE_SERVICES and not strip_http(strip_query_params(url))in _relation_urls:
                    rel = Relation(
                        content_object=self.obj,
                        url=strip_query_params(url),
                        service=_service
                    )
                    rel.save()

                    if not 'relations' in self._changes:
                        self._changes['relations'] = []

                    self._changes['relations'].append(url)
Пример #2
0
    def update_relations(self):
        """
        relation mappings (urls)
        """

        relations = self._data.get('relations')
        if relations:

            _relation_qs = self.obj.relations.all()
            _relation_urls = [strip_http(r.url) for r in _relation_qs]

            for relation in [r for r in relations if 'url' in r]:
                _url = relation['url']['resource']
                if relation['type'] == 'official homepage':
                    _service = 'official'
                else:
                    _service = get_service_by_url(_url)

                if relation['type'] in MB_RELATION_USE_SERVICES and not strip_http(_url) in _relation_urls:
                    rel = Relation(
                        content_object=self.obj,
                        url=_url,
                        service=_service
                    )
                    rel.save()

                    if not 'relations' in self._changes:
                        self._changes['relations'] = []

                    self._changes['relations'].append(_url)
Пример #3
0
    def get_label(self, uri):

        """
        discogs API labels url schema:
         - url: http://www.discogs.com/label/234260-Rosso-Corsa-Records
         - api: http://api.discgogs.com/labels/234260
         for labels, we neet to take the last bit, split it by '-' and use the first element
        """

        provider_id = uri.split('/')[-1].split('-')[0]
        api_url = 'http://%s/labels/%s' % (DISCOGS_HOST, provider_id)

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)
        data= r.json()

        res = {}
        d_tags = [] # needed as merged from different keys
        relations = []


        for k in data:

            if k == 'name':
                res[k] = self.reformat_name(data[k])

            if k == 'profile':
                mk = 'description'

            if k == 'contact_info':
                mk = 'address'

            if k == 'parent_label':
                res['parent_0'] = self.reformat_name(data[k]['name'])

            if k == 'urls':
                mapped = []
                for rel in data[k]:
                    relations.append({
                        'uri': rel,
                        'service': get_service_by_url(rel, None),
                        })

            if k == 'images':
                res['remote_image'] = res['main_image'] = self.map_image(data[k])



            try:
                if not mk in res:
                    res[mk] = data[k]
            except:
                pass


        res['d_tags'] = ', '.join(d_tags)
        res['relations'] = relations

        return res
Пример #4
0
    def update_relations(self):
        """
        relation mappings (urls)
        """

        relations = self._data.get('relations')
        if relations:

            _relation_qs = self.obj.relations.all()
            _relation_urls = [strip_http(r.url) for r in _relation_qs]

            for relation in [r for r in relations if 'url' in r]:
                _url = relation['url']['resource']
                if relation['type'] == 'official homepage':
                    _service = 'official'
                else:
                    _service = get_service_by_url(_url)

                if relation[
                        'type'] in MB_RELATION_USE_SERVICES and not strip_http(
                            _url) in _relation_urls:
                    rel = Relation(content_object=self.obj,
                                   url=_url,
                                   service=_service)
                    rel.save()

                    if not 'relations' in self._changes:
                        self._changes['relations'] = []

                    self._changes['relations'].append(_url)
Пример #5
0
    def get_artist(self, uri):

        """
        discogs API artists url schema:
         - url: http://www.discogs.com/artist/2693332-Miami-Nights-1984
         - api: http://api.discgogs.com/artists/2693332
         for artists, we neet to take the last bit, split it by '-' and use the first element
        """

        provider_id = uri.split('/')[-1].split('-')[0]
        api_url = 'http://%s/artists/%s' % (DISCOGS_HOST, provider_id)

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)
        data= r.json()

        res = {}
        d_tags = [] # needed as merged from different keys
        relations = []

        for k in data:

            if k == 'profile':
                mk = 'biography'

            if k == 'realname':
                mk = 'real_name'

            if k == 'namevariations':
                res['namevariations'] = ', '.join(data[k])

            if k == 'name':
                # reformat numbering & 'The'
                res[k] = self.reformat_name(data[k])

            if k == 'urls':
                mapped = []
                for rel in data[k]:
                    relations.append({
                        'uri': rel,
                        'service': get_service_by_url(rel, None),
                        })

            if k == 'images':
                res['remote_image'] = res['main_image'] = self.map_image(data[k])

            try:
                if not mk in res:
                    res[mk] = data[k]
            except:
                pass


        res['d_tags'] = ', '.join(d_tags)
        res['relations'] = relations


        return res
Пример #6
0
    def get_artist(self, uri):
        """
        discogs API artists url schema:
         - url: http://www.discogs.com/artist/2693332-Miami-Nights-1984
         - api: http://api.discgogs.com/artists/2693332
         for artists, we neet to take the last bit, split it by '-' and use the first element
        """

        provider_id = uri.split('/')[-1].split('-')[0]
        api_url = 'http://%s/artists/%s' % (DISCOGS_HOST, provider_id)

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)
        data = r.json()

        res = {}
        d_tags = []  # needed as merged from different keys
        relations = []

        for k in data:

            if k == 'profile':
                mk = 'biography'

            if k == 'realname':
                mk = 'real_name'

            if k == 'namevariations':
                res['namevariations'] = ', '.join(data[k])

            if k == 'name':
                # reformat numbering & 'The'
                res[k] = self.reformat_name(data[k])

            if k == 'urls':
                mapped = []
                for rel in data[k]:
                    relations.append({
                        'uri': rel,
                        'service': get_service_by_url(rel, None),
                    })

            if k == 'images':
                res['remote_image'] = res['main_image'] = self.map_image(
                    data[k])

            try:
                if not mk in res:
                    res[mk] = data[k]
            except:
                pass

        res['d_tags'] = ', '.join(d_tags)
        res['relations'] = relations

        return res
Пример #7
0
    def get_label(self, uri):
        """
        discogs API labels url schema:
         - url: http://www.discogs.com/label/234260-Rosso-Corsa-Records
         - api: http://api.discgogs.com/labels/234260
         for labels, we neet to take the last bit, split it by '-' and use the first element
        """

        provider_id = uri.split('/')[-1].split('-')[0]
        api_url = 'http://%s/labels/%s' % (DISCOGS_HOST, provider_id)

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)
        data = r.json()

        res = {}
        d_tags = []  # needed as merged from different keys
        relations = []

        for k in data:

            if k == 'name':
                res[k] = self.reformat_name(data[k])

            if k == 'profile':
                mk = 'description'

            if k == 'contact_info':
                mk = 'address'

            if k == 'parent_label':
                res['parent_0'] = self.reformat_name(data[k]['name'])

            if k == 'urls':
                mapped = []
                for rel in data[k]:
                    relations.append({
                        'uri': rel,
                        'service': get_service_by_url(rel, None),
                    })

            if k == 'images':
                res['remote_image'] = res['main_image'] = self.map_image(
                    data[k])

            try:
                if not mk in res:
                    res[mk] = data[k]
            except:
                pass

        res['d_tags'] = ', '.join(d_tags)
        res['relations'] = relations

        return res
Пример #8
0
    def save(self, *args, **kwargs):

        self.service = get_service_by_url(self.url, self.service)

        # find already assigned services and delete them
        if self.service != 'generic':
            # TODO: fix unique problem
            reld = Relation.objects.filter(service=self.service, content_type=self.content_type, object_id=self.object_id).delete()
        
        super(Relation, self).save(*args, **kwargs)    
Пример #9
0
    def get_label(self, uri):

        """
        discogs API labels url schema:
         - url: http://www.discogs.com/label/234260-Rosso-Corsa-Records
         - api: http://api.discgogs.com/labels/234260
         for labels, we neet to take the last bit, split it by '-' and use the first element
        """

        provider_id = uri.split("/")[-1].split("-")[0]
        api_url = "http://%s/labels/%s" % (DISCOGS_HOST, provider_id)

        log.info("composed api url: %s" % api_url)

        r = requests.get(api_url)
        data = r.json()

        res = {}
        d_tags = []  # needed as merged from different keys
        relations = []

        for k in data:

            if k == "name":
                # reformat numbering & 'The'
                res[k] = self.reformat_name(data[k])

            if k == "profile":
                mk = "description"

            if k == "contact_info":
                mk = "address"

            if k == "parent_label":
                res["parent_0"] = data[k]["name"]

            if k == "urls":
                mapped = []
                for rel in data[k]:
                    relations.append({"uri": rel, "service": get_service_by_url(rel, None)})

            if k == "images":
                res["remote_image"] = res["main_image"] = self.map_image(data[k])

            try:
                if not mk in res:
                    res[mk] = data[k]
            except:
                pass

        res["d_tags"] = ", ".join(d_tags)
        res["relations"] = relations

        return res
Пример #10
0
    def get_artist(self, uri):

        """
        discogs API artists url schema:
         - url: http://www.discogs.com/artist/2693332-Miami-Nights-1984
         - api: http://api.discgogs.com/artists/2693332
         for artists, we neet to take the last bit, split it by '-' and use the first element
        """

        provider_id = uri.split("/")[-1].split("-")[0]
        api_url = "http://%s/artists/%s" % (DISCOGS_HOST, provider_id)

        log.info("composed api url: %s" % api_url)

        r = requests.get(api_url)
        data = r.json()

        res = {}
        d_tags = []  # needed as merged from different keys
        relations = []

        for k in data:

            if k == "profile":
                mk = "biography"

            if k == "realname":
                mk = "real_name"

            if k == "namevariations":
                res["namevariations"] = ", ".join(data[k])

            if k == "name":
                # reformat numbering & 'The'
                res[k] = self.reformat_name(data[k])

            if k == "urls":
                mapped = []
                for rel in data[k]:
                    relations.append({"uri": rel, "service": get_service_by_url(rel, None)})

            if k == "images":
                res["remote_image"] = res["main_image"] = self.map_image(data[k])

            try:
                if not mk in res:
                    res[mk] = data[k]
            except:
                pass

        res["d_tags"] = ", ".join(d_tags)
        res["relations"] = relations

        return res
Пример #11
0
    def save(self, *args, **kwargs):

        self.service = get_service_by_url(self.url, self.service)

        # find already assigned services and delete them
        if self.service != 'generic':
            #if self.service in Relation.UNIQUE_SERVICES:
            # TODO: fix unique problem
            Relation.objects.filter(service=self.service,
                                    content_type=self.content_type,
                                    object_id=self.object_id).delete()

        super(Relation, self).save(*args, **kwargs)
Пример #12
0
    def get_artist(self, uri):

        provider_id = uri.split("/")[-1]
        inc = ("aliases", "url-rels", "annotation", "tags", "artist-rels")
        api_url = "http://%s/ws/2/artist/%s/?fmt=json&inc=%s" % (MUSICBRAINZ_HOST, provider_id, "+".join(inc))

        log.info("composed api url: %s" % api_url)

        r = requests.get(api_url)
        data = r.json()

        # data mapping
        res = {}
        d_tags = []
        for k in data:
            mk = k
            if k == "annotation":
                mk = "description"

            if k == "life-span":
                if "begin" in data[k]:
                    res["date_start"] = data[k]["begin"]

                if "end" in data[k]:
                    res["date_end"] = data[k]["end"]

            if k == "relations":
                mapped = []
                for rel in data[k]:
                    if "url" in rel:
                        mapped.append(
                            {"uri": rel["url"]["resource"], "service": get_service_by_url(rel["url"]["resource"], None)}
                        )
                data[k] = mapped

            if k == "tags":
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v["name"])
                except:
                    pass

            res[mk] = data[k]

        res["d_tags"] = ", ".join(d_tags)

        # disable tags on mb
        res["d_tags"] = res["tags"] = ""

        return res
Пример #13
0
    def get_artist(self, uri):

        provider_id = uri.split('/')[-1]
        inc = ('aliases', 'url-rels', 'annotation', 'tags', 'artist-rels')
        api_url = 'http://%s/ws/2/artist/%s/?fmt=json&inc=%s' % (MUSICBRAINZ_HOST, provider_id, "+".join(inc))

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)

        if r.status_code == 404:
            return {'error': MUSICBRAINZ_404_MESSAGE}

        data = r.json()

        res = {}
        d_tags = []
        for k in data:
            mk = k
            if k == 'annotation':
                mk = 'description'

            if k == 'life-span':
                if 'begin' in data[k]:
                    res['date_start'] = data[k]['begin']

                if 'end' in data[k]:
                    res['date_end'] = data[k]['end']

            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'uri': rel['url']['resource'],
                            'service': get_service_by_url(rel['url']['resource'], None),
                            })
                data[k] = mapped

            if k == 'ipis':
                try:
                    d = data[k]
                    res['ipi_code'] = d[0]
                except:
                    pass

            if k == 'isni':
                try:
                    d = data[k]
                    res['isni_code'] = d
                except:
                    pass

            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        res['d_tags'] = ', '.join(d_tags)

        # disable tags on mb
        res['d_tags'] = res['tags'] = ''


        # temporary hack to get isni
        # http://tickets.musicbrainz.org/browse/MBS-8762
        api_url = 'http://%s/ws/2/artist/%s/?fmt=xml&inc=%s' % (MUSICBRAINZ_HOST, provider_id, "+".join(inc))
        log.info('composed api url: %s' % api_url)
        r = requests.get(api_url)
        if r.status_code == 200:
            import xmltodict
            import json
            try:
                isni_code = json.loads(json.dumps(xmltodict.parse(r.text)))['metadata']['artist']['isni-list']['isni']
                if isinstance(isni_code, types.ListType):
                    res['isni_code'] = isni_code[0]
                else:
                    res['isni_code'] = isni_code
            except:
                pass


        return res
Пример #14
0
    def get_release(self, uri):
        """
        discogs API releases url schema:
         - url: http://www.discogs.com/Miami-Nights-1984-Early-Summer/release/5015407
         - api: http://api.discgogs.com/releases/5015407
         for releases it is fine to just take the last bit
        """

        provider_id = uri.split('/')[-1]

        if '/release/' in uri:
            api_url = 'http://%s/releases/%s' % (DISCOGS_HOST, provider_id)

        if '/master/' in uri:
            api_url = 'http://%s/masters/%s' % (DISCOGS_HOST, provider_id)

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)
        data = r.json()

        res = {}
        d_tags = []
        relations = []

        for k in data:

            mk = k

            #if k == 'title':
            #    mk = 'name'

            if k == 'title':
                # reformat numbering & 'The' and strip blank characters
                res['name'] = self.reformat_name(data[k])

            if k == 'artists':
                reformated_artists = []
                for item in data['artists']:
                    if 'name' in item:
                        item['name'] = self.reformat_name(item['name'])
                        reformated_artists.append(item)
                res['artists'] = reformated_artists

            if k == 'notes':
                mk = 'description'

            if k == 'released_formatted':
                mk = 'releasedate_approx'

            if k == 'formats':
                try:
                    d = data[k]
                    if len(d[0]['descriptions']) > 1:
                        res['releasetype'] = d[0]['descriptions'][1]
                    else:
                        res['releasetype'] = d[0]['descriptions'][0]

                except:
                    pass

            if k == 'country':
                mk = 'release_country'

            if k == 'labels':
                try:
                    d = data[k][0]
                    res['label_0'] = self.reformat_name(d['name'])
                    res['catalognumber'] = d['catno']
                except:
                    pass

            # tagging
            if k == 'styles':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v)
                except:
                    pass

            if k == 'genres':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v)
                except:
                    pass

            if k == 'urls':
                mapped = []
                for rel in data[k]:
                    relations.append({
                        'uri': rel,
                        'service': get_service_by_url(rel, None),
                    })

            # image
            if k == 'images':
                res['remote_image'] = res['main_image'] = self.map_image(
                    data[k])

            res[mk] = data[k]

        try:
            res['totaltracks'] = len(data['tracklist'])
        except:
            pass

        res['d_tags'] = ', '.join(d_tags)
        res['relations'] = relations

        # remap borgious countries
        try:
            if res['release_country'].upper() == 'UK':
                res['release_country'] = 'GB'
        except:
            pass

        # reformat tracklist
        res['tracklist'] = []
        for track in data['tracklist']:

            if 'artists' in track:
                try:
                    track['artists'][0]['name'] = self.reformat_name(
                        track['artists'][0]['name'])
                except:
                    pass
            """
            TODO: this is an ugly & temporary hack to address discogs API issue:
            https://lab.hazelfire.com/issues/1622
            """
            try:
                track['title'] = track['title'].replace('\x92', "'").strip()
            except Exception as e:
                pass

            res['tracklist'].append(track)

        return res
Пример #15
0
    def get_release(self, uri):

        provider_id = uri.split("/")[-1]
        inc = (
            "aliases",
            "url-rels",
            "annotation",
            "tags",
            "artist-rels",
            "recordings",
            "artists",
            "labels",
            "release-groups",
        )
        api_url = "http://%s/ws/2/release/%s/?fmt=json&inc=%s" % (MUSICBRAINZ_HOST, provider_id, "+".join(inc))

        log.info("composed api url: %s" % api_url)

        r = requests.get(api_url)
        data = r.json()

        # data mapping
        res = {}
        d_tags = []
        for k in data:
            mk = k

            if k == "annotation":
                mk = "description"

            if k == "title":
                mk = "name"

            if k == "country":
                mk = "release_country"

            if k == "date":
                mk = "releasedate_approx"

            if k == "label-info":
                try:
                    if "label" in data[k][0] and "name" in data[k][0]["label"]:
                        res["label_0"] = data[k][0]["label"]["name"]

                    if "catalog-number" in data[k][0]:
                        res["catalognumber"] = data[k][0]["catalog-number"]

                except:
                    pass

            if k == "release-group":
                try:
                    res["releasetype"] = data[k]["primary-type"]

                except:
                    pass

            if k == "relations":
                mapped = []
                for rel in data[k]:
                    if "url" in rel:
                        mapped.append(
                            {"uri": rel["url"]["resource"], "service": get_service_by_url(rel["url"]["resource"], None)}
                        )
                data[k] = mapped

            if k == "media":
                mapped = []
                pos = 1
                disc = 1
                for disc in data[k]:
                    for m in disc["tracks"]:
                        mapped.append({"position": "%s-%s" % (disc, pos), "duration": m["length"], "title": m["title"]})

                # res['tracklist'] = mapped

            if k == "tags":
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v["name"])
                except:
                    pass

            res[mk] = data[k]

        # try to remap country
        if "release_country" in res:
            try:
                c = Country.objects.get(iso2_code=res["release_country"])
            except:
                pass

        try:
            url = "http://coverartarchive.org/release/%s" % id
            r = requests.get(url)
            if r.ok:
                res["main_image"] = r.json()["images"][0]["image"]
                res["remote_image"] = r.json()["images"][0]["image"]
        except:
            pass

        try:
            num_tracks = 0
            for x in data["media"]:
                num_tracks += int(x["track-count"])
            res["totaltracks"] = num_tracks if num_tracks > 0 else None
        except:
            pass

        res["d_tags"] = ", ".join(d_tags)

        # disable tags on mb
        res["d_tags"] = res["tags"] = ""

        return res
Пример #16
0
    def get_media(self, uri):

        provider_id = uri.split('/')[-1]

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

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)

        if r.status_code == 404:
            return {'error': MUSICBRAINZ_404_MESSAGE}

        data = r.json()

        res = {}
        d_tags = []
        for k in data:
            mk = k
            if k == 'title':
                mk = 'name'

            if k == 'annotation':
                mk = 'description'

            # we just take the first isrc code..
            if k == 'isrcs' and len(data['isrcs']) > 0:
                res['isrc'] = data['isrcs'][0]

            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'uri':
                            rel['url']['resource'],
                            'service':
                            get_service_by_url(rel['url']['resource'], None),
                        })
                data[k] = mapped

            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        res['d_tags'] = ', '.join(d_tags)

        # TODO: implement artist mapping
        # res['artist_0'] = 'peter'

        # disable tags on mb
        res['d_tags'] = res['tags'] = ''

        return res
Пример #17
0
    def get_release(self, uri):

        provider_id = uri.split('/')[-1]
        inc = ('aliases', 'url-rels', 'annotation', 'tags', 'artist-rels',
               'recordings', 'artists', 'labels', 'release-groups',
               'artist-credits', 'isrcs')
        api_url = 'http://%s/ws/2/release/%s/?fmt=json&inc=%s' % (
            MUSICBRAINZ_HOST, provider_id, "+".join(inc))

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)

        if r.status_code == 404:
            return {'error': MUSICBRAINZ_404_MESSAGE}

        data = r.json()

        res = {}
        d_tags = []
        mapped_media = None
        for k in data:
            mk = k

            if k == 'annotation':
                mk = 'description'

            if k == 'title':
                mk = 'name'

            if k == 'country':
                mk = 'release_country'

            if k == 'date':
                mk = 'releasedate_approx'

            if k == 'label-info':
                try:
                    if 'label' in data[k][0] and 'name' in data[k][0]['label']:
                        res['label_0'] = data[k][0]['label']['name']

                    if 'catalog-number' in data[k][0]:
                        res['catalognumber'] = data[k][0]['catalog-number']

                except:
                    pass

            if k == 'release-group':
                try:
                    res['releasetype'] = data[k]['primary-type']

                except:
                    pass

            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'uri':
                            rel['url']['resource'],
                            'service':
                            get_service_by_url(rel['url']['resource'], None),
                        })
                data[k] = mapped

            if k == 'media':
                mapped_media = []
                pos = 0
                disc_no = 0

                for disc in data[k]:
                    disc_no += 1
                    for m in disc['tracks']:
                        pos += 1

                        if 'artist-credit' in m:
                            track_artists = m['artist-credit']
                        else:
                            track_artists = []

                        if 'recording' in m and 'isrcs' in m['recording']:
                            isrcs = m['recording']['isrcs']
                        else:
                            isrcs = []

                        mapped_media.append({
                            'number':
                            '%s' % (pos),
                            'position':
                            '%s-%s' % (disc_no, pos),
                            'duration':
                            m['length'],
                            'title':
                            m['title'],
                            'artists':
                            track_artists,
                            'isrcs':
                            isrcs,
                        })

            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        # try to remap country
        if 'release_country' in res:
            try:
                c = Country.objects.get(iso2_code=res['release_country'])
            except:
                pass

        try:
            url = 'http://coverartarchive.org/release/%s' % id
            r = requests.get(url)
            if r.ok:
                res['main_image'] = r.json()['images'][0]['image']
                res['remote_image'] = r.json()['images'][0]['image']
        except:
            pass

        try:
            num_tracks = 0
            for x in data['media']:
                num_tracks += int(x['track-count'])
            res['totaltracks'] = num_tracks if num_tracks > 0 else None
        except:
            pass

        res['d_tags'] = ', '.join(d_tags)

        # disable tags on mb
        res['d_tags'] = res['tags'] = ''

        res['tracklist'] = mapped_media

        return res
Пример #18
0
    def get_artist(self, uri):

        provider_id = uri.split('/')[-1]
        inc = ('aliases', 'url-rels', 'annotation', 'tags', 'artist-rels')
        api_url = 'http://%s/ws/2/artist/%s/?fmt=json&inc=%s' % (
            MUSICBRAINZ_HOST, provider_id, "+".join(inc))

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)

        if r.status_code == 404:
            return {'error': MUSICBRAINZ_404_MESSAGE}

        data = r.json()

        res = {}
        d_tags = []
        for k in data:
            mk = k
            if k == 'annotation':
                mk = 'description'

            if k == 'life-span':
                if 'begin' in data[k]:
                    res['date_start'] = data[k]['begin']

                if 'end' in data[k]:
                    res['date_end'] = data[k]['end']

            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'uri':
                            rel['url']['resource'],
                            'service':
                            get_service_by_url(rel['url']['resource'], None),
                        })
                data[k] = mapped

            if k == 'ipis':
                try:
                    d = data[k]
                    res['ipi_code'] = d[0]
                except:
                    pass

            if k == 'isni':
                try:
                    d = data[k]
                    res['isni_code'] = d
                except:
                    pass

            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        res['d_tags'] = ', '.join(d_tags)

        # disable tags on mb
        res['d_tags'] = res['tags'] = ''

        # temporary hack to get isni
        # http://tickets.musicbrainz.org/browse/MBS-8762
        api_url = 'http://%s/ws/2/artist/%s/?fmt=xml&inc=%s' % (
            MUSICBRAINZ_HOST, provider_id, "+".join(inc))
        log.info('composed api url: %s' % api_url)
        r = requests.get(api_url)
        if r.status_code == 200:
            import xmltodict
            import json
            try:
                isni_code = json.loads(json.dumps(xmltodict.parse(
                    r.text)))['metadata']['artist']['isni-list']['isni']
                if isinstance(isni_code, types.ListType):
                    res['isni_code'] = isni_code[0]
                else:
                    res['isni_code'] = isni_code
            except:
                pass

        return res
Пример #19
0
    def get_label(self, uri):

        provider_id = uri.split('/')[-1]
        inc = (
            'aliases',
            'url-rels',
            'annotation',
            'tags',
        )
        api_url = 'http://%s/ws/2/label/%s/?fmt=json&inc=%s' % (
            MUSICBRAINZ_HOST, provider_id, "+".join(inc))

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)

        if r.status_code == 404:
            return {'error': MUSICBRAINZ_404_MESSAGE}

        data = r.json()

        res = {}
        d_tags = []
        for k in data:

            mk = k
            if k == 'label-code':
                mk = 'labelcode'

            if k == 'annotation':
                mk = 'description'

            # wrong type-map
            if k == 'type':
                mk = '__unused__'

            if k == 'life-span':
                if 'begin' in data[k]:
                    res['date_start'] = data[k]['begin']

                if 'end' in data[k]:
                    res['date_end'] = data[k]['end']

            if k == 'parent_label':
                res['parent_0'] = data[k]['name']

            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'uri':
                            rel['url']['resource'],
                            'service':
                            get_service_by_url(rel['url']['resource'], None),
                        })
                data[k] = mapped

            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        if 'country' in res:
            try:
                c = Country.objects.get(iso2_code=res['country'])
            except:
                pass

        res['d_tags'] = ', '.join(d_tags)

        # disable tags on mb
        res['d_tags'] = res['tags'] = ''

        return res
Пример #20
0
    def get_release_from_musicbrainz(self):

        log.info('uri: %s' % self.uri)


        # TODO: make more dynamic...
        # some hacks to convert site url to api id
        id = self.uri.split('/')[-1]
        url = "http://%s/ws/2/release/%s?fmt=json&inc=aliases+url-rels+annotation+tags+artist-rels+recordings+artists+labels" % (MUSICBRAINZ_HOST, id)

        print "#########################################"
        print url
        r = requests.get(url)
        data= r.json()

        print data

        res = {}
        d_tags = [] # needed as merged from different keys

        for k in data:
            print k

            # kind of ugly data mapping
            mk = k

            if k == 'annotation':
                mk = 'description'

            if k == 'title':
                mk = 'name'

            if k == 'country':
                mk = 'release_country'

            if k == 'date':
                mk = 'releasedate_approx'

            # wrong type-map
            """
            if k == 'type':
                mk = '__unused__'
            """

            if k == 'label-info':
                try:
                    print '*******************************************'
                    print data[k][0]

                    if 'label' in data[k][0] and 'name' in data[k][0]['label']:
                        res['label_0'] = data[k][0]['label']['name']

                    if 'catalog-number' in data[k][0]:
                        res['catalognumber'] = data[k][0]['catalog-number']

                except:
                    pass


            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        #print rel['url']
                        #print get_service_by_url(rel['url'], None)
                        mapped.append({
                            'url': rel['url'],
                            'service': get_service_by_url(rel['url'], None),
                            })

                data[k] = mapped





            if k == 'media':
                mapped = []
                # initials
                pos = 1
                disc = 1
                for disc in data[k]:
                    for m in disc['tracks']:
                        mapped.append({
                            'position': '%s-%s' % (disc, pos),
                            'duration': m['length'],
                            'title': m['title'],
                            })

                #res['tracklist'] = mapped




            # tagging
            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        # try to remap country
        if 'release_country' in res:
            try:
                c = Country.objects.get(iso2_code=res['release_country'])
                #res['release_country'] = c.pk
            except:
                pass

        print 'DTAGS:'
        print d_tags

        res['d_tags'] = ', '.join(d_tags)

        self.api_data = res
        self.save()

        return res
Пример #21
0
    def get_release(self, uri):

        """
        discogs API releases url schema:
         - url: http://www.discogs.com/Miami-Nights-1984-Early-Summer/release/5015407
         - api: http://api.discgogs.com/releases/5015407
         for releases it is fine to just take the last bit
        """

        provider_id = uri.split("/")[-1]

        if "/release/" in uri:
            api_url = "http://%s/releases/%s" % (DISCOGS_HOST, provider_id)

        if "/master/" in uri:
            api_url = "http://%s/masters/%s" % (DISCOGS_HOST, provider_id)

        log.info("composed api url: %s" % api_url)

        r = requests.get(api_url)
        data = r.json()

        res = {}
        d_tags = []
        relations = []

        for k in data:

            mk = k

            # if k == 'title':
            #    mk = 'name'

            if k == "title":
                # reformat numbering & 'The'
                res["name"] = self.reformat_name(data[k])

            if k == "notes":
                mk = "description"

            if k == "released_formatted":
                mk = "releasedate_approx"

            if k == "formats":
                try:
                    d = data[k]
                    if len(d[0]["descriptions"]) > 1:
                        res["releasetype"] = d[0]["descriptions"][1]
                    else:
                        res["releasetype"] = d[0]["descriptions"][0]

                except:
                    pass

            if k == "country":
                mk = "release_country"

            if k == "labels":
                try:
                    d = data[k][0]
                    res["label_0"] = self.reformat_name(d["name"])
                    res["catalognumber"] = d["catno"]
                except:
                    pass

            # tagging
            if k == "styles":
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v)
                except:
                    pass

            if k == "genres":
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v)
                except:
                    pass

            if k == "urls":
                mapped = []
                for rel in data[k]:
                    relations.append({"uri": rel, "service": get_service_by_url(rel, None)})

            # image
            if k == "images":
                res["remote_image"] = res["main_image"] = self.map_image(data[k])

            res[mk] = data[k]

        try:
            res["totaltracks"] = len(data["tracklist"])
        except:
            pass

        res["d_tags"] = ", ".join(d_tags)
        res["relations"] = relations

        # remap borgious countries
        try:
            if res["release_country"].upper() == "UK":
                res["release_country"] = "GB"
        except:
            pass

        # reformat tracklist
        res["tracklist"] = []
        for track in data["tracklist"]:
            if "artists" in track:
                try:
                    track["artists"][0]["name"] = self.reformat_name(track["artists"][0]["name"])
                except:
                    pass
            res["tracklist"].append(track)

        return res
Пример #22
0
    def get_release(self, uri):

        provider_id = uri.split('/')[-1]
        inc = ('aliases', 'url-rels', 'annotation', 'tags', 'artist-rels', 'recordings', 'artists', 'labels', 'release-groups', 'artist-credits', 'isrcs')
        api_url = 'http://%s/ws/2/release/%s/?fmt=json&inc=%s' % (MUSICBRAINZ_HOST, provider_id, "+".join(inc))

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)

        if r.status_code == 404:
            return {'error': MUSICBRAINZ_404_MESSAGE}

        data= r.json()

        res = {}
        d_tags = []
        mapped_media = None
        for k in data:
            mk = k

            if k == 'annotation':
                mk = 'description'

            if k == 'title':
                mk = 'name'

            if k == 'country':
                mk = 'release_country'

            if k == 'date':
                mk = 'releasedate_approx'

            if k == 'label-info':
                try:
                    if 'label' in data[k][0] and 'name' in data[k][0]['label']:
                        res['label_0'] = data[k][0]['label']['name']

                    if 'catalog-number' in data[k][0]:
                        res['catalognumber'] = data[k][0]['catalog-number']

                except:
                    pass

            if k == 'release-group':
                try:
                    res['releasetype'] = data[k]['primary-type']

                except:
                    pass


            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'uri': rel['url']['resource'],
                            'service': get_service_by_url(rel['url']['resource'], None),
                            })
                data[k] = mapped

            if k == 'media':
                mapped_media = []
                pos = 0
                disc_no = 0

                for disc in data[k]:
                    disc_no += 1
                    for m in disc['tracks']:
                        pos += 1

                        if 'artist-credit' in m:
                            track_artists = m['artist-credit']
                        else:
                            track_artists = []

                        if 'recording' in m and 'isrcs' in m['recording']:
                            isrcs = m['recording']['isrcs']
                        else:
                            isrcs = []

                        mapped_media.append({
                            'number': '%s' % (pos),
                            'position': '%s-%s' % (disc_no, pos),
                            'duration': m['length'],
                            'title': m['title'],
                            'artists': track_artists,
                            'isrcs': isrcs,
                            })



            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass


            res[mk] = data[k]

        # try to remap country
        if 'release_country' in res:
            try:
                c = Country.objects.get(iso2_code=res['release_country'])
            except:
                pass

        try:
            url = 'http://coverartarchive.org/release/%s' % id
            r = requests.get(url)
            if r.ok:
                res['main_image'] = r.json()['images'][0]['image']
                res['remote_image'] = r.json()['images'][0]['image']
        except:
            pass



        try:
            num_tracks = 0
            for x in data['media']:
                num_tracks += int(x['track-count'])
            res['totaltracks'] = num_tracks if num_tracks > 0 else None
        except:
            pass

        res['d_tags'] = ', '.join(d_tags)

        # disable tags on mb
        res['d_tags'] = res['tags'] = ''


        res['tracklist'] = mapped_media

        return res
Пример #23
0
    def get_release(self, uri):

        """
        discogs API releases url schema:
         - url: http://www.discogs.com/Miami-Nights-1984-Early-Summer/release/5015407
         - api: http://api.discgogs.com/releases/5015407
         for releases it is fine to just take the last bit
        """

        provider_id = uri.split('/')[-1]

        if '/release/' in uri:
            api_url = 'http://%s/releases/%s' % (DISCOGS_HOST, provider_id)

        if '/master/' in uri:
            api_url = 'http://%s/masters/%s' % (DISCOGS_HOST, provider_id)


        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)
        data= r.json()

        res = {}
        d_tags = []
        relations = []

        for k in data:

            mk = k

            #if k == 'title':
            #    mk = 'name'

            if k == 'title':
                # reformat numbering & 'The' and strip blank characters
                res['name'] = self.reformat_name(data[k])

            if k == 'artists':
                reformated_artists = []
                for item in data['artists']:
                    if 'name' in item:
                        item['name'] = self.reformat_name(item['name'])
                        reformated_artists.append(item)
                res['artists'] = reformated_artists


            if k == 'notes':
                mk = 'description'

            if k == 'released_formatted':
                mk = 'releasedate_approx'

            if k == 'formats':
                try:
                    d = data[k]
                    if len(d[0]['descriptions']) > 1:
                        res['releasetype'] = d[0]['descriptions'][1]
                    else:
                        res['releasetype'] = d[0]['descriptions'][0]

                except:
                    pass


            if k == 'country':
                mk = 'release_country'

            if k == 'labels':
                try:
                    d = data[k][0]
                    res['label_0'] = self.reformat_name(d['name'])
                    res['catalognumber'] = d['catno']
                except:
                    pass


            # tagging
            if k == 'styles':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v)
                except:
                    pass

            if k == 'genres':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v)
                except:
                    pass


            if k == 'urls':
                mapped = []
                for rel in data[k]:
                    relations.append({
                        'uri': rel,
                        'service': get_service_by_url(rel, None),
                        })

            # image
            if k == 'images':
                res['remote_image'] = res['main_image'] = self.map_image(data[k])

            res[mk] = data[k]

        try:
            res['totaltracks'] = len(data['tracklist'])
        except:
            pass

        res['d_tags'] = ', '.join(d_tags)
        res['relations'] = relations

        # remap borgious countries
        try:
            if res['release_country'].upper() == 'UK':
                res['release_country'] = 'GB'
        except:
            pass


        # reformat tracklist
        res['tracklist'] = []
        for track in data['tracklist']:

            if 'artists' in track:
                try:
                    track['artists'][0]['name'] = self.reformat_name(track['artists'][0]['name'])
                except:
                    pass
            """
            TODO: this is an ugly & temporary hack to address discogs API issue:
            https://lab.hazelfire.com/issues/1622
            """
            try:
                track['title'] = track['title'].replace('\x92', "'").strip()
            except Exception as e:
                pass

            res['tracklist'].append(track)



        return res
Пример #24
0
    def get_media(self, uri):

        provider_id = uri.split('/')[-1]

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

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)

        if r.status_code == 404:
            return {'error': MUSICBRAINZ_404_MESSAGE}

        data= r.json()

        res = {}
        d_tags = []
        for k in data:
            mk = k
            if k == 'title':
                mk = 'name'

            if k == 'annotation':
                mk = 'description'

            # we just take the first isrc code..
            if k == 'isrcs' and len(data['isrcs']) > 0:
                res['isrc'] = data['isrcs'][0]

            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'uri': rel['url']['resource'],
                            'service': get_service_by_url(rel['url']['resource'], None),
                            })
                data[k] = mapped

            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        res['d_tags'] = ', '.join(d_tags)

        # TODO: implement artist mapping
        # res['artist_0'] = 'peter'

        # disable tags on mb
        res['d_tags'] = res['tags'] = ''


        return res
Пример #25
0
    def get_label(self, uri):

        provider_id = uri.split('/')[-1]
        inc = ('aliases', 'url-rels', 'annotation', 'tags',)
        api_url = 'http://%s/ws/2/label/%s/?fmt=json&inc=%s' % (MUSICBRAINZ_HOST, provider_id, "+".join(inc))

        log.info('composed api url: %s' % api_url)

        r = requests.get(api_url)

        if r.status_code == 404:
            return {'error': MUSICBRAINZ_404_MESSAGE}

        data= r.json()

        res = {}
        d_tags = []
        for k in data:

            mk = k
            if k == 'label-code':
                mk = 'labelcode'

            if k == 'annotation':
                mk = 'description'

            # wrong type-map
            if k == 'type':
                mk = '__unused__'

            if k == 'life-span':
                if 'begin' in data[k]:
                    res['date_start'] = data[k]['begin']

                if 'end' in data[k]:
                    res['date_end'] = data[k]['end']

            if k == 'parent_label':
                res['parent_0'] = data[k]['name']


            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'uri': rel['url']['resource'],
                            'service': get_service_by_url(rel['url']['resource'], None),
                            })
                data[k] = mapped

            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        if 'country' in res:
            try:
                c = Country.objects.get(iso2_code=res['country'])
            except:
                pass

        res['d_tags'] = ', '.join(d_tags)

        # disable tags on mb
        res['d_tags'] = res['tags'] = ''

        return res
Пример #26
0
    def get_media(self, uri):

        provider_id = uri.split("/")[-1]

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

        log.info("composed api url: %s" % api_url)

        r = requests.get(api_url)
        data = r.json()

        # data mapping
        res = {}
        d_tags = []
        for k in data:
            mk = k
            if k == "title":
                mk = "name"

            if k == "annotation":
                mk = "description"

            # we just take the first isrc code..
            if k == "isrcs" and len(data["isrcs"]) > 0:
                res["isrc"] = data["isrcs"][0]

            if k == "relations":
                mapped = []
                for rel in data[k]:
                    if "url" in rel:
                        mapped.append(
                            {"uri": rel["url"]["resource"], "service": get_service_by_url(rel["url"]["resource"], None)}
                        )
                data[k] = mapped

            if k == "tags":
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v["name"])
                except:
                    pass

            res[mk] = data[k]

        res["d_tags"] = ", ".join(d_tags)

        # TODO: implement artist mapping
        # res['artist_0'] = 'peter'

        # disable tags on mb
        res["d_tags"] = res["tags"] = ""

        return res
Пример #27
0
    def get_media_from_musicbrainz(self):

        log.info('uri: %s' % self.uri)


        # TODO: make more dynamic...
        # some hacks to convert site url to api id
        id = self.uri.split('/')[-1]
        #url = "http://%s/ws/2/recording/%s?fmt=json&inc=aliases+url-rels+annotation+tags+artists+isrcs+artist-credits+work-rels+work-level-rels" % (MUSICBRAINZ_HOST, id)
        url = "http://%s/ws/2/recording/%s?fmt=json&inc=aliases+url-rels+annotation+tags+artists+isrcs+artist-credits+artist-rels+work-rels" % (MUSICBRAINZ_HOST, id)

        print "#########################################"
        print url
        r = requests.get(url)
        data= r.json()

        print data

        res = {}
        d_tags = [] # needed as merged from different keys

        for k in data:
            print k

            # kind of ugly data mapping
            mk = k

            if k == 'title':
                mk = 'name'

            if k == 'annotation':
                mk = 'description'

            # wrong type-map
            """
            if k == 'type':
                mk = '__unused__'
            """


            # we just take the first isrc code..
            if k == 'isrcs' and len(data['isrcs']) > 0:
                res['isrc'] = data['isrcs'][0]



            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        #print rel['url']
                        #print get_service_by_url(rel['url'], None)
                        mapped.append({
                            'url': rel['url'],
                            'service': get_service_by_url(rel['url'], None),
                            })

                data[k] = mapped

            # tagging
            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        # try to remap country
        if 'country' in res:
            try:
                c = Country.objects.get(iso2_code=res['country'])
                res['country'] = c.pk
            except:
                pass

        print 'DTAGS:'
        print d_tags

        res['d_tags'] = ', '.join(d_tags)

        self.api_data = res
        self.save()

        return res
Пример #28
0
    def get_label_from_musicbrainz(self):

        log.info('uri: %s' % self.uri)


        # TODO: make more dynamic...
        # some hacks to convert site url to api id
        id = self.uri.split('/')[-1]
        url = "http://%s/ws/2/label/%s?fmt=json&inc=aliases+url-rels+annotation+tags+label-rels" % (MUSICBRAINZ_HOST, id)

        print "#########################################"
        print url
        r = requests.get(url)
        data= r.json()

        print data

        res = {}
        d_tags = [] # needed as merged from different keys

        for k in data:
            print k
            #print d_artist.data[k]

            # kind of ugly data mapping
            mk = k
            if k == 'label-code':
                mk = 'labelcode'

            if k == 'annotation':
                mk = 'description'

            # wrong type-map
            if k == 'type':
                mk = '__unused__'


            if k == 'life-span':
                if 'begin' in data[k]:
                    res['date_start'] = data[k]['begin']

                if 'end' in data[k]:
                    res['date_end'] = data[k]['end']


            if k == 'parent_label':
                res['parent_0'] = data[k]['name']



            if k == 'relations':
                mapped = []
                for rel in data[k]:
                    if 'url' in rel:
                        mapped.append({
                            'url': rel['url'],
                            'service': get_service_by_url(rel['url'], None),
                            })

                data[k] = mapped

            # tagging
            if k == 'tags':
                try:
                    d = data[k]
                    for v in d:
                        d_tags.append(v['name'])
                except:
                    pass

            res[mk] = data[k]

        # try to remap country
        if 'country' in res:
            try:
                c = Country.objects.get(iso2_code=res['country'])
                res['country'] = c.pk
            except:
                pass

        print 'DTAGS:'
        print d_tags

        res['d_tags'] = ', '.join(d_tags)

        self.api_data = res
        self.save()

        return res