Ejemplo n.º 1
0
def twitter_handler(data, server_entity):
    '''
    Syndicates request data to Twitter.

    :param data: request data with syndication information.
    :param server_entity: the entity we are syndicating for.
    '''
    tw_conf = current_app.config.get('TWITTER', None)
    if not tw_conf:
        abort(400, 'Twitter not configured on the server.')
    tw = Twython(tw_conf['app_key'], tw_conf['app_secret'],
                 tw_conf['token'], tw_conf['token_secret'])
    post_type = os.path.join(server_entity, 'types', 'note')
    errors = validate_service(data, TWITTER_ENTITY, post_type)
    if errors:
        abort(422, ' '.join(errors))
    if 'content' not in data or 'text' not in data['content']:
        abort(422, 'Post content not found!')
    # Get the post we want to syndicate
    post_id = data['links'][0]['post']
    post = get_post_by_id(post_id)
    if not post:
        abort(404)
    # Grab the existing links from the post, if any
    links = []
    if 'links' in post:
        links = post['links']
    if is_duplicate_syndication(links, TWITTER_ENTITY):
        abort(400, 'Post ID %s already syndicated to Twitter.' % (post_id,))
    # Grab information for attachments, if any. Make sure they're only the kind
    # that Twitter accepts and use only the first 4.
    attachments = []
    if 'attachments' in post:
        attachments = post['attachments']
    attachments = [x for x in attachments if x['content_type']\
                   in ('image/gif', 'image/jpeg', 'image/png')]
    if len(attachments) > 4:
        attachments = attachments[0:3]
    media_ids = []
    try:
        for attachment in attachments:
            media_id = upload_media(tw,
                                    get_attachment_file(attachment['digest']),
                                    attachment['content_type'])
            media_ids.append(media_id)
    except TwythonError as e:
        abort(400, str(e))
    if not len(media_ids):
        media_ids = None
    # Create the twitter status and grab the permalink
    try:
        url = create_tweet(tw, data['content']['text'], media_ids=media_ids)
    except TwythonError as e:
        abort(400, str(e))
    # Append the syndication link and `PATCH` the original post
    links.append({'entity': TWITTER_ENTITY, 'type': 'syndication', 'url': url})
    return (post_id, links)
Ejemplo n.º 2
0
Archivo: fb.py Proyecto: dephraser/piss
def facebook_handler(data, server_entity):
    '''
    Syndicates request data to Facebook.

    :param data: request data with syndication information.
    :param server_entity: the entity we are syndicating for.
    '''
    post_type = os.path.join(server_entity, 'types', 'note')
    errors = validate_service(data, FACEBOOK_ENTITY, post_type)
    if errors:
        abort(422, ' '.join(errors))
    if 'content' not in data or 'text' not in data['content']:
        abort(422, 'Post content not found!')
    # Get the post we want to syndicate
    post_id = data['links'][0]['post']
    post = get_post_by_id(post_id)
    if not post:
        abort(404)
    # Grab the existing links from the post, if any
    links = []
    if 'links' in post:
        links = post['links']
    if is_duplicate_syndication(links, FACEBOOK_ENTITY):
        abort(400, 'Post ID %s already syndicated to Facebook.' % (post_id,))
    # Grab information for attachments, if any. Make sure they're only the kind
    # that Twitter accepts and use only the first 4.
    attachments = []
    if 'attachments' in post:
        attachments = post['attachments']
    attachments = [x for x in attachments if x['content_type']\
                   in ('image/gif', 'image/jpeg', 'image/png')]
    if len(attachments) > 1:
        attachments = attachments[0:1]
    if attachments:
        attachments = [os.path.join(server_entity, 'posts', post['_id'],
                                    x['name']) for x in attachments]
    # Create the Facebook status and grab the permalink
    try:
        url = create_status_post(data['content']['text'], attachments)
    except facebook.GraphAPIError as e:
        abort(400, str(e))
    # Append the syndication link and `PATCH` the original post
    links.append({'entity': FACEBOOK_ENTITY, 'type': 'syndication',
                 'url': url})
    return (post_id, links)