Exemplo n.º 1
0
def AddMediumByISBN(isbn, search_index):
    """
        id_type: ISBN,
        search_index: Books, DVD
    """
    response = {'input_isbn':isbn}
    try:
        api = amazonproduct.API(config['Amazon.AccessKeyID'],
                                config['Amazon.SecretAccessKey'])

        #~ log.debug("FOLLOW ME FOLLOW ME FOLLOW ME")
        #~ # Valid Values: SKU | UPC | EAN | ISBN (US only, when search
        #~ # index is Books) | JAN. UPC is not valid in the CA locale
        #~ id_type = 'SKU'
        #~ log.debug("id_type: %s" % id_type)

        #~ search_index = 'DVD'
        if search_index == 'Books':
            id_type = 'ISBN'
        elif search_index == 'DVD':
            id_type = 'EAN'
        else:
            raise Exception("unknown search_index: '%s'" % search_index)
            
        node = api.item_lookup(isbn,
                               IdType=id_type,
                               SearchIndex=search_index,
                                ResponseGroup="Images,ItemAttributes")
        item = node.Items.Item
        title = unicode(item.ItemAttributes.Title)
        response['title'] = title
        log.info("title: %s" % title)

        media_type = meta.Session\
                         .query(model.MediaType)\
                         .filter(model.MediaType.name == 'book')\
                         .first()

        medium = model.Medium()
        medium.title = title
        medium.created_ts = datetime.now()
        medium.updated_ts = datetime.now()
        log.debug("medium.title: %s" % medium.title)
        medium.media_type_id = media_type.id
        medium.isbn = isbn

        try:
            url = str(item.LargeImage.URL)
        except:
            url = None
            log.warn("%s has no image" % medium)

        if url:
            print url
            webFile = urllib.urlopen(url)
            buffer = StringIO()
            buffer.write(webFile.read())
            if buffer.len > 65536:
                log.warn("image is too big")
            else:
                medium.image_data = buffer
        else:
            url = ""

        response['image_url'] = url

        meta.Session.add(medium)
        #~ session.commit()

        asin = model.MediaToAsin()
        asin.media_id = medium.id
        asin.asin = item.ASIN
        #~ session.add(asin)
        medium.asins.append(asin)

        added_persons = []
        add_persons(item, 'Author', medium, added_persons, meta.Session)
        add_persons(item, 'Creator', medium, added_persons, meta.Session)
        add_persons(item, 'Manufacturer', medium, added_persons, meta.Session)

        response['persons'] = map(lambda x: x.name, added_persons)

        meta.Session.commit()

        response['medium_url'] = h.url_for(controller='medium', action='edit', id=medium.id)
        response['success'] = True
        response['medium_id'] = medium.id
        response['message'] = "Successfully added '%s'" % isbn
    except Exception, ex:
        log.error("AddMediumByISBN: %s" % ex)
        response['success'] = False
        response['message'] = str(ex)
Exemplo n.º 2
0
def one(isbn):
    print "ISBN: %s" % isbn
    isbn_log.info("ISBN: %s" % isbn)
    node = api.item_lookup(isbn,
                           IdType='ISBN',
                           SearchIndex=SearchIndex,
                            ResponseGroup="Images,ItemAttributes")
    item = node.Items.Item
    title = unicode(item.ItemAttributes.Title)
    #~ title = item.ItemAttributes.Title
    #~ h.ipython()()
    #~ print title
    log.info("title: %s" % title)

    media_type = session.query(model.MediaType)\
                        .filter(model.MediaType.name == 'book')\
                        .first()

    medium = model.Medium()
    medium.title = title
    medium.created_ts = datetime.now()
    medium.updated_ts = datetime.now()
    log.debug("medium.title: %s" % medium.title)
    medium.media_type_id = media_type.id
    medium.isbn = isbn
    #~ medium.image_data = buffer.getvalue()

    try:
        url = str(item.LargeImage.URL)
    except:
        url = None
        log.warn("%s has no image" % medium)
        
    if url:
        print url
        webFile = urllib.urlopen(url)
        buffer = StringIO()
        buffer.write(webFile.read())
        if buffer.len > 65536:
            log.warn("image is too big")
        else:
            medium.image_data = buffer
            
    session.add(medium)
    session.commit()

    asin = model.MediaToAsin()
    asin.media_id = medium.id
    asin.asin = item.ASIN
    #~ session.add(asin)
    medium.asins.append(asin)

    # Languages
    #~ msg = RefHelper(u"added: ")
    added_persons = []
    add_persons(item, 'Author', medium.id, added_persons, session)
    add_persons(item, 'Creator', medium.id, added_persons, session)
    add_persons(item, 'Manufacturer', medium.id, added_persons, session)

    session.commit()
    #~ print unicode(msg.value, errors='replace')
    #~ print msg.value
    print "added: %s" % ", ".join(map(lambda x: x.name, added_persons))
Exemplo n.º 3
0
        """ id = media.id """
        query = meta.Session.query(model.MediaToAsin).filter(model.MediaToAsin.media_id == id)
        asins = map(lambda item: item.asin, query.all())
        log.debug("asins: %s" % asins)
        try:
            node = self.api.item_lookup(",".join(asins), ResponseGroup="Images,ItemAttributes")
        except Exception, ex:
            h.flash("%s: %s" % (type(ex), ex))
            return redirect(url(controller="medium", action="edit", id=id))

        added_persons = []
        medium = meta.Session.query(model.Medium).get(id)
        for item in node.Items.Item:
            log.debug("item.title: %s" % item.ItemAttributes.Title)
            log.debug("item: %s" % item.ASIN)
            add_persons(item, "Actor", medium, added_persons, meta.Session)
            add_persons(item, "Author", medium, added_persons, meta.Session)
            add_persons(item, "Creator", medium, added_persons, meta.Session)
            add_persons(item, "Director", medium, added_persons, meta.Session)
            add_persons(item, "Manufacturer", medium, added_persons, meta.Session)

        meta.Session.commit()

        from pylons import config

        template_name = "person/snippets.mako"
        def_name = "link_to_person"
        template = config["pylons.app_globals"].mako_lookup.get_template(template_name).get_def(def_name)

        if len(added_persons) > 0:
            person_list = map(lambda item: template.render_unicode(item=item, h=h, c=c), added_persons)