Created
June 11, 2021 15:28
-
-
Save nsrCodes/ce3749069db9b679bb2ef53088c4c092 to your computer and use it in GitHub Desktop.
Setup daily post using linux cron jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
Cron job needs to be setup to run everytime PC boots. | |
Script should be in the same file as all the pictures. | |
Pictures should be named 1,2,3... and the captions json should have captions with those numbers as keys. | |
``` | |
import json | |
from instabot import Bot | |
from datetime import date | |
import requests | |
status_path = "/home/razor/main/funProject/experimenting/insta/v1/status.json" # FILE SAVING DAILY RUNNING INFO | |
captions_path = "/home/razor/main/funProject/experimenting/insta/v1/captions.json" # JSON FILE WITH PRE WRITTEN HASTAGS | |
hashtags_path = "/home/razor/main/funProject/experimenting/insta/v1/hashtags.txt" # FILW WITH ALL THE HASTAGS | |
# Function to upload post | |
def upload(num, caption): | |
bot = Bot() | |
bot.login(username = "YOUR USERNAME", | |
password = "YOUR PASSWORD") | |
space = "\n.\n.\n.\n.\n.\n.\n" | |
hashtag_file = open(hashtags_path) | |
hashtags = hashtag_file.read() | |
bot.upload_photo(f"/home/razor/main/funProject/experimenting/insta/v1/{num}.jpg", caption = caption + space + hashtags) | |
# print(caption + space + hashtags) | |
# Function to notify when post is uploaded | |
def notify(num, caption): | |
url = "DISCORD WEBHOOK URL FOR NOTIFYING WHEN POSTED" #webhook url, from here: https://i.imgur.com/aT3AThK.png | |
data = {} | |
#for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook | |
data["content"] = "New Post Uploaded" | |
data["username"] = "Insta alert bot" | |
#leave this out if you dont want an embed | |
data["embeds"] = [] | |
embed = {} | |
#for all params, see https://discordapp.com/developers/docs/resources/channel#embed-object | |
embed["description"] = caption | |
embed["title"] = f"Counter #{num}" | |
# Getting posted image from inta open endpoint | |
insta_acc_detail_endpoint = "https://www.instagram.com/<USERNAME>/?__a=1" | |
insta_data = requests.get(insta_acc_detail_endpoint) | |
json_data = json.loads(insta_data.text) | |
embed["image"] = { | |
"url" : json_data["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["display_url"] | |
} | |
data["embeds"].append(embed) | |
result = requests.post(url, data=json.dumps(data), headers={"Content-Type": "application/json"}) | |
try: | |
result.raise_for_status() | |
except requests.exceptions.HTTPError as err: | |
# print(err) | |
pass | |
else: | |
# print("Payload delivered successfully, code {}.".format(result.status_code)) | |
pass | |
def main(): | |
# getting status | |
status = open(status_path) | |
status_data = json.load(status) | |
status.close() | |
# getting todays date | |
today = date.today() | |
if str(today) != status_data["date"]: | |
# Get counter | |
counter = str(int(status_data["counter"]) + 1) | |
# print(counter) | |
# Get all captions to check if there is something for today | |
captions = open(captions_path) | |
caption_data = json.load(captions) | |
captions.close() | |
# Checking if there is a pic to upload at the given counter | |
if str(counter) in caption_data.keys(): | |
upload(counter, caption_data[str(counter)]) | |
notify(counter, caption_data[str(counter)]) | |
# Updating status details | |
status_data["counter"] = counter | |
status_data["date"] = str(today) | |
status = open(status_path, "w") | |
json.dump(status_data, status) | |
status.close() | |
print(f"Successfully ran on {str(today)} with final counter {counter}") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment