mirror of
https://git.juggalol.com/agatha/gitmon.git
synced 2024-12-26 23:56:39 +00:00
Implement basic logging
This commit is contained in:
parent
fb7c7e7bdf
commit
a15b5ffda0
@ -1,10 +1,13 @@
|
||||
"""GitHub API module"""
|
||||
import logging
|
||||
import requests
|
||||
from datetime import datetime
|
||||
from time import sleep
|
||||
from requests.adapters import HTTPAdapter
|
||||
from requests.packages.urllib3.util.retry import Retry
|
||||
from loguru import logger
|
||||
from .logger import setup_logger
|
||||
|
||||
setup_logger()
|
||||
|
||||
|
||||
class GitHubApi:
|
||||
@ -32,14 +35,14 @@ class GitHubApi:
|
||||
|
||||
# Check rate limits
|
||||
if self.rate_remaining == 0 and self.rate_reset > datetime.now():
|
||||
logging.warning(f'Out of API calls. Sleeping until {self.rate_reset}.')
|
||||
logger.warning(f'Out of API calls. Sleeping until {self.rate_reset}.')
|
||||
delta = self.rate_reset - datetime.now()
|
||||
sleep(delta.total_seconds())
|
||||
|
||||
try:
|
||||
response = self.session.get(url, params=params)
|
||||
except requests.RequestException as e:
|
||||
logging.warning(f'Failed to execute GET request: {str(e)}')
|
||||
logger.warning(f'Failed to execute GET request: {str(e)}')
|
||||
return None
|
||||
|
||||
# 304 Not Modified
|
||||
@ -53,6 +56,8 @@ class GitHubApi:
|
||||
"""Parse headers to update rate limits."""
|
||||
self.rate_reset = datetime.fromtimestamp(int(headers['x-ratelimit-reset']))
|
||||
self.rate_remaining = int(headers['x-ratelimit-remaining'])
|
||||
logger.info(f'{self.rate_remaining} calls remaining until {self.rate_reset}')
|
||||
|
||||
|
||||
def get_events(self, page=None):
|
||||
"""Fetch GitHub events."""
|
||||
|
8
gitmon/logger.py
Normal file
8
gitmon/logger.py
Normal file
@ -0,0 +1,8 @@
|
||||
"""Logging module."""
|
||||
import sys
|
||||
from loguru import logger
|
||||
|
||||
|
||||
def setup_logger():
|
||||
logger.remove()
|
||||
logger.add(sys.stdout, level='INFO')
|
4
main.py
4
main.py
@ -1,6 +1,6 @@
|
||||
"""GitMon"""
|
||||
from loguru import logger
|
||||
from gitmon.api import GitHubApi
|
||||
from gitmon.logger import setup_logger
|
||||
|
||||
from config import token
|
||||
|
||||
@ -15,7 +15,7 @@ def print_event(event):
|
||||
|
||||
def main():
|
||||
"""Main entry point."""
|
||||
|
||||
setup_logger()
|
||||
api = GitHubApi(token)
|
||||
|
||||
events = api.get_events()
|
||||
|
Loading…
Reference in New Issue
Block a user