def index(req, session, postgres_handle): #if it's not a valid request keep reduction_id None #don't do work for bots that don't know what they're #looking for reduction = None user_reduction_counts = TwitterReduction.get_user_reduction_counts( postgres_handle) random.shuffle(user_reduction_counts) if req.path.split('/') > 1 and req.path.split( '/')[1]: # path looks like '/something' root_user = TwitterUser.by_screen_name( req.path.split('/')[1], postgres_handle) if root_user: reduction = TwitterReduction.get_latest_reduction( root_user.id, postgres_handle) if not reduction and is_int(req.path.split('/')[1]): reduction = TwitterReduction.get_by_id( req.path.split('/')[1], postgres_handle) else: reduction = TwitterReduction.get_latest_reduction( user_reduction_counts[0][0].id, postgres_handle) return { 'reduction_id': reduction.id if reduction and reduction.tiles_are_written_to_disk else None, 'reduction': reduction if reduction and reduction.tiles_are_written_to_disk else None, 'user_reduction_counts': user_reduction_counts }
def next_or_previous_reduction_id(req, session, postgres_handle): reduction = None if 'reduction_id' in req.params: try: reduction_id = int(req.params['reduction_id']) except ValueError: reduction_id = -1 reduction = TwitterReduction.get_by_id(reduction_id, postgres_handle) #prev / next reduction new_reduction_id = -1 if reduction and 'next_or_previous' in req.params: if req.params['next_or_previous'] in ['prev_reduction', 'next_reduction']: ordered_reduction_list = TwitterReduction.get_ordered_id_list( reduction.root_user_id, postgres_handle) for i in range(len(ordered_reduction_list)): if reduction.id == ordered_reduction_list[i]: current_idx = i break if req.params['next_or_previous'] == 'prev_reduction': idx = current_idx - 1 if req.params['next_or_previous'] == 'next_reduction': idx = current_idx + 1 new_reduction_id = ordered_reduction_list[idx] return { 'content_type': 'application/json', 'json': { 'reduction_id': new_reduction_id, 'num_groups': len(TwitterGroup.all_groups(new_reduction_id, postgres_handle)) } }
def group_details(req, session, postgres_handle): if 'group_index' in req.params and 'reduction_id' in req.params: reduction = TwitterReduction.get_by_id(req.params['reduction_id'], postgres_handle) group_index = int(req.params['group_index']) twitter_group = TwitterGroup.get_by_index(reduction.id, group_index, postgres_handle) else: twitter_group = None return { 'template_path': 'social_map/group_details.html', 'twitter_group': twitter_group, }
def community_features(req, session, postgres_handle): reduction = None if req.path.split('/') > 3 and req.path.split('/')[3]: # path looks like '/social_map/community_features/something' root_user = TwitterUser.by_screen_name(req.path.split('/')[3], postgres_handle) if root_user: reduction = TwitterReduction.get_latest_reduction(root_user.id, postgres_handle) if not reduction and is_int(req.path.split('/')[3]): reduction = TwitterReduction.get_by_id(req.path.split('/')[3], postgres_handle) return { 'content_type': 'application/json', 'json':reduction.get_geojson_community_features() if reduction else [], }
def node_details(req, session, postgres_handle): twitter_user, in_links, out_links = None, [], [] if 'node_id' in req.params and 'reduction_id' in req.params: reduction = TwitterReduction.get_by_id(req.params['reduction_id'], postgres_handle) twitter_user = TwitterUser.get_by_id(req.params['node_id'], postgres_handle) if twitter_user: in_links, out_links = reduction.get_in_and_out_links_for_user(req.params['node_id']) return { 'template_path': 'social_map/node_details.html', 'twitter_user': twitter_user, 'in_links':in_links, 'out_links':out_links, }
def group_details(req, session, postgres_handle): if 'group_index' in req.params and 'reduction_id' in req.params: reduction = TwitterReduction.get_by_id(req.params['reduction_id'], postgres_handle) group_index = int(req.params['group_index']) twitter_group = TwitterGroup.get_by_index(reduction.id, group_index, postgres_handle) else: twitter_group = None return { 'template_path': 'social_map/group_details.html', 'twitter_group': twitter_group, }
def map_data(req, session, postgres_handle): reduction = None if 'reduction_id' in req.params: try: reduction_id = int(req.params['reduction_id']) except ValueError: reduction_id = -1 reduction = TwitterReduction.get_by_id(reduction_id, postgres_handle) #details reduction_details = [] if reduction: return_all = not smarttypes.config.IS_PROD # for debugging #return_all = False reduction_details = reduction.get_details(return_all=return_all) return {'content_type': 'application/json', 'json': reduction_details}
def node_details(req, session, postgres_handle): twitter_user, in_links, out_links = None, [], [] if 'node_id' in req.params and 'reduction_id' in req.params: reduction = TwitterReduction.get_by_id(req.params['reduction_id'], postgres_handle) twitter_user = TwitterUser.get_by_id(req.params['node_id'], postgres_handle) if twitter_user: in_links, out_links = reduction.get_in_and_out_links_for_user( req.params['node_id']) return { 'template_path': 'social_map/node_details.html', 'twitter_user': twitter_user, 'in_links': in_links, 'out_links': out_links, }
def map_data(req, session, postgres_handle): reduction = None if 'reduction_id' in req.params: try: reduction_id = int(req.params['reduction_id']) except ValueError: reduction_id = -1 reduction = TwitterReduction.get_by_id(reduction_id, postgres_handle) #details reduction_details = [] if reduction: return_all = not smarttypes.config.IS_PROD # for debugging #return_all = False reduction_details = reduction.get_details(return_all=return_all) return { 'content_type': 'application/json', 'json': reduction_details }
def map_data(req, session, postgres_handle): reduction = None if 'reduction_id' in req.params: try: reduction_id = int(req.params['reduction_id']) except ValueError: reduction_id = -1 reduction = TwitterReduction.get_by_id(reduction_id, postgres_handle) reduction_info = { 'coordinates':[], 'min_coord':0, 'max_coord':1, } if reduction: return_all = not smarttypes.config.IS_PROD # for debugging #return_all = False reduction_info = reduction.get_reduction_info(return_all=return_all) return { 'content_type': 'application/json', 'json': reduction_info }
def index(req, session, postgres_handle): #if it's not a valid request keep reduction_id None #don't do work for bots that don't know what they're #looking for reduction = None user_reduction_counts = TwitterReduction.get_user_reduction_counts(postgres_handle) random.shuffle(user_reduction_counts) if req.path.split('/') > 1 and req.path.split('/')[1]: # path looks like '/something' root_user = TwitterUser.by_screen_name(req.path.split('/')[1], postgres_handle) if root_user: reduction = TwitterReduction.get_latest_reduction(root_user.id, postgres_handle) if not reduction and is_int(req.path.split('/')[1]): reduction = TwitterReduction.get_by_id(req.path.split('/')[1], postgres_handle) else: reduction = TwitterReduction.get_latest_reduction(user_reduction_counts[0][0].id, postgres_handle) return { 'reduction_id': reduction.id if reduction and reduction.tiles_are_written_to_disk else None, 'reduction': reduction if reduction and reduction.tiles_are_written_to_disk else None, 'user_reduction_counts': user_reduction_counts }
def next_or_previous_reduction_id(req, session, postgres_handle): reduction = None if 'reduction_id' in req.params: try: reduction_id = int(req.params['reduction_id']) except ValueError: reduction_id = -1 reduction = TwitterReduction.get_by_id(reduction_id, postgres_handle) #prev / next reduction new_reduction_id = -1 if reduction and 'next_or_previous' in req.params: if req.params['next_or_previous'] in [ 'prev_reduction', 'next_reduction' ]: ordered_reduction_list = TwitterReduction.get_ordered_id_list( reduction.root_user_id, postgres_handle) for i in range(len(ordered_reduction_list)): if reduction.id == ordered_reduction_list[i]: current_idx = i break if req.params['next_or_previous'] == 'prev_reduction': idx = current_idx - 1 if req.params['next_or_previous'] == 'next_reduction': idx = current_idx + 1 new_reduction_id = ordered_reduction_list[idx] return { 'content_type': 'application/json', 'json': { 'reduction_id': new_reduction_id, 'num_groups': len(TwitterGroup.all_groups(new_reduction_id, postgres_handle)) } }
if __name__ == "__main__": postgres_handle = PostgresHandle(smarttypes.connection_string) #get reduction_id qry = """ select tr.id from twitter_reduction tr where tr.tiles_are_written_to_disk = False order by tr.id desc limit 1; """ reduction_id = postgres_handle.execute_query(qry, {})[0]['id'] tile_dir = '../static/tiles/%s/' % reduction_id if not os.path.isdir(tile_dir): os.mkdir(tile_dir) style_file = 'mapnik.xml' min_zoom = 0 max_zoom = 5 bbox = (-180, -85.0511, 180, 85.0511) render_tiles(bbox, style_file, tile_dir, min_zoom, max_zoom) reduction = TwitterReduction.get_by_id(reduction_id, postgres_handle) reduction.tiles_are_written_to_disk = True reduction.save() postgres_handle.connection.commit()
queue.put(None) # wait for pending rendering jobs to complete queue.join() for i in range(num_threads): renderers[i].join() if __name__ == "__main__": postgres_handle = PostgresHandle(smarttypes.connection_string) #get reduction_id qry = """ select tr.id from twitter_reduction tr where tr.tiles_are_written_to_disk = False order by tr.id desc limit 1; """ reduction_id = postgres_handle.execute_query(qry, {})[0]['id'] tile_dir = '../static/tiles/%s/' % reduction_id if not os.path.isdir(tile_dir): os.mkdir(tile_dir) style_file = 'mapnik.xml' min_zoom = 0 max_zoom = 5 bbox = (-180, -85.0511, 180, 85.0511) render_tiles(bbox, style_file, tile_dir, min_zoom, max_zoom) reduction = TwitterReduction.get_by_id(reduction_id, postgres_handle) reduction.tiles_are_written_to_disk = True reduction.save() postgres_handle.connection.commit()