Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Instagram.config import PATH_TO_IMAGES, PATH_TO_ARCHIVE, PATH_TO_VIDEOS
- from Instagram.config import engagement_matrix, POST_PER_DAY
- import datetime
- import random
- class ScheduledPoster:
- def __init__(self, Account):
- assert hasattr(Account, "api")
- self.Account = Account
- self.active = True
- def run(self):
- """ Starts the ScheduledPoster """
- day = None
- self.post_times = self.get_post_times()
- while self.active:
- if day is not self.UTCdate.day:
- """ Day has passed, get new post times """
- hour = None
- day = self.UTCdate.day
- self.post_times = self.get_post_times()
- if self.UTCdate.hour in self.post_times and hour is not self.UTCdate.hour:
- """ Time to Post media """
- hour = self.UTCdate.hour
- media = self.get_media()
- if media is None:
- return
- self.post(media)
- self.post_times.remove(self.UTCdate.hour)
- @property
- def UTCdate(self):
- """ Returns then current datetime.now object for the UTC timezone """
- return datetime.datetime.now(datetime.timezone.utc)
- def get_post_times(self):
- """ Returns a list of len POST_PER_DAY containing semi-random integers
- representing hour of the day """
- return random.sample(engagement_matrix[self.UTCdate.day], random.choice(POST_PER_DAY))
- def get_caption(self, media, tag_original_poster=True, tag_self=True):
- """ Returns a fitting random caption for the media """
- caption = ""
- if tag_self is True:
- caption = caption + " \n @{0}".format(self.Account.username)
- return caption
- def get_media(self, type="image"):
- """ Finds postable media from image or video storage """
- if type == "video":
- f = open(PATH_TO_VIDEOS, 'r')
- elif type == "image":
- f = open(PATH_TO_IMAGES, 'r')
- else:
- return None
- try:
- medias = [media for media in f.readlines() if media not in open(PATH_TO_ARCHIVE, 'r').readlines()]
- except Exception as e:
- print(str(e))
- f.close()
- if medias is None:
- return None
- return random.choice(medias)
- def post(self, media):
- """ Posts given media on ScheduledPosters' linked account.
- Allows for custom captions and different post settings"""
- media.caption = self.get_caption()
- if media.type == "video":
- try:
- self.Account.api.post_video(*media.data)
- except Exception as e:
- print(str(e))
- elif media.type == "image":
- try:
- self.Account.api.post_photo(*media.data)
- except Exception as e:
- print(str(e))
- else:
- print("Media doesnt have a supported type")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement