Beispiel #1
0
def find_tweets(args):
    """
    Manages search for tweets(checks if username is search param and then
    guides searching algorithm).
    :param args: Object containing search params
    :return: Final search results sorted  and last_time if paging is required
    else 0
    """
    results = []
    if args.name is None:
        results.extend(examine_tweets_from_all_servers(args))
    elif args.name == g.my_name:
        results.extend(examine_tweets_from_this_server(args))
    elif g.registry.servers.get(args.name):
        r = g.get_client(server_name=args.name).search_me(args.to_dict())
        if r.status_code == 200:
            results.extend(r.json())
        else:
            logger.error('Server reported status: {}'.format(r.status_code))
    else:
        raise NotFound('Server with that name does not exists')
    if not results:
        raise NotFound('No search results')

    return sort_and_cut_results(results, args)
Beispiel #2
0
def examine_tweets_from_all_servers(args):
    """
    Sends search_me requests to all active servers and aggregates results
    :param args: Object containing search params
    :return: list of found tweets
    """
    results = []
    for name in g.registry.servers.keys():
        if name == g.my_name:
            try:
                results.extend(examine_tweets_from_this_server(args))
            except Exception as e:
                logger.debug('No tweets from this server', exc_info=e)
        else:
            client = g.get_client(server_name=name)
            r = client.search_me(args.to_dict())
            if r.status_code == 200:
                results.extend(r.json())
            else:
                logger.error(
                    'Server {} reported status: {}'.format(name, r.status_code)
                )
    if not results:
        raise NotFound('No search results')

    return results
Beispiel #3
0
def delete_tweet(id):
    """
    Deletes tweet with supplied id if id exists
    :param id: id of the tweet
    :return:
    """
    result = db.delete_tweet(id)
    if not result:
        raise NotFound('id not found, nothing to delete')
Beispiel #4
0
def modify_tweet(tweet, id):
    """
    Modifies content of the tweet with supplied id
    :param tweet: new content
    :param id: id of the tweet that has to be modified
    :return:
    """
    result = db.modify_tweet(tweet=tweet, id=id)
    if not result:
        raise NotFound('id not found, nothing to modify')
Beispiel #5
0
def delete(id_):
    """
    Removes tweet with provided ID from database.
    :param id_: ID of tweet to delete.
    :raises NotFound: If tweet with provided ID was not found.
    """
    deleted = get_db().do(partial(get_ops().delete_tweet, id_))
    if not deleted:
        raise NotFound(f'Tweet with ID: {id_} not found.')
    return deleted
Beispiel #6
0
def delete_tweet():
    if not request.data:
        raise BadRequest("Data not sent")
    request_data = json.loads(request.data)
    if 'id' in request_data:
        if Storage.delete_tweet((int)(request_data["id"])):
            return "Tweet deleted", 204
        else:
            raise NotFound("Tweet not found")
    else:
        raise BadRequest("Missing tweet content")
Beispiel #7
0
def get_tweet(id):
    """
    Gets tweet from local database with supplied id
    :param id: id of the tweet
    :return: one tweet as dict
    """
    result = None
    for row in db.get_tweet_by_id(id):
        result = get_tweet_content(row)
    if not result:
        raise NotFound('id not found')
    return result
Beispiel #8
0
def by_id(id_):
    """
    Returns tweet with specified ID.
    :param int id_: ID of tweet to get.
    :return: Tweet with requested ID.
    :rtype: Tweet
    :raises NotFound: If tweet with provided ID was not found.
    """
    res = get_db().do(partial(get_ops().get_tweet, id_))
    if res is None:
        raise NotFound(f'Tweet with id: {id_} not found.')
    return Tweet(*res)
Beispiel #9
0
def get_tweets():
    """
    Gets all tweets from local database
    :return: Sorted list of tweets
    """
    results = []
    for row in db.get_all_tweets():
        result = get_tweet_content(row)
        if result:
            results.append(result)
    if not results:
        raise NotFound('No tweets from this server')

    return sorted(results, key=lambda elm: elm['creation_time'])
Beispiel #10
0
def examine_tweets_from_this_server(args):
    """
    Searches through all types of tweets on this server
    :param args: Object containing search params
    :return: list of found tweets
    """
    results = []
    results.extend(search_original_tweets(args))
    results.extend(search_re_tweets(args))

    if not results:
        raise NotFound('No search results')

    return results
Beispiel #11
0
def modify(id_, content):
    """
    Modifies existing tweet with provided ID. New content will be set to
    whatever is provided.

    :param int id_: ID of tweet to modify.
    :param str content: New tweet content.
    :return: Modified tweet.
    :rtype: Tweet
    :raises NotFound: If tweet with provided ID was not found.
    """
    check_length(content)
    updated = get_db().do(partial(get_ops().modify_tweet, id_, content))
    if not updated:
        raise NotFound(f'Tweet for ID: {id_} not found.')
    return Tweet(*updated)
Beispiel #12
0
def single_tweet(tweet_id):
    tweet = Storage.get_by_id(tweet_id)
    if tweet:
        return jsonify(tweet)
    else:
        raise NotFound("Tweet not found")
Beispiel #13
0
def unregister_server(name):
    deleted = registry.delete(name)
    if deleted:
        return '', 204
    else:
        raise NotFound(f'Node with name {name} not found.')