custom.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. import datetime
  3. import crontab
  4. def answer_next_showoffhour_delta(mastodon, context, toot):
  5. soh1_start = crontab.CronTab( "0 0 * * wed" )
  6. soh2_start = crontab.CronTab( "0 12 * * wed" )
  7. soh3_start = crontab.CronTab( "0 18 * * sat" )
  8. soh4_start = crontab.CronTab( "0 6 * * sun" )
  9. soh1_end = crontab.CronTab( "0 1 * * wed" )
  10. soh2_end = crontab.CronTab( "0 13 * * wed" )
  11. soh3_end = crontab.CronTab( "0 19 * * sat" )
  12. soh4_end = crontab.CronTab( "0 7 * * sun" )
  13. next_soh_in_seconds = int( min( soh1_start.next(default_utc=True),
  14. soh2_start.next(default_utc=True),
  15. soh3_start.next(default_utc=True),
  16. soh4_start.next(default_utc=True) ) )
  17. next_soh_end_in_seconds = int( min( soh1_end.next(default_utc=True),
  18. soh2_end.next(default_utc=True),
  19. soh3_end.next(default_utc=True),
  20. soh4_end.next(default_utc=True) ) )
  21. seconds = next_soh_in_seconds
  22. days = seconds // (3600*24)
  23. seconds = seconds % (3600*24)
  24. hours = seconds // 3600
  25. seconds = seconds % 3600
  26. minutes = seconds // 60
  27. seconds = seconds % 60
  28. now = datetime.datetime.now()
  29. tomorrow = datetime.datetime.combine ( datetime.date.today() \
  30. + datetime.timedelta(days=1),
  31. datetime.datetime.min.time() )
  32. seconds_to_tomorrow = (tomorrow - now).total_seconds()
  33. status = "@{} ".format(context["author"])
  34. status += "The next #showoffhour is "
  35. if next_soh_end_in_seconds < 60:
  36. status += "right now! Show the world your hard work!"
  37. else:
  38. if days == 1:
  39. if seconds_to_tomorrow > next_soh_end_in_seconds - (24*3600):
  40. status += "tomorrow! Be sure to join in. :)"
  41. else:
  42. status += "the day after tomorrow! Come back later!"
  43. elif days == 2:
  44. if seconds_to_tomorrow > next_soh_end_in_seconds - (48*3600):
  45. status += "the day after tomorrow! Come back later!"
  46. else:
  47. status += "in three days! You still have time to get ready"
  48. elif days != 0:
  49. status += "in more than {} days! You still have time. :)" \
  50. .format(days)
  51. else:
  52. if seconds_to_tomorrow < (hours*3600) + (minutes*60):
  53. status += "tomorrow! Be sure to join in. :)"
  54. elif 3 < hours:
  55. status += "in about {} hours. Time to prepare your gifs." \
  56. .format(hours)
  57. elif 0 < hours < 3:
  58. status += "in about {} hours! Stay logged in!" \
  59. .format(hours)
  60. else:
  61. if 15 < minutes:
  62. status += "in {} minutes! It's almost there!" \
  63. .format(minutes)
  64. elif 0 < minutes:
  65. status += "in {} minutes! So close!" \
  66. .format(minutes)
  67. else:
  68. status += "starting now! Show the world your hard work!"
  69. mastodon.status_post( status = status, in_reply_to_id = toot["id"] )
  70. return