gitmon/main.py
2023-11-11 15:40:52 -05:00

55 lines
1.3 KiB
Python

"""GitMon"""
import signal
import time
from loguru import logger
from gitmon.api import GitHubApi
from gitmon.logger import setup_logger
try:
from config import token
except ModuleNotFoundError:
token = None
def log_event(event):
event_type = event.get('type')
repo = event.get('repo')
actor = event.get('actor')
if event_type == 'DeleteEvent':
logger.log('DELETE', f'{event_type}:\thttps://github.com/{repo["name"]}, {actor["login"]}')
else:
logger.log('CREATE', f'{event_type}:\thttps://github.com/{repo["name"]}, {actor["login"]}')
def sigint_handler(signum, frame):
"""Signal handler for Ctrl+C."""
raise KeyboardInterrupt
def main():
"""Main entry point."""
# Setup logger and signal handling
setup_logger()
signal.signal(signal.SIGINT, sigint_handler)
api = GitHubApi(token)
try:
while True:
events = api.get_events()
for event in events:
if (
isinstance(event, dict)
and event.get('type') in ['CreateEvent', 'DeleteEvent']
):
log_event(event)
time.sleep(1)
except KeyboardInterrupt:
logger.info('Program terminating')
if __name__ == '__main__':
main()