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)
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
def get_tweet_content(row): """ Checks if tweet is of type original or re-tweet. If tweet is re-tweet try to acquire content from original server where tweet is perserved. :param row: tweet row from the table tweets :return: tweet as dict """ result = { 'id': row[0], 'name': row[1], 'tweet': row[2], 'creation_time': row[3], 'type': row[4], 'reference': row[5] } if row[5]: try: reference = row[5].split('#') if reference[0] == g.my_name: db_result_row = db.get_tweet_by_id(reference[1])[0] result['tweet'] = db_result_row[2] possible_reference = db_result_row[5] # re tweet on re tweet situation if not result['tweet']: row[5] = possible_reference result['tweet'] = get_tweet_content(row)['tweet'] else: response = g.get_client( server_name=reference[0] ).get_tweet(reference[1]) result['tweet'] = response.json()['tweet'] except Exception as e: logger.debug('Error getting tweet through reference', exc_info=e) result = None return result
def unregister_from_servers(): """ Sends unregister requests to all servers :return: """ for name in g.registry.servers.keys(): if name != g.my_name: client = g.get_client(server_name=name) client.unregister(g.my_name) return 'ok'
def connect_to_server(): """ Sends register request to one server who's address was supplied in the command line :return: Server's response - json with all servers addresses and names """ client = g.get_client(server_address=g.connect_to) return client.register( { 'name': g.my_name, 'port': g.port } )
def connect_to_all_servers(): """ Sends register requests to all servers in network :return: """ for name in g.registry.servers.keys(): if name != g.my_name: client = g.get_client(server_name=name) client.register( { 'name': g.my_name, 'port': g.port } ) return 'ok'