MonroCoury

pyNewYear

Dec 13th, 2017
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import datetime
  4. import time
  5. import sys
  6. import platform
  7. from dateutil.relativedelta import relativedelta
  8. from threading import Thread
  9. try:
  10.     import requests
  11. except ImportError:
  12.     os.system("pip install requests")
  13.  
  14.  
  15. def download_file(url, local_fname):
  16.     try:
  17.         r = requests.get(url, stream=True)
  18.         with open(local_fname, 'wb') as f:
  19.             for chunk in r.iter_content(chunk_size=1024):
  20.                 if chunk:
  21.                     f.write(chunk)
  22.  
  23.         size = os.stat(local_fname).st_size / 1024
  24.         if size > 1100:
  25.             return True
  26.         else:
  27.             return False
  28.     except:
  29.         return False
  30.  
  31. def play_video(vid_name):
  32.     os_name = platform.system()
  33.     if "Linux" in os_name:
  34.         os.system("xdg-open %s" % vid_name)
  35.     elif "Windows" in os_name:
  36.         os.system(vid_name)
  37.     elif "Darwin" in os_name:
  38.         os.system("open %s" % vid_name)
  39.  
  40. def count_down(future):
  41.     diff = relativedelta(datetime.datetime.now(), future)
  42.     days = abs(diff.days)
  43.     hours = abs(diff.hours)
  44.     mins = abs(diff.minutes)
  45.     secs = abs(diff.seconds)
  46.     return "\r%d days, %d hours, %d mins %d secs left.." % (days, hours, mins, secs)
  47.    
  48. def main():
  49.     url = r"https://dl.dropboxusercontent.com/s/nq4p7klj6myaqea/python_2018.mp4?dl=0"
  50.     target = datetime.datetime(2018, 1, 1, 00, 00, 00)
  51.     downloaded = False
  52.     print("Brought to you by Monro Coury... Happy New Year!")
  53.     while True:
  54.         try:
  55.             df = target - datetime.datetime.now()
  56.            
  57.             sys.stdout.write(count_down(target))
  58.             time.sleep(1)
  59.             sys.stdout.flush()
  60.            
  61.             if df.total_seconds() <= 300 and not downloaded:
  62.                 th = Thread(target=download_file, args= (url, "python_2018.mp4", ))
  63.                 th.start()
  64.                 downloaded = True
  65.  
  66.             if df.total_seconds() <= 4 and downloaded:
  67.                 play_video("python_2018.mp4")
  68.                 break
  69.  
  70.         except KeyboardInterrupt:
  71.             sys.stdout.write("\nCouldn't wait eh? ._.")
  72.             break
  73.  
  74. main()
Advertisement
Add Comment
Please, Sign In to add comment