gitmon/main.py

55 lines
1.3 KiB
Python
Raw Normal View History

2023-11-11 00:25:45 +00:00
"""GitMon"""
2023-11-11 20:40:52 +00:00
import signal
2023-11-11 03:46:54 +00:00
import time
from loguru import logger
2023-11-11 00:25:45 +00:00
from gitmon.api import GitHubApi
2023-11-11 03:33:08 +00:00
from gitmon.logger import setup_logger
2023-11-11 00:25:45 +00:00
2023-11-11 04:43:25 +00:00
try:
from config import token
except ModuleNotFoundError:
token = None
2023-11-11 02:33:57 +00:00
2023-11-11 00:25:45 +00:00
2023-11-11 03:46:54 +00:00
def log_event(event):
2023-11-11 01:26:21 +00:00
event_type = event.get('type')
repo = event.get('repo')
actor = event.get('actor')
if event_type == 'DeleteEvent':
2023-11-11 19:34:14 +00:00
logger.log('DELETE', f'{event_type}:\thttps://github.com/{repo["name"]}, {actor["login"]}')
else:
2023-11-11 19:34:14 +00:00
logger.log('CREATE', f'{event_type}:\thttps://github.com/{repo["name"]}, {actor["login"]}')
2023-11-11 01:26:21 +00:00
2023-11-11 20:40:52 +00:00
def sigint_handler(signum, frame):
"""Signal handler for Ctrl+C."""
raise KeyboardInterrupt
2023-11-11 00:25:45 +00:00
def main():
"""Main entry point."""
2023-11-11 20:40:52 +00:00
# Setup logger and signal handling
2023-11-11 03:33:08 +00:00
setup_logger()
2023-11-11 20:40:52 +00:00
signal.signal(signal.SIGINT, sigint_handler)
2023-11-11 00:25:45 +00:00
2023-11-11 20:40:52 +00:00
api = GitHubApi(token)
2023-11-11 03:46:54 +00:00
2023-11-11 20:40:52 +00:00
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')
2023-11-11 00:25:45 +00:00
if __name__ == '__main__':
main()