Rewrite update code to use flask app_context()
This commit is contained in:
		
							parent
							
								
									66ca6c0744
								
							
						
					
					
						commit
						dbc12adb3d
					
				
							
								
								
									
										53
									
								
								update.py
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								update.py
									
									
									
									
									
								
							| 
						 | 
					@ -10,6 +10,7 @@ from email.message import EmailMessage
 | 
				
			||||||
from email.utils import formatdate, make_msgid
 | 
					from email.utils import formatdate, make_msgid
 | 
				
			||||||
from time import time
 | 
					from time import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import flask
 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import agenda.fx
 | 
					import agenda.fx
 | 
				
			||||||
| 
						 | 
					@ -18,14 +19,13 @@ import agenda.types
 | 
				
			||||||
import agenda.uk_holiday
 | 
					import agenda.uk_holiday
 | 
				
			||||||
import agenda.waste_schedule
 | 
					import agenda.waste_schedule
 | 
				
			||||||
from agenda import gwr
 | 
					from agenda import gwr
 | 
				
			||||||
 | 
					from web_view import app
 | 
				
			||||||
config = __import__("config.default", fromlist=[""])
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async def update_bank_holidays() -> None:
 | 
					async def update_bank_holidays(config: flask.config.Config) -> None:
 | 
				
			||||||
    """Update cached copy of UK Bank holidays."""
 | 
					    """Update cached copy of UK Bank holidays."""
 | 
				
			||||||
    t0 = time()
 | 
					    t0 = time()
 | 
				
			||||||
    events = await agenda.uk_holiday.get_holiday_list(config.DATA_DIR)
 | 
					    events = await agenda.uk_holiday.get_holiday_list(config["DATA_DIR"])
 | 
				
			||||||
    time_taken = time() - t0
 | 
					    time_taken = time() - t0
 | 
				
			||||||
    if not sys.stdin.isatty():
 | 
					    if not sys.stdin.isatty():
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
| 
						 | 
					@ -33,11 +33,11 @@ async def update_bank_holidays() -> None:
 | 
				
			||||||
    print(f"took {time_taken:.1f} seconds")
 | 
					    print(f"took {time_taken:.1f} seconds")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async def update_bristol_bins() -> None:
 | 
					async def update_bristol_bins(config: flask.config.Config) -> None:
 | 
				
			||||||
    """Update waste schedule from Bristol City Council."""
 | 
					    """Update waste schedule from Bristol City Council."""
 | 
				
			||||||
    t0 = time()
 | 
					    t0 = time()
 | 
				
			||||||
    events = await agenda.waste_schedule.get_bristol_gov_uk(
 | 
					    events = await agenda.waste_schedule.get_bristol_gov_uk(
 | 
				
			||||||
        date.today(), config.DATA_DIR, config.BRISTOL_UPRN, refresh=True
 | 
					        date.today(), config["DATA_DIR"], config["BRISTOL_UPRN"], refresh=True
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    time_taken = time() - t0
 | 
					    time_taken = time() - t0
 | 
				
			||||||
    if not sys.stdin.isatty():
 | 
					    if not sys.stdin.isatty():
 | 
				
			||||||
| 
						 | 
					@ -47,30 +47,30 @@ async def update_bristol_bins() -> None:
 | 
				
			||||||
    print(f"took {time_taken:.1f} seconds")
 | 
					    print(f"took {time_taken:.1f} seconds")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def send_mail(subject: str, body: str) -> None:
 | 
					def send_mail(config: flask.config.Config, subject: str, body: str) -> None:
 | 
				
			||||||
    """Send an e-mail."""
 | 
					    """Send an e-mail."""
 | 
				
			||||||
    msg = EmailMessage()
 | 
					    msg = EmailMessage()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    msg["Subject"] = subject
 | 
					    msg["Subject"] = subject
 | 
				
			||||||
    msg["To"] = f"{config.NAME} <{config.MAIL_TO}>"
 | 
					    msg["To"] = f"{config['NAME']} <{config['MAIL_TO']}>"
 | 
				
			||||||
    msg["From"] = f"{config.NAME} <{config.MAIL_FROM}>"
 | 
					    msg["From"] = f"{config['NAME']} <{config['MAIL_FROM']}>"
 | 
				
			||||||
    msg["Date"] = formatdate()
 | 
					    msg["Date"] = formatdate()
 | 
				
			||||||
    msg["Message-ID"] = make_msgid()
 | 
					    msg["Message-ID"] = make_msgid()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Add extra mail headers
 | 
					    # Add extra mail headers
 | 
				
			||||||
    for header, value in config.MAIL_HEADERS:
 | 
					    for header, value in config["MAIL_HEADERS"]:
 | 
				
			||||||
        msg[header] = value
 | 
					        msg[header] = value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    msg.set_content(body)
 | 
					    msg.set_content(body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    s = smtplib.SMTP(config.SMTP_HOST)
 | 
					    s = smtplib.SMTP(config["SMTP_HOST"])
 | 
				
			||||||
    s.sendmail(config.MAIL_TO, [config.MAIL_TO], msg.as_string())
 | 
					    s.sendmail(config["MAIL_TO"], [config["MAIL_TO"]], msg.as_string())
 | 
				
			||||||
    s.quit()
 | 
					    s.quit()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_gwr_advance_ticket_date() -> None:
 | 
					def update_gwr_advance_ticket_date(config: flask.config.Config) -> None:
 | 
				
			||||||
    """Update GWR advance ticket date cache."""
 | 
					    """Update GWR advance ticket date cache."""
 | 
				
			||||||
    filename = os.path.join(config.DATA_DIR, "advance-tickets.html")
 | 
					    filename = os.path.join(config["DATA_DIR"], "advance-tickets.html")
 | 
				
			||||||
    existing_html = open(filename).read()
 | 
					    existing_html = open(filename).read()
 | 
				
			||||||
    existing_date = gwr.extract_weekday_date(existing_html)
 | 
					    existing_date = gwr.extract_weekday_date(existing_html)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -92,12 +92,12 @@ New date: {new_date}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Agenda: https://edwardbetts.com/agenda/
 | 
					Agenda: https://edwardbetts.com/agenda/
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
    send_mail(subject, body)
 | 
					    send_mail(config, subject, body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_thespacedevs() -> None:
 | 
					def update_thespacedevs(config: flask.config.Config) -> None:
 | 
				
			||||||
    """Update cache of space launch API."""
 | 
					    """Update cache of space launch API."""
 | 
				
			||||||
    rocket_dir = os.path.join(config.DATA_DIR, "thespacedevs")
 | 
					    rocket_dir = os.path.join(config["DATA_DIR"], "thespacedevs")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    t0 = time()
 | 
					    t0 = time()
 | 
				
			||||||
    rockets = agenda.thespacedevs.next_launch_api(rocket_dir)
 | 
					    rockets = agenda.thespacedevs.next_launch_api(rocket_dir)
 | 
				
			||||||
| 
						 | 
					@ -108,11 +108,11 @@ def update_thespacedevs() -> None:
 | 
				
			||||||
    print(f"took {time_taken:.1f} seconds")
 | 
					    print(f"took {time_taken:.1f} seconds")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_gandi() -> None:
 | 
					def update_gandi(config: flask.config.Config) -> None:
 | 
				
			||||||
    """Retrieve list of domains from gandi.net."""
 | 
					    """Retrieve list of domains from gandi.net."""
 | 
				
			||||||
    url = "https://api.gandi.net/v5/domain/domains"
 | 
					    url = "https://api.gandi.net/v5/domain/domains"
 | 
				
			||||||
    headers = {"authorization": "Bearer " + config.GANDI_TOKEN}
 | 
					    headers = {"authorization": "Bearer " + config["GANDI_TOKEN"]}
 | 
				
			||||||
    filename = os.path.join(config.DATA_DIR, "gandi_domains.json")
 | 
					    filename = os.path.join(config["DATA_DIR"], "gandi_domains.json")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    r = requests.request("GET", url, headers=headers)
 | 
					    r = requests.request("GET", url, headers=headers)
 | 
				
			||||||
    items = r.json()
 | 
					    items = r.json()
 | 
				
			||||||
| 
						 | 
					@ -127,16 +127,17 @@ def main() -> None:
 | 
				
			||||||
    now = datetime.now()
 | 
					    now = datetime.now()
 | 
				
			||||||
    hour = now.hour
 | 
					    hour = now.hour
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with app.app_context():
 | 
				
			||||||
        if hour % 3 == 0:
 | 
					        if hour % 3 == 0:
 | 
				
			||||||
        asyncio.run(update_bank_holidays())
 | 
					            asyncio.run(update_bank_holidays(app.config))
 | 
				
			||||||
        asyncio.run(update_bristol_bins())
 | 
					            asyncio.run(update_bristol_bins(app.config))
 | 
				
			||||||
        update_gwr_advance_ticket_date()
 | 
					            update_gwr_advance_ticket_date(app.config)
 | 
				
			||||||
        update_gandi()
 | 
					            update_gandi(app.config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if hour % 12 == 0:
 | 
					        if hour % 12 == 0:
 | 
				
			||||||
        agenda.fx.get_exchange_rates(config)
 | 
					            agenda.fx.get_rates(app.config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    update_thespacedevs()
 | 
					        update_thespacedevs(app.config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue