Advertisement
Guest User

/r/SHIELD Countdown Bot

a guest
Jul 26th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. import praw
  2. from pprint import pprint
  3.  
  4. from pytz import timezone
  5. from datetime import datetime, timedelta, tzinfo
  6. from tzlocal import get_localzone
  7. import pytz
  8. import time
  9. import math
  10.  
  11. #Time stuffs
  12. class FixedOffset(tzinfo):
  13.     def __init__(self, offset):
  14.         hours = int(offset[1:-2])
  15.         minutes = int(offset[-2:])
  16.  
  17.         if(offset[:1] == '-'):
  18.             hours = 0 - hours
  19.             minutes = 0 - minutes
  20.  
  21.         self.__offset = timedelta(hours=hours, minutes=minutes)
  22.         self.__dst = timedelta(hours=hours-1, minutes=minutes)
  23.  
  24.     def utcoffset(self, dt):
  25.         return self.__offset
  26.     def dst(self, dt):
  27.         return self.__dst
  28.  
  29. def formatTimeleft(delta):
  30.     days = delta.days
  31.     months = 0
  32. #    if days > 31:
  33. #        days = days % 31
  34. #        months = days / 31
  35.  
  36.     if days == 1:
  37.         daystr = 'day'
  38.     else:
  39.         daystr = 'days'
  40.  
  41. #    if months == 0:
  42.     return str(days) + ' Days'
  43. #    else:
  44. #        return str(math.ceil(months)) + ' Month, ' + str(days) + ' Days'
  45.  
  46. def getDurationstr():
  47.     eastern = timezone('EST')
  48.  
  49.     releaseTimestamp = datetime(2014, 9, 23, 21, 0, 0)# 23/09/2014 9:00PM
  50.     releaseLocalized = eastern.localize(releaseTimestamp)
  51.  
  52.     offset = datetime.now(eastern).strftime('%z')
  53.     now = datetime.now(FixedOffset(offset))
  54.  
  55.     timeLeft = releaseLocalized - now
  56.  
  57.     return formatTimeleft(timeLeft)
  58.  
  59.  
  60. BOT_START = '[](#SHIELDBOTSTART)'
  61. BOT_END = '[](#SHIELDBOTEND)'
  62.  
  63. #User Login
  64. USER = {
  65.     'username': 'SHIELD_countdown_bot',
  66.     'password': 'LOLNOPE'
  67.     }
  68.  
  69.  
  70. ##Reddit Login
  71. r = praw.Reddit(user_agent='Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36', disable_update_check=True)
  72. r.config._ssl_url = None
  73. r.login(USER['username'], USER['password'])
  74.  
  75. page = r.get_wiki_page('shield', 'config/sidebar')
  76. content = page.content_md
  77.  
  78. bot_start_loc = content.find(BOT_START)
  79. bot_end_loc = content.find(BOT_END)
  80.  
  81. string_before = content[:bot_start_loc] + BOT_START
  82. string_after = content[bot_end_loc:]
  83.  
  84. countdown_content = '\n**Agents of SHIELD Season 2 Starts in:**\n# **' + getDurationstr() + '**\n'
  85.  
  86. new_page = string_before + countdown_content + string_after
  87.  
  88. r.edit_wiki_page('shield', 'config/sidebar', new_page, 'Testing Reasons')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement