Beispiel #1
0
def call(methodname, params=None):
    """ Invokes API method """

    log.debug('API KEY = {0}, SECRET = {1}'.format(
        settings.FLICKR_API_KEY,
        settings.FLICKR_API_SECRET
    ))
    flickr = flickrapi.FlickrAPI(settings.FLICKR_API_KEY,
                                 secret=settings.FLICKR_API_SECRET)

    try:
        # Extract the api method
        log.debug('Calling method - "%s"' % methodname)
        method = getattr(flickr, methodname)

    except Exception:
        log.error('No such API method.')
        return

    try:
        if not params:
            return method(format='json')
        else:
            return method(**params)

    except urllib2.HTTPError as e:
        log.error('Could not reach service.')

    except Exception as e:
        log.error(e.message)
        return None
Beispiel #2
0
def get_flickr_photos(flickr_json):
    """
    Retrience Flickr photo content from Flickr API
    :param article: str; article name
    :return:        list; list of Flickr photo json
    """
    photos = []
    for i in xrange(settings.NUM_PHOTOS_TO_FETCH):
        try:
            photos.append(
                {
                    'owner': flickr_json['photos']['photo'][i]['owner'],
                    'photo_id': flickr_json['photos']['photo'][i]['id'],
                    'farm': flickr_json['photos']['photo'][i]['farm'],
                    'server': flickr_json['photos']['photo'][i]['server'],
                    'title': flickr_json['photos']['photo'][i]['title'],
                    'secret': flickr_json['photos']['photo'][i]['secret'],
                },
            )
        except (IndexError, KeyError) as e:
            log.error('No more photos to process for: - "%s"' % (e.message))
        log.debug('Photo info: %s' % (str(photos)))
    return photos
Beispiel #3
0
def mashup():

    DataIORedis().connect()
    mysql_inst = DataIOMySQL()
    mysql_inst.connect()

    # Check for POST otherwise GET
    refresh = False
    if request.form:
        article = str(request.form['article']).strip()
        article = '_'.join(article.split())
        log.debug('Processing POST - ' + article)

    else:
        article = str(request.args.get(settings.GET_VAR_ARTICLE)).strip()
        if 'refresh' in request.args:
            refresh = True
        article = '_'.join(article.split())
        log.debug('Processing GET - ' + article)

    # Fetch article count and stored body (if exists)
    article_count = get_article_count()
    body = get_article_stored_body(article)

    if not body or refresh:

        # Calls to Wiki & Flickr APIs
        try:
            wiki = call_wiki(article)
        except WikiAPICallError as e:
            return render_template(e.template, error=e.message)
        try:
            res_json = call_flickr(article)
        except FlickrAPICallError as e:
            return render_template(e.template, error=e.message)

        # Extract photo data
        photos = get_flickr_photos(res_json)
        if not photos:
            render_template('index.html', error="Couldn't find any photos "
                                                "for '{0}'!".format(article))

        # 1. Fetch the max article - Refresh periodically
        # 2. Remove a random article and replace, ensure that max has
        #       been fetched
        # 3. Article insertion and ORM fetch
        # 4. rank photos according to UGC
        # 5. Photo & markup parsing
        # 6. Article content insertion and ORM fetch
        max_aid = get_max_article_id()
        manage_article_storage(max_aid, article_count)
        article_id, insert_ok = handle_article_insert(article, wiki.pageid)
        photos = order_photos_by_rank(article_id, photos)
        page_content = prep_page_content(article_id, article, wiki, photos,
                                         User(current_user.get_id()))
        if insert_ok:
            handle_article_content_insert(article_id, page_content, not body)

    else:
        page_content = json.loads(body, object_hook=_decode_dict)
        # refresh the user id
        page_content['user_id'] = User(current_user.get_id()).get_id()

    # Update last_access
    with ArticleModel() as am:
        am.update_last_access(page_content['article_id'])

    log.info('Rendering article "%s"' % article)
    return render_template('mashup.html', **page_content)