def hastag_tweets():
    try:
        if request.method =='GET':
            return render_template('search_form.html')

        elif request.method == 'POST':
            #tweets
            form_text = request.form["text"]    # get form text
            search_term = form_text.upper()  # process form text
            data_set = api.get_tweets(search_term, 10)

            #create chart
            neutral = 0
            positive = 0
            negative = 0
            print(range(len(data_set)))
            legend = 'Number of tweets'
            labels = ['Positive', 'Negative', 'Neutral']

            for i in range(len(data_set)):
                if data_set[i]['sentiment'] == 'neutral':
                    neutral += 1

                elif data_set[i]['sentiment'] == 'positive':
                    positive += 1

                elif data_set[i]['sentiment'] == 'negative':
                    negative += 1

            values = [positive, negative, neutral]
            print(positive)
            print(negative)
            print(neutral)


            geolocation(search_term)

            if os.path.exists('templates/heatmap_result.html'):
                os.remove('templates/heatmap_result.html')
                shutil.move('heatmap_result.html', 'templates')

            else:
                shutil.move('heatmap_result.html', 'templates')
            return render_template(
            'chart.html', response=data_set, values=values, labels=labels, legend=legend
            )
    except:
        print('Function: hashtag_tweets failed to run')
Beispiel #2
0
    def __init__(self,options):
        self.DEBUG = True

        self.geo_utils = geo_utils()
        self.geoloc = geolocation()
        self.method = 'tdoa'  # method {'tdoa'|'custom'}

        
        self.tdoa_iterator = 1
        self.t_sleep = 0.01

        self.user = '******'
        self.passwd = 'sdrc_pass'
        self.kml_file_number = 1
        self.state = 1
        self.dead_loop = 0
        self.loop_escape = 5
        
        #beacon packet number for extracting from db
        self.bpn = 0
        self.bpn_max = 0




        self.db_host = '192.168.42.200'
        self.db = 'sdrc_db'
        self.t1 = 'data_table'+options.number
        self.t1_fields = '(rpt_pkt_num, rpt_team_id, rpt_location, rpt_timestamp, beacon_id, beacon_pkt_num)'
        self.t1_field1 = '(rpt_location)'
        self.t1_field2 = '(rpt_timestamp)'
        self.x_results = []
        self.y_results = []
Beispiel #3
0
def find_closest_forecast_location(address):
    """address in format['location', 'area', 'state', 'country'], returns closes weather URL"""

    address_location = geolocation(address)
    ############CHANGE OUT OF ARCHIVE WHEN COMPLETE##########
    filename = r"C:\Users\thoma\Desktop\Python\TH Projects\Port\tools\weather\weather_programs\forecast_working_files\Archive\forecast_location.txt"

    with open(filename) as f:
        forecast_location_dict = json.load(f)

    distance_list = []
    url_list = []
    for key, items in forecast_location_dict.items():
        forecast_lat = items[0]
        forecast_lon = items[1]
        distance = coordinate_distance(address_location[0],
                                       address_location[1], forecast_lat,
                                       forecast_lon)
        distance_list.append(distance)
        url_list.append([distance, key, items])

    ranked_list = sorted(distance_list)
    best_ranking = ranked_list[0]

    answer_url = []
    for url_entry in url_list:
        if url_entry[0] == best_ranking:
            answer_url.append(url_entry)

    print("Found these urls and using the first entry: " + str(answer_url))
    return answer_url[0][1]
    def __call__(self):
        try:
            location = geolocation(self.address)
            lat = location[0]
            lon = location[1]
            answer = [self.url, lat, lon, self.address]
            print(answer)

        except Exception as error:
            print(error)
            answer = [self.url, 'fail', 'fail', self.address]
            print(answer)

        return answer
Beispiel #5
0
def find_nearest_noaa_station(address, weather_type):
    location = geolocation(address)
    lat1 = location[0]
    lon1 = location[1]

    if weather_type == "T":
        weather_type = "TAVG"
    elif weather_type == "P":
        weather_type = "PRCP"
    else:
        print("weather type argument spelt wrong - P or T")

    filename = r"C:\Users\thoma\Desktop\Python\TH Projects\Port\tools\weather\weather_databases\ghcnd-inventory_processed.txt"
    with open(filename) as f:
        station_list = json.load(f)
    adj_station_list = []
    for i in range(len(station_list)):
        if str(station_list[i][3]) == str(weather_type):
            adj_station_list.append(station_list[i])
        else:
            None

    if len(adj_station_list) == 0:
        print("weather type argument spelt wrong - P or T")

    distance_dict = {}
    for i in range(len(adj_station_list)):
        station = adj_station_list[i]
        station_code = station[0]
        lat2 = station[1]
        lon2 = station[2]
        key = coordinate_distance(lat1, lon1, lat2, lon2)
        distance_dict[key] = station_code

    sorted_locations = []
    for key in distance_dict.keys():
        sorted_locations.append(float(key))
    sorted_locations = sorted(sorted_locations)

    eligible_dict = {}
    for key in sorted_locations[:30]:
        eligible_dict[key] = distance_dict[key]

    return eligible_dict
Beispiel #6
0
def main():
    t0 = time.perf_counter()

    processed_forecast_location = r'C:\Users\thoma\Desktop\Python\TH Projects\Port\tools\weather\weather_programs\forecast_working_files\processed_forecast_location.txt'
    with open(processed_forecast_location) as f:
        processed_dict = json.load(f)
        print(" dict loaded")

    local_dict = {}
    for key, items in processed_dict.items():
        local_dict[key] = items
    #print(local_dict)

    total_no = 0
    for key in local_dict.keys():
        total_no += 1

    done_no = 0
    location_dict = {}
    reject_dict = {}
    for key, items in local_dict.items():
        t2 = time.perf_counter()

        try:
            raw_address = items.split(",")
            address = []
            for location in raw_address:
                address.append(location.strip())
            print(address)

            country = address[len(address) - 2]
            town = address[0]
            region = address[1]
            state = address[2]

            answer = geolocation(
                town, region, state,
                country)  #[lat, lon, #of entries - above 1 bad]
            lat = answer[0]
            lon = answer[1]
        except Exception:
            lat = "Not found"
            lon = "Not found"

        if lat != "Not found":
            location_dict[key] = [lat, lon, items]
            print([lat, lon, items])

        else:
            reject_dict[key] = items
            print('Not found')

        done_no += 1
        t1 = time.perf_counter()
        time_taken = t1 - t0
        time_prediction = ((time_taken) * (total_no / done_no) - t1) / 3600
        print("On #" + str(done_no) + "/" + str(total_no) +
              " - estimated time to completion in hours is " +
              str(time_prediction) + "and the last opeation took " +
              str(t1 - t2))

    file1 = r'C:\Users\thoma\Desktop\Python\TH Projects\Port\tools\weather\weather_programs\forecast_working_files\forecast_location.txt'
    file2 = r'C:\Users\thoma\Desktop\Python\TH Projects\Port\tools\weather\weather_programs\forecast_working_files\reject_location.txt'

    with open(file1, 'w') as f:
        json.dump(location_dict, f)

    with open(file2, 'w') as f:
        json.dump(reject_dict, f)