Help save net neutrality! Learn more.
Close

Please help with amazon look-up action

Help
2011-07-24
2013-04-25
  • Johannes Graumann

    Hi,

    I'm trying to build an "Action", that will, based on basic info in the form (ISBN, Language, …) lookup the rest of the book information. I came up with the script below. But it throws an error and despite my installing clouseau, I don't seem to find where errors are logged and what the actual problem is … I have some suspicions though, that lead to the following questions:
    1) Can I get access to the contents of a field (ISBN) BEFORE the form has been saved (plominoDocument.mediaAsinIsbn in my case)?
    2) where are details on errors logged? I have clouseau installed and activated and told the database to log errors.

    Thanks for any hint, Joh

    ######################################
    # Import the add-on libraries needed #
    ######################################
    import amazonproduct
    import re
    import urllib2
    import sys

    # Deal with stringifying of non-ascii content
    reload(sys)
    sys.setdefaultencoding("utf-8")

    #####################################
    # Define the AWS access credentials #
    #####################################
    AWS_KEY = 'KEY'
    SECRET_KEY = 'SECRET_KEY'

    ##############################
    # Read out the prerequisits: #
    ##############################
    # Only book search implemented
    if plominoDocument.mediaType == 'Buch':
      searchIndex = 'Books'
      idType = 'ISBN'
    else:
      return

    # Works only if ISBN given
    if plominoDocument.mediaAsinIsbn == '':
      return

    # Work out what language's amazon to check
    if plominoDocument.mediaLanguage == 'Deutsch':
      language = 'de'
    elif plominoDocument.mediaLanguage == 'Englisch':
      language = 'us'
    elif plominoDocument.mediaLanguage == 'Spanisch':
      return
    elif plominoDocument.mediaLanguage == 'Anderes':
      language = 'de'
    else
      language = 'fr'

    ############################
    # Establish the connection #
    ############################
    api = amazonproduct.API(AWS_KEY, SECRET_KEY, language)

    ###################
    # Fetch something #
    ###################
    root = api.item_lookup(plominoDocument.mediaAsinIsbn, IdType=idType,
                SearchIndex=searchIndex,ResponseGroup='Medium')
    if len(root.Items == 0):
      return

    # Fill in fields
    #plominoDocument.mediaArtistEditor
    #plominoDocument.mediaAsinIsbn
    #plominoDocument.mediaAudience
    plominoDocument.mediaAuthorComposer = str(root.Items.Item.ItemAttributes.Author)
    plominoDocument.mediaEdition = str(root.Items.Item.ItemAttributes.Edition)
    format = str(root.Items.Item.ItemAttributes.Binding)
    if format == 'Cartonn\xc3\xa9':
      plominoDocument.mediaFormat = "Fester Einband"
    elif format == 'Gebundene Ausgabe':
      plominoDocument.mediaFormat = "Fester Einband"
    elif format == 'Taschenbuch':
      plominoDocument.mediaFormat = "Broschiert"
    else:
      plominoDocument.mediaFormat = "Anderes"
    plominoDocument.mediaLabel = str(root.Items.Item.ItemAttributes.Label)
    #plominoDocument.mediaLanguage
    #plominoDocument.mediaLender
    #plominoDocument.mediaLendingDate
    myMatch = re.match(".*(\d{4,4}).*",str(root.Items.Item.ItemAttributes.PublicationDate))
    if myMatch != None:
      plominoDocument.mediaPublicationYear = myMatch.group(1)
    #plominoDocument.mediaSeries
    #plominoDocument.mediaSubTitle
    plominoDocument.mediaTitle = str(root.Items.Item.ItemAttributes.Title)
    #plominoDocument.mediaType

    url = root.Items.Item.ImageSets.ImageSet.LargeImage.URL.pyval
    fp = open('/tmp/amazonImage.jpg', 'wb')
    fp.write(urllib2.urlopen(url).read())
    fp.close()
    plominoDocument.mediaCover = '/tmp/amazonImage.jpg'

     
  • Eric Brehault

    Eric Brehault - 2011-07-25

    Hello,

    1: as it is not saved, it is unknown on the server.
    So either you save first (so it is stored in the doc, you access it like that: plominoDocument.mediaAsinIsbn), either you send it as parameter to your action url using javascript (and then you read it in the REQUEST like that: plominoDocument.REQUEST.get('mediaAsinIsbn') )

    2: By default, errors occuring in Plomino formulas are not logged in the server log.
    If you want to see the errors in the log, go to your Plomino db parameters, and check "Debug mode".

    Regarding your formula: be aware Plomino formulas are based on Zope Python script, so there are security restrictions.
    So you are not allowed to import Python packages like amazonproduct, urllib2, etc.

    So 2 possibilities: either you implement your formula into an external method (so package import is allowed), either you declare extra utils in plomino:
    https://plomino.svn.sourceforge.net/svnroot/plomino/trunk/Plomino/docs/CUSTOMUTILS_HOWTO.txt

    Eric

     

Log in to post a comment.