|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | + fb2cal - Facebook Birthday Events to ICS file converter |
| 5 | + Created by: mobeigi |
| 6 | +
|
| 7 | + This program is free software: you can redistribute it and/or modify it under |
| 8 | + the terms of the GNU General Public License as published by the Free Software |
| 9 | + Foundation, either version 3 of the License, or (at your option) any later |
| 10 | + version. |
| 11 | +
|
| 12 | + This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 | + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 | + You should have received a copy of the GNU General Public License along with |
| 16 | + this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import sys |
| 21 | +import logging |
| 22 | +from distutils import util |
| 23 | + |
| 24 | +from .ics_writer import ICSWriter |
| 25 | +from .logger import Logger |
| 26 | +from .config import Config |
| 27 | +from .facebook_browser import FacebookBrowser |
| 28 | +from .transformer import Transformer |
| 29 | + |
| 30 | +from .__init__ import __version__, __status__, __website__, __license__ |
| 31 | + |
| 32 | +# Set CWD to script directory |
| 33 | +os.chdir(sys.path[0]) |
| 34 | + |
| 35 | +# Init logger |
| 36 | +logger = Logger('fb2cal').getLogger() |
| 37 | +logger.info(f'Starting fb2cal v{__version__} ({__status__}) [{__website__}]') |
| 38 | +logger.info(f'This project is released under the {__license__} license.') |
| 39 | + |
| 40 | +try: |
| 41 | + # Read config |
| 42 | + logger.info(f'Attemping to parse config file...') |
| 43 | + config = Config().getConfig() |
| 44 | + logger.info('Config successfully loaded.') |
| 45 | + |
| 46 | + # Set logging level based on config |
| 47 | + try: |
| 48 | + logger.setLevel(getattr(logging, config['LOGGING']['level'])) |
| 49 | + logging.getLogger().setLevel(logger.level) # Also set root logger level |
| 50 | + except AttributeError: |
| 51 | + logger.error(f'Invalid logging level specified. Level: {config["LOGGING"]["level"]}') |
| 52 | + raise SystemError |
| 53 | + |
| 54 | + logger.info(f'Logging level set to: {logging.getLevelName(logger.level)}') |
| 55 | + |
| 56 | + # Init Facebook browser |
| 57 | + facebook_browser = FacebookBrowser() |
| 58 | + |
| 59 | + # Attempt login |
| 60 | + logger.info('Attemping to authenticate with Facebook...') |
| 61 | + facebook_browser.authenticate(config['AUTH']['FB_EMAIL'], config['AUTH']['FB_PASS']) |
| 62 | + logger.info('Successfully authenticated with Facebook.') |
| 63 | + |
| 64 | + # Fetch birthdays for a full calendar year and transform them |
| 65 | + facebook_users = [] |
| 66 | + transformer = Transformer() |
| 67 | + |
| 68 | + # Endpoint will return all birthdays for offset_month plus the following 2 consecutive months. |
| 69 | + logger.info('Fetching all Birthdays via BirthdayCometRootQuery endpoint...') |
| 70 | + for offset_month in [1, 4, 7, 10]: |
| 71 | + birthday_comet_root_json = facebook_browser.query_graph_ql_birthday_comet_root(offset_month) |
| 72 | + facebook_users_for_quarter = transformer.transform_birthday_comet_root_to_birthdays(birthday_comet_root_json) |
| 73 | + facebook_users.extend(facebook_users_for_quarter) |
| 74 | + |
| 75 | + if len(facebook_users) == 0: |
| 76 | + logger.warning(f'Facebook user list is empty. Failed to fetch any birthdays.') |
| 77 | + raise SystemError |
| 78 | + |
| 79 | + logger.info(f'A total of {len(facebook_users)} birthdays were found.') |
| 80 | + |
| 81 | + # Generate ICS |
| 82 | + ics_writer = ICSWriter(facebook_users) |
| 83 | + logger.info('Creating birthday ICS file...') |
| 84 | + ics_writer.generate() |
| 85 | + logger.info('ICS file created successfully.') |
| 86 | + |
| 87 | + # Save to file system |
| 88 | + if util.strtobool(config['FILESYSTEM']['SAVE_TO_FILE']): |
| 89 | + ics_writer.write(config['FILESYSTEM']['ICS_FILE_PATH']) |
| 90 | + |
| 91 | + logger.info('Done! Terminating gracefully.') |
| 92 | +except SystemExit: |
| 93 | + logger.critical(f'Critical error encountered. Terminating.') |
| 94 | + sys.exit() |
| 95 | +finally: |
| 96 | + logging.shutdown() |
0 commit comments