2017年4月20日 星期四

Use Python with PyAutoGUI to Edit a Quake Forecast Automatically

Use Python with PyAutoGUI to Edit a Quake Forecast Automatically

Use Python with PyAutoGUI to Edit a Quake Forecast Automatically


In this Python program, I use PyAutoGUI to edit a quake forecast automatically.

import pyautogui
import time
import pyperclip
pyautogui.PAUSE = 0.5
pyautogui.FAILSAFE = True

def get_time_stamp():
    time_stamp = time.strftime("%Y-%m-%d")
    time_stamp += ' '
    time_stamp += time.strftime("%H:%M")
    time_stamp += ' UTC+8'
    return time_stamp

quake_forecast_title = {
    'Chinese': '地震預報:',
    'Japanese': '地震予報:',
    'English': 'Quake Forecast: '
}

author = {
    'Chinese': '台灣地震預測研究所 所長\n林湧森',
    'Japanese': '台湾地震予測研究所 所長\n林湧森',
    'English': 'Dyson Lin\nFounder & CEO, Taiwan Quake Forecast Institute'}

Within = {
    'Chinese': '',
    'Japanese': '',
    'English': 'Within '}

Days = {
    'Chinese': '日以內,',
    'Japanese': '日以內に、',
    'English': ' days, '}

Red = {
    'Chinese': '紅:',
    'Japanese': '赤:',
    'English': 'Red: '}

Orange = {
    'Chinese': '橙:',
    'Japanese': 'オレンジ:',
    'English': 'Orange: '}

Yellow = {
    'Chinese': '黃:',
    'Japanese': '黃:',
    'English': 'Yellow: '}

station = {}

station['Taoyuan'] = {
    'Chinese': '桃園站',
    'Japanese': '桃園局',
    'English': 'Taoyuan Station '}

station['Hsinchu Green Light'] = {
    'Chinese': '新竹綠光站',
    'Japanese': '新竹綠光局',
    'English': 'Hsinchu Green Light Station '}

station['Yilan'] = {
    'Chinese': '宜蘭站',
    'Japanese': '宜蘭局',
    'English': 'Yilan Station '}

station['Taichung Tantzu'] = {
    'Chinese': '台中潭子站',
    'Japanese': '台中潭子局',
    'English': 'Taichung Tantzu Station '}

station['Tainan Xinsheng'] = {
    'Chinese': '台南新生站',
    'Japanese': '台南新生局',
    'English': 'Tainan Xinsheng Station '}

station['Tainan Joy & Luck'] = {
    'Chinese': '台南喜福站',
    'Japanese': '台南喜福局',
    'English': 'Tainan Joy & Luck Station '}

station['Kaohsiung'] = {
    'Chinese': '高雄站',
    'Japanese': '高雄局',
    'English': 'Kaohsiung Station '}

station['Tainan Hot Heart'] = {
    'Chinese': '台南熱心站',
    'Japanese': '台南熱心局',
    'English': 'Tainan Hot Heart Station '}

station['Durban, South Africa'] = {
    'Chinese': '南非德本站',
    'Japanese': '南アフリカ ダーバン局',
    'English': 'Durban, South Africa Station '}

station['Nantou Caotun'] = {
    'Chinese': '南投草屯站',
    'Japanese': '南投草屯局',
    'English': 'Nantou Caotun Station '}

station['Faenza, Italy'] = {
    'Chinese': '義大利法恩扎站',
    'Japanese': 'イタリア ファエンツァ局',
    'English': 'Faenza, Italy Station '}

station_name = station['Durban, South Africa']

quake_forecast = [
    {'color': Red,
    'time': 2,
    'location': {
        'Chinese': '東台灣或南日本,',
        'Japanese': '東台湾または南日本、',
        'English': 'East Taiwan or South Japan, '},
    'magnitude': 'M6+ ~ M7+'},
    {'color': Orange,
    'time': 10,
    'location': {
        'Chinese': '東台灣或南日本,',
        'Japanese': '東台湾または南日本、',
        'English': 'East Taiwan or South Japan, '},
    'magnitude': 'M6+ ~ M7+'}]

time_stamp = get_time_stamp()
languages = ['Chinese', 'Japanese', 'English']

blog_title = {}
for language in languages:
    blog_title[language] = station_name[language]
    blog_title[language] += quake_forecast_title[language]
    quake_number = 0
    for quake in quake_forecast:
        quake_number += 1
        blog_title[language] += '('
        blog_title[language] += str(quake_number)
        blog_title[language] += ')'
        blog_title[language] += quake['color'][language]
        blog_title[language] += Within[language]
        blog_title[language] += str(quake['time'])
        blog_title[language] += Days[language]
        blog_title[language] += quake['location'][language]
        blog_title[language] += quake['magnitude']
        blog_title[language] += ' '
blog_title_str = (time_stamp + ' ')
blog_title_str += (blog_title['Chinese'])
blog_title_str += (blog_title['Japanese'])
blog_title_str += blog_title['English']
blog_title_str = blog_title_str.strip() # remove extra white space
print(blog_title_str)
pyperclip.copy(blog_title_str) # copy to clipboard

blog_text = {}
for language in languages:
    blog_text[language] = station_name[language]
    blog_text[language] += quake_forecast_title[language] + '\n'
    quake_number = 0
    for quake in quake_forecast:
        quake_number += 1
        blog_text[language] += '('
        blog_text[language] += str(quake_number)
        blog_text[language] += ')'
        blog_text[language] += quake['color'][language]
        blog_text[language] += Within[language]
        blog_text[language] += str(quake['time'])
        blog_text[language] += Days[language]
        blog_text[language] += quake['location'][language]
        blog_text[language] += quake['magnitude']
        blog_text[language] += '\n'
    blog_text[language] += '\n'    
    blog_text[language] += author[language]
    blog_text[language] += '\n'
    blog_text[language] += time_stamp
    blog_text[language] += '\n\n\n'

blog_text_str = ''        
for language in languages:
    blog_text_str += blog_text[language]
blog_text_str = blog_text_str.strip() # remove extra white space
print(blog_text_str)
pyperclip.copy(blog_text_str) # copy to clipboard

台灣地震預測研究所 所長
林湧森(Dyson Lin)
2017-04-20 14:40 UTC+8

沒有留言:

張貼留言