Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. from Instagram.config import PATH_TO_IMAGES, PATH_TO_ARCHIVE, PATH_TO_VIDEOS
  2. from Instagram.config import engagement_matrix, POST_PER_DAY
  3. import datetime
  4. import random
  5.  
  6.  
  7. class ScheduledPoster:
  8.  
  9.     def __init__(self, Account):
  10.         assert hasattr(Account, "api")
  11.  
  12.         self.Account = Account
  13.         self.active = True
  14.  
  15.     def run(self):
  16.         """ Starts the ScheduledPoster """
  17.  
  18.         day = None
  19.         self.post_times = self.get_post_times()
  20.  
  21.         while self.active:
  22.  
  23.             if day is not self.UTCdate.day:
  24.                 """ Day has passed, get new post times """
  25.  
  26.                 hour = None
  27.                 day = self.UTCdate.day
  28.                 self.post_times = self.get_post_times()
  29.  
  30.             if self.UTCdate.hour in self.post_times and hour is not self.UTCdate.hour:
  31.                 """ Time to Post media """
  32.  
  33.                 hour = self.UTCdate.hour
  34.                 media = self.get_media()
  35.  
  36.                 if media is None:
  37.                     return
  38.  
  39.                 self.post(media)
  40.                 self.post_times.remove(self.UTCdate.hour)
  41.  
  42.     @property
  43.     def UTCdate(self):
  44.         """ Returns then current datetime.now object for the UTC timezone """
  45.  
  46.         return datetime.datetime.now(datetime.timezone.utc)
  47.  
  48.     def get_post_times(self):
  49.         """ Returns a list of len POST_PER_DAY containing semi-random integers
  50.            representing hour of the day """
  51.  
  52.         return random.sample(engagement_matrix[self.UTCdate.day], random.choice(POST_PER_DAY))
  53.  
  54.     def get_caption(self, media, tag_original_poster=True, tag_self=True):
  55.         """ Returns a fitting random caption for the media """
  56.  
  57.         caption = ""
  58.  
  59.         if tag_self is True:
  60.             caption = caption + " \n @{0}".format(self.Account.username)
  61.  
  62.         return caption
  63.  
  64.     def get_media(self, type="image"):
  65.         """ Finds postable media from image or video storage """
  66.  
  67.         if type == "video":
  68.             f = open(PATH_TO_VIDEOS, 'r')
  69.         elif type == "image":
  70.             f = open(PATH_TO_IMAGES, 'r')
  71.         else:
  72.             return None
  73.  
  74.         try:
  75.             medias = [media for media in f.readlines() if media not in open(PATH_TO_ARCHIVE, 'r').readlines()]
  76.         except Exception as e:
  77.             print(str(e))
  78.  
  79.         f.close()
  80.  
  81.         if medias is None:
  82.             return None
  83.  
  84.         return random.choice(medias)
  85.  
  86.     def post(self, media):
  87.         """ Posts given media on ScheduledPosters' linked account.
  88.            Allows for custom captions and different post settings"""
  89.  
  90.         media.caption = self.get_caption()
  91.  
  92.         if media.type == "video":
  93.             try:
  94.                 self.Account.api.post_video(*media.data)
  95.             except Exception as e:
  96.                 print(str(e))
  97.         elif media.type == "image":
  98.             try:
  99.                 self.Account.api.post_photo(*media.data)
  100.             except Exception as e:
  101.                 print(str(e))
  102.         else:
  103.             print("Media doesnt have a supported type")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement