def import_instrument_description(instrument, overwrite=False): iname = wikipedia.search(instrument.name) img, imgurl, b, u = wikipedia.get_artist_details(instrument.name) if b and b.startswith("#REDIR"): newname = b.replace("#REDIRECT ", "") img, imgurl, b, u = wikipedia.get_artist_details(newname) if b: sn = data.models.SourceName.objects.get(name="Wikipedia") source, created = data.models.Source.objects.get_or_create(source_name=sn, uri=u, defaults={"title": instrument.name}) description = data.models.Description.objects.create(description=b, source=source) instrument.description = description if imgurl: sn = data.models.SourceName.objects.get(name="Wikipedia") source, created = data.models.Source.objects.get_or_create(source_name=sn, uri=imgurl, defaults={"title": instrument.name}) imagefilename = instrument.name.replace(" ", "_") try: existingimg = instrument.images.get(image__contains="%s" % imagefilename) # TODO: If the image doesn't exist on disk this is an error. fix it. if existingimg.image.size != len(img) or overwrite: # If the imagesize has changed, or overwrite is set, remove the image existingimg.delete() except data.models.Image.DoesNotExist: pass im = data.models.Image() im.image.save("instrument-%s.jpg" % imagefilename, ContentFile(img)) instrument.images.add(im) instrument.save()
def import_instrument_description(instrument, overwrite=False): iname = wikipedia.search(instrument.name) img, imgurl, b, u = wikipedia.get_artist_details(instrument.name) if b and b.startswith("#REDIR"): newname = b.replace("#REDIRECT ", "") img, imgurl, b, u = wikipedia.get_artist_details(newname) if b: sn = data.models.SourceName.objects.get(name="Wikipedia") source, created = data.models.Source.objects.get_or_create( source_name=sn, uri=u, defaults={"title": instrument.name}) description = data.models.Description.objects.create(description=b, source=source) instrument.description = description if imgurl: sn = data.models.SourceName.objects.get(name="Wikipedia") source, created = data.models.Source.objects.get_or_create( source_name=sn, uri=imgurl, defaults={"title": instrument.name}) imagefilename = instrument.name.replace(" ", "_") try: existingimg = instrument.images.get(image__contains="%s" % imagefilename) # TODO: If the image doesn't exist on disk this is an error. fix it. if existingimg.image.size != len(img) or overwrite: # If the imagesize has changed, or overwrite is set, remove the image existingimg.delete() except data.models.Image.DoesNotExist: pass im = data.models.Image() im.image.save("instrument-%s.jpg" % imagefilename, ContentFile(img)) instrument.images.add(im) instrument.save()
def import_artist_wikipedia(artist, overwrite): print "Looking for data on wikipedia" sn = data.models.SourceName.objects.get(name="Wikipedia") refs = artist.references.filter(source_name=sn) if refs.exists(): source = refs.all()[0] wikiurl = source.uri name = os.path.basename(wikiurl) img, bio = wikipedia.get_artist_details(name) if bio: description = data.models.Description.objects.create(description=bio, source=source) if overwrite or not artist.description: artist.description = description artist.save() if img: try: existingimg = artist.images.get(image__contains="%s" % artist.mbid) if not os.path.exists(existingimg.image.path): existingimg.delete() elif existingimg.image.size != len(img) or overwrite: # If the imagesize has changed, or overwrite is set, remove the image os.unlink(existingimg.image.path) existingimg.delete() except data.models.Image.DoesNotExist: pass im = data.models.Image() im.image.save("artist-%s.jpg" % artist.mbid, ContentFile(img)) artist.images.add(im)
def import_artist_wikipedia(artist, source): print "Looking for data on wikipedia" wikipedia_url = source.uri name = os.path.basename(wikipedia_url) img, bio = wikipedia.get_artist_details(name) if bio: description = data.models.Description.objects.create(description=bio, source=source) # We call this with Composers too, which don't have `description_edited` # If d_edited is set, never overwrite it. if not getattr(artist, 'description_edited', False): artist.description = description artist.save() if img: if artist.image: if not os.path.exists(artist.image.image.path): artist.image.delete() elif artist.image.image.size != len(img): # If the imagesize has changed, remove the image os.unlink(existingimg.image.path) artist.image.delete() im = data.models.Image() im.image.save("artist-%s.jpg" % artist.mbid, ContentFile(img)) artist.image = im
def import_artist_wikipedia(artist, source): print "Looking for data on wikipedia" wikipedia_url = source.uri name = os.path.basename(wikipedia_url) img, bio = wikipedia.get_artist_details(name) if bio: description = data.models.Description.objects.create(description=bio, source=source) # We call this with Composers too, which don't have `description_edited` # If d_edited is set, never overwrite it. if not getattr(artist, "description_edited", False): artist.description = description artist.save() if img: if artist.image: if not os.path.exists(artist.image.image.path): artist.image.delete() elif artist.image.image.size != len(img): # If the imagesize has changed, remove the image os.unlink(existingimg.image.path) artist.image.delete() im = data.models.Image() im.image.save("artist-%s.jpg" % artist.mbid, ContentFile(img)) artist.image = im
def import_artist_wikipedia(artist): print "Looking for data on wikipedia" sn = data.models.SourceName.objects.get(name="Wikipedia") refs = artist.references.filter(source_name=sn) if refs.exists(): source = refs.all()[0] wikiurl = source.uri name = os.path.basename(wikiurl) img, bio = wikipedia.get_artist_details(name) if bio: description = data.models.Description.objects.create(description=bio, source=source) # We call this with Composers too, which don't have `description_edited` # If d_edited is set, never overwrite it. if not getattr(artist, 'description_edited', False): artist.description = description artist.save() if img: try: existingimg = artist.images.get(image__contains="%s" % artist.mbid) if not os.path.exists(existingimg.image.path): existingimg.delete() elif existingimg.image.size != len(img): # If the imagesize has changed, remove the image os.unlink(existingimg.image.path) existingimg.delete() except data.models.Image.DoesNotExist: pass except data.models.Image.MultipleObjectsReturned: artist.images.filter(image__contains="%s" % artist.mbid).delete() im = data.models.Image() im.image.save("artist-%s.jpg" % artist.mbid, ContentFile(img)) artist.images.add(im)
def import_artist_bio(a, overwrite): artist = kutcheris.search_artist(a.name) additional_urls = [] if not len(artist): print "Looing for data on wikipedia" i, iurl, b, u = wikipedia.get_artist_details(a.name) if u: additional_urls.append(u) #if not b: # newname = wikipedia.search(a.name) # mx = max(len(a.name), len(newname)) # mn = min(len(a.name), len(newname)) # # Only accept the wikipedia search result if it's # if newname and mn + 3 < mx: # i, b, u = wikipedia.get_artist_details(newname) if b: sn = data.models.SourceName.objects.get(name="Wikipedia") else: print "Found data on kutcheris.com" aid = artist.values()[0] i, b, u = kutcheris.get_artist_details(aid) u = "http://kutcheris.com/artist.php?id=%s" % aid if b: sn = data.models.SourceName.objects.get(name="kutcheris.com") if b: source, created = data.models.Source.objects.get_or_create( source_name=sn, uri=u, defaults={"title": a.name}) description = data.models.Description.objects.create(description=b, source=source) a.description = description if i: print "Found image" try: existingimg = a.images.get(image__contains="%s" % a.mbid) # TODO: If the image doesn't exist on disk this is an error. fix it. #if existingimg.image.size != len(i) or overwrite: if overwrite: # If the imagesize has changed, or overwrite is set, remove the image existingimg.delete() except data.models.Image.DoesNotExist: pass im = data.models.Image() im.image.save("artist-%s.jpg" % a.mbid, ContentFile(i)) a.images.add(im) if additional_urls: for u in additional_urls: # Currently we only have wikipedia additionals, but this may change sn = data.models.SourceName.objects.get(name="Wikipedia") title = u.split("/")[-1].replace("_", " ") source, created = data.models.Source.objects.get_or_create( source_name=sn, uri=u, defaults={"title": title}) a.references.add(source) a.save()
def import_artist_bio(a, overwrite): artist = kutcheris.search_artist(a.name) additional_urls = [] if not len(artist): print "Looing for data on wikipedia" i, iurl, b, u = wikipedia.get_artist_details(a.name) if u: additional_urls.append(u) #if not b: # newname = wikipedia.search(a.name) # mx = max(len(a.name), len(newname)) # mn = min(len(a.name), len(newname)) # # Only accept the wikipedia search result if it's # if newname and mn + 3 < mx: # i, b, u = wikipedia.get_artist_details(newname) if b: sn = data.models.SourceName.objects.get(name="Wikipedia") else: print "Found data on kutcheris.com" aid = artist.values()[0] i, b, u = kutcheris.get_artist_details(aid) u = "http://kutcheris.com/artist.php?id=%s" % aid if b: sn = data.models.SourceName.objects.get(name="kutcheris.com") if b: source, created = data.models.Source.objects.get_or_create(source_name=sn, uri=u, defaults={"title": a.name}) description = data.models.Description.objects.create(description=b, source=source) a.description = description if i: print "Found image" try: existingimg = a.images.get(image__contains="%s" % a.mbid) # TODO: If the image doesn't exist on disk this is an error. fix it. #if existingimg.image.size != len(i) or overwrite: if overwrite: # If the imagesize has changed, or overwrite is set, remove the image existingimg.delete() except data.models.Image.DoesNotExist: pass im = data.models.Image() im.image.save("artist-%s.jpg" % a.mbid, ContentFile(i)) a.images.add(im) if additional_urls: for u in additional_urls: # Currently we only have wikipedia additionals, but this may change sn = data.models.SourceName.objects.get(name="Wikipedia") title = u.split("/")[-1].replace("_", " ") source, created = data.models.Source.objects.get_or_create(source_name=sn, uri=u, defaults={"title": title}) a.references.add(source) a.save()