twitchnotifier 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. '''
  3. The module that does everything according to what the user wants
  4. '''
  5. import argparse
  6. import time
  7. import sys
  8. import signal
  9. import os
  10. import libtn
  11. def cb_sighup(signum, frame):
  12. '''
  13. A signal handler that makes TN reread the cfg file
  14. Parameters:
  15. signum - signal number
  16. frame - current stack frame
  17. '''
  18. API.fmt.read_file()
  19. if __name__ == '__main__':
  20. PARSER = argparse.ArgumentParser()
  21. PARSER.add_argument('-c', '--nick', help='Twitch nickname', default='')
  22. PARSER.add_argument('-i', '--interval', help='Interval between checks '
  23. 'in seconds. Default: 120', type=int, default=120)
  24. PARSER.add_argument('-n', '--online', help='Only check for online channels'
  25. ' a user follows', action='store_true')
  26. PARSER.add_argument('-f', '--offline', help='Only check for offline'
  27. ' channels a user follows', action='store_true')
  28. PARSER.add_argument('-v', '--verbose', help='Enable verbose output',
  29. action='store_true')
  30. PARSER.add_argument('-u', '--user', help='Check status of user(,user)',
  31. type=str)
  32. PARSER.add_argument('-t', '--token', help='Tokens are not needed anymore. '
  33. 'Option is left here for compability', type=str)
  34. PARSER.add_argument('-l', '--logfile', help='File used for logging events '
  35. 'in -c/--nick mode', type=str)
  36. PARSER.add_argument('-g', '--config', help='Path to configuration file',
  37. type=str)
  38. ARGS = PARSER.parse_args()
  39. if not ARGS.nick and not ARGS.user:
  40. print('You have to pass atleast either -c/--nick or '
  41. '-u/--user to ' + sys.argv[0] + '!')
  42. sys.exit(1)
  43. CONFIG_FILE = os.environ.get('XDG_CONFIG_HOME',
  44. os.environ.get('HOME', '') + '/.config')
  45. CONFIG_FILE += '/twitchnotifier.cfg'
  46. if ARGS.config:
  47. CONFIG_FILE = ARGS.config
  48. if ARGS.verbose:
  49. print('Configuration file:', CONFIG_FILE)
  50. FMT = libtn.Settings(CONFIG_FILE)
  51. API = libtn.NotifyApi(ARGS.nick, FMT, ARGS.logfile, ARGS.verbose)
  52. signal.signal(signal.SIGHUP, cb_sighup)
  53. if ARGS.user:
  54. LST = ARGS.user.split(',')
  55. RET = API.check_if_online(LST)
  56. EXIT_CODE = 1
  57. for name, data in RET.items():
  58. if data[0] is True:
  59. EXIT_CODE = 0
  60. print(data[1])
  61. del API
  62. sys.exit(EXIT_CODE)
  63. try:
  64. ST = API.get_status()
  65. except NameError:
  66. print(ARGS.nick + ' is an invalid nickname!', file=sys.stderr)
  67. del API
  68. sys.exit(1)
  69. if ARGS.online:
  70. EXIT_CODE = 1
  71. for name, data in ST.items():
  72. if data[0]:
  73. EXIT_CODE = 0
  74. print(libtn.repl(data[1], name, API.fmt.list_entry['on']))
  75. del API
  76. sys.exit(EXIT_CODE)
  77. if ARGS.offline:
  78. EXIT_CODE = 1
  79. for name, data in ST.items():
  80. if not data[0]:
  81. EXIT_CODE = 0
  82. print(libtn.repl(data[1], name, API.fmt.list_entry['off']))
  83. del API
  84. sys.exit(EXIT_CODE)
  85. # Parse the initial statuses
  86. API.diff(ST)
  87. while True:
  88. time.sleep(ARGS.interval)
  89. API.diff(API.get_status())