Пример #1
0
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import requests
from datetime import datetime
from datetime import timedelta
from datetime import date
from geopy.distance import vincenty
from lib import json_io
from lib import csv_io

url = 'http://denguefever.csie.ncku.edu.tw/file/dengue_all.csv'
data = csv_io.req_csv(url, 'utf-8')
weather_data = json_io.read_json('../data/weather.json')
drug_data = json_io.read_json('../data/drug_days.json')


def format_data(current_date, value):
    d = datetime.strptime(current_date, '%Y/%m/%d').date().strftime('%Y/%m/%d')
    return {\
            'date': current_date, \
            'value': value, \
            '氣溫': weather_data[d]['氣溫'], \
            '相對溼度': weather_data[d]['相對溼度'], \
            '降水量': weather_data[d]['降水量'] \
           }


def insert_village_data(village_data, village_values, current_date):
    rain, rain_day = get_wather_data(current_date)

    for v in village_values:
            precp += float(tds[10].text)
            precp_count += 1
        except:
            pass

    try:
        temp = round(temp / temp_count, 2)
        rh = round(rh / rh_count, 2)
        precp = round(precp)
    except:
        return temp, rh, precp
    return temp, rh, precp


if __name__ == '__main__':
    url_t = "http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=467410&datepicker="
    now = datetime.strptime('2015/10/20', '%Y/%m/%d').date()
    end = datetime.now().date()
    data = json_io.read_json('../data/weather.json')
    while now < end:
        html_doc_t = requests.get(url_t + now.strftime('%Y-%m-%d')).text
        temp_t, rh_t, precp_t = get_values(html_doc_t)

        data[now.strftime('%Y/%m/%d')] = {
            '氣溫': temp_t,
            '相對溼度': rh_t,
            '降水量': precp_t
        }
        now += timedelta(days=1)
    json_io.write_json('../data/weather.json', data)
Пример #3
0
import requests
from bs4 import BeautifulSoup

def is_match_country (title, countries):
    for country in countries:
        if len(country) == 3 and title.startswith(country):
            return country
        elif country in title:
            return country
    return False

html_doc = requests.get('http://www.bbc.com/zhongwen/trad/world').text
soup = BeautifulSoup(html_doc)
articles = soup.select(".top-stories__stories .hard-news-unit")

country_list = json_io.read_json('./data/tw.json')
'''lang = json_io.read_json('./data/lang.json')
country_list = []
for en, country in lang.iteritems():
    if country['tw']:
        country_list.append(country['tw'])
json_io.write_json('tw.json', country_list)'''

geo = json_io.read_json('./data/latlng.json')

pages = []
has_counties = []
for article in articles:
    title_link = article.select('.hard-news-unit__headline a')
    if title_link:
        title = title_link[0].text.replace(' ', '').replace('\n', '')
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import requests
from datetime import datetime
from datetime import timedelta
from datetime import date
from geopy.distance import vincenty
from lib import json_io
from lib import csv_io

url = 'http://denguefever.csie.ncku.edu.tw/file/dengue_all_v3.csv'
data = csv_io.req_csv(url, 'utf-8')
weather_data = json_io.read_json('../data/dynamic/weather.json')
csv_output = [['日期', '區別', '里別', '病例數']]
village_list = []
village_hash = {}
village_table = []

def format_data(current_date, value):
    d = datetime.strptime(current_date, '%Y/%m/%d').date().strftime('%Y/%m/%d')
    try:
        return {\
            'date': current_date, \
            'value': value, \
            '氣溫': weather_data[d]['氣溫'], \
            '相對溼度': weather_data[d]['相對溼度'], \
            '降水量': weather_data[d]['降水量'] \
           }
    except:
        return {\
            'date': current_date, \
Пример #5
0
            continue
        delta = now - event_date
        item[-1], item[-2] = float(item[-1]), float(item[-2])
        if delta.days < 5:
            days5.append(item)
            if delta.days < 3:
                days3.append(item)
                if delta.days < 1:
                    days1.append(item)
    return days5, days3, days1


if __name__ == '__main__':
    url = 'http://denguefever.csie.ncku.edu.tw/file/dengue_all_v3.csv'
    data = csv_io.req_csv(url, 'utf-8')
    output_data = json_io.read_json('../data/dynamic/data.json')
    #output_data = {}

    header = data[0]
    data = data[1:]
    data = sorted(data,
                  key=lambda x: datetime.strptime(x[1], '%Y/%m/%d').date())
    now = datetime.strptime(sys.argv[1], '%Y/%m/%d').date()
    end = datetime.strptime(data[-1][1], '%Y/%m/%d').date() + timedelta(days=1)
    while now < end:
        print(now)
        days5, days3, days1 = getDaysWithin(data)
        days1 = geo.get_hot_points(days1, 3, 50)
        days3 = geo.get_hot_points(days3, 3, 50)
        days5 = geo.get_hot_points(days5, 3, 50)
        days1.insert(0, header)
Пример #6
0
                    inbox_msg += msg['from']['name'].encode('utf-8') + ': \n'
                    inbox_msg += msg['message'].encode('utf-8') + '\n'
                    inbox_msg += '------------------\n'
    except KeyError:
        print 'KeyError.'
        exit(-1)
    return inbox_msg

def is_one_day(updated_time):
    yesterday = datetime.today() - timedelta(days=1)  
    post_date = datetime.strptime(updated_time[:19], '%Y-%m-%dT%H:%M:%S')
    return post_date >= yesterday

if __name__=='__main__':

    user = json_io.read_json('user.json')
    fb_config = user['Facebook']
    mail_config = user['Mailer']

    fb = FB(fb_config[u'oauth_access_token'])
    mailer = Mailer(mail_config['username'], mail_config['passwd'], mail_config['fromaddr'])
    
    stream = fb.get_stream()
    notifications = fb.get_notification()
    inbox = fb.get_inbox()

    stream_msg = stream_summary(stream)
    notifications_msg = notification_summary(notifications)
    inbox_msg = mailbox_summary(inbox)
    
    fb_summary = 'News: \n %s \n Notifications \n %s \n Inbox: \n %s \n' \
Пример #7
0
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from lib import json_io
import geocoder
import time


lang = json_io.read_json('./data/lang.json')
country_dic = {}
for en, country in iter(lang.items()):
    name = country['tw']
    if name:
        latlng = geocoder.google(en).latlng
        country_dic[name] = latlng
    time.sleep(1)

json_io.write_json('latlng.json', country_dic)
Пример #8
0
from lib import json_io
from datetime import datetime
from datetime import timedelta
from datetime import date

data = json_io.read_json('../../data/2014_kao_bar_data.json')
s = datetime.strptime('2014/10/20', '%Y/%m/%d').date()
e = datetime.strptime('2014/11/7', '%Y/%m/%d').date()

t = 0
for row in data:
    d = datetime.strptime(row['date'], '%Y/%m/%d').date()
    if d > s and d < e:
        t += row['value']
        print(row['date'])
print(t)
Пример #9
0
                    inbox_msg += '------------------\n'
    except KeyError:
        print 'KeyError.'
        exit(-1)
    return inbox_msg


def is_one_day(updated_time):
    yesterday = datetime.today() - timedelta(days=1)
    post_date = datetime.strptime(updated_time[:19], '%Y-%m-%dT%H:%M:%S')
    return post_date >= yesterday


if __name__ == '__main__':

    user = json_io.read_json('user.json')
    fb_config = user['Facebook']
    mail_config = user['Mailer']

    fb = FB(fb_config[u'oauth_access_token'])
    mailer = Mailer(mail_config['username'], mail_config['passwd'],
                    mail_config['fromaddr'])

    stream = fb.get_stream()
    notifications = fb.get_notification()
    inbox = fb.get_inbox()

    stream_msg = stream_summary(stream)
    notifications_msg = notification_summary(notifications)
    inbox_msg = mailbox_summary(inbox)
Пример #10
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from lib import json_io
import os
base_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(base_dir)
import sys
data1 = json_io.read_json('./data1.json')['result']['records']
data2 = json_io.read_json('./data2.json')['result']['records']
data3 = json_io.read_json('./data3.json')['result']['records']
data4 = json_io.read_json('./data4.json')['result']['records']

data = data1 + data2 + data3 + data4
new_data = {}

for row in data:
    if row['SiteName'] not in new_data:
        new_data[row['SiteName']] = { \
                'city': row['County'], \
                'riverName"': row['River'], \
                'date': row['SampleDate'], \
                'basin': row['Basin'] \
                }
    prop = row['ItemEngAbbreviation']
    value = row['ItemValue']
    new_data[row['SiteName']][prop] = value

print (new_data.keys())
json_io.write_json('./data/river.json', new_data) 
            p2 = (item[-2], item[-1])
            dis = vincenty(p1, p2).meters
            if dis < 500:
                count += 1
            if count > len(input_data) * 0.03:
                break
        if count > len(input_data) * 0.03:
            tmp = each.copy()
            tmp.append(count)
            circle.append(tmp)
        count = 0
    return circle


if __name__ == '__main__':
    new_data = json_io.read_json('../../data/2015_kao_data.json')
    data = csv_io.read_csv('./2015_dengue.csv')
    now = datetime.strptime(new_data['end'], '%Y/%m/%d').date()
    end = datetime.strptime(data[-1][0], '%Y/%m/%d').date() + timedelta(days=1)
    row = data[0]
    header = row
    header.append('count')
    #new_data = {}
    data = data[1:]
    while now < end:
        in_three_days = []
        in_five_days = []
        for row in data:
            event_date = datetime.strptime(row[0], '%Y/%m/%d').date()
            if event_date > now:
                break