Replies: 1 comment
-
I added print statements and the error seems to occur when "m = cl.media_info_v1(Mlpk)" is run. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Not sure why I am getting this as it was working recently.
I am doing the login correctly as per the repository. Using the sessions.json file and the login function given.
I'll attach all of my code here.
And the traceback after it.
CODE:
from instagrapi import Client
import time
import os
import datetime
from instagrapi.exceptions import LoginRequired
import logging
credentialsPath = "/Users/Shared/InstagramAutomated/credentials.txt"
with open(credentialsPath, "r") as f:
USERNAME, PASSWORD = f.read().splitlines()
cl = Client()
logger = logging.getLogger()
def login_user():
"""
Attempts to login to Instagram using either the provided session information
or the provided username and password.
"""
login_user()
runtime = time.time()
notYet = False
while notYet == False:
if runtime < 15:
notYet = False
else:
notYet = True
start = time.time()
print("Code is running and being timed. Please wait....")
file_path = '/Users/Shared/InstagramAutomated/runFile.txt'
x = datetime.datetime.now()
with open(file_path, 'w') as file:
content = f"File ran at {x}"
file.write(content)
file.close()
usernames = [
"hannesribbner",
"teamlyonfish",
"captnickstanczyk",
"ariatunafishing",
"fv_whiskeybusiness",
"highlymigratoryfishing",
"joevt_fishing",
"reeladdictioniiisportfishing",
"almcglashan",
"dennis_verreet",
"david_lassley",
"high_seas_hawaii",
"bhagz_fishing",
]
print('Successful Login')
ids = []
id_dict = {}
PKDict = {}
PKList = []
max_likes = 0
primaryKeysInFile = []
def create_user_id_list(usernames):
for i in usernames:
ids.append(cl.user_id_from_username(i))
def create_user_id_dict(usernames):
for i in range(len(usernames)):
id_dict[usernames[i]] = ids[i]
def collect_like_count():
media = []
for i in ids:
media += cl.user_medias(i, 9)
for i in range(len(media)):
media_dict = media[i].dict()
likeCount = media_dict['like_count']
PKDict[media_dict['pk']] = likeCount
def most_liked_PK():
max_likes = 0
primaryKeyPath = "/Users/Shared/InstagramAutomated/primarykeys.txt"
def prevent_repost(mostLikedPK):
with open(primaryKeyPath, "r") as f:
primaryKeysInFile = f.read().splitlines()
create_user_id_list(usernames)
print('1')
create_user_id_dict(usernames)
print('2')
collect_like_count()
print('3')
MLPK = most_liked_PK()
print('4')
MLPK = prevent_repost(MLPK)
print('5')
media_type = cl.media_info(MLPK).dict()['media_type']
product_type = cl.media_info(MLPK).dict()['product_type']
m = cl.media_info_v1(MLPK)
url = m.video_url
print(m,url)
downloadPath = "/Users/Shared/InstagramAutomated/downloadedPosts"
print("media type: " + str(media_type))
print("Attempting to download...")
if media_type == 1: #picture
# cl.photo_download(MLPK, downloadPath)
p = cl.photo_download_by_url(url,downloadPath)
if media_type == 2 and product_type == 'clips':
p = cl.clip_download_by_url(url,downloadPath) #Reel
# cl.clip_download(MLPK, downloadPath)
if media_type == 2 and product_type == 'feed': #Video
# cl.video_download(MLPK, downloadPath)
p = cl.video_download_by_url(url,downloadPath)
if media_type == 2 and product_type == 'igtv': #IGTV
# cl.igtv_download(MLPK,downloadPath)
p = cl.igtv_download_by_url(url,downloadPath)
if media_type == 8: #Album
# cl.album_download(MLPK, downloadPath)
p = cl.album_download_by_urls(url,downloadPath)
poster = "@" + str(cl.media_user(MLPK).dict()['username'])
location = cl.media_info(MLPK).dict()['location']
if location != None:
location = str(location['name'])
caption = "Check out this post by " + poster + " out near " + location + "! \nTag your Friends and comment below! \n . \n . \n . \n . \n . \n . \n . \n . \n #billfish #bluemarlin #sailfish #centerconsole #simrad #furuno #sailfishing #fishing #gamefish #sportfishing #pelagicgirl #pelagicworldwide #fishingteam #tuna #bluefin #jigging #trolling #offshore #swordfish #panamafishing #spearfishing #bahamasfishing #catchandcook #saltlife #catchoftheday #pescasub #floridawildlife #floridakeys"
else:
caption = f"Check out this post by {poster}! \nTag your Friends and comment below! \n . \n . \n . \n . \n . \n . \n . \n . \n #billfish #bluemarlin #sailfish #centerconsole #simrad #furuno #sailfishing #fishing #gamefish #sportfishing #pelagicgirl #pelagicworldwide #fishingteam #tuna #bluefin #jigging #trolling #offshore #swordfish #panamafishing #spearfishing #bahamasfishing #catchandcook #saltlife #catchoftheday #pescasub #floridawildlife #floridakeys"
files = os.listdir(downloadPath)
print()
print("Attempting to Upload...")
if media_type == 1: #picture
cl.photo_upload(p, caption)
if media_type == 2 and product_type == 'clips': #Reel
cl.clip_upload(p, caption)
if media_type == 2 and product_type == 'feed': #Video
cl.video_upload(p, caption)
if media_type == 2 and product_type == 'igtv': #IGTV
cl.igtv_upload(p, caption)
if media_type == 8: #Album
cl.album_upload(p, caption)
os.remove(p)
print("File Deleted")
cl.logout()
end = time.time()
t = end - start
print(f"Ran in: {t.round(2)} seconds")
Traceback:
Traceback (most recent call last):
File "/Users/nicolasrulli/Library/Python/3.9/lib/python/site-packages/instagrapi/mixins/private.py", line 360, in _send_private_request
response.raise_for_status()
File "/Users/nicolasrulli/Library/Python/3.9/lib/python/site-packages/requests/models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://i.instagram.com/api/v1/media/3150637460300994484/info/
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/Shared/InstagramAutomated/file2.py", line 183, in
m = cl.media_info_v1(MLPK)
File "/Users/nicolasrulli/Library/Python/3.9/lib/python/site-packages/instagrapi/mixins/media.py", line 236, in media_info_v1
raise e
File "/Users/nicolasrulli/Library/Python/3.9/lib/python/site-packages/instagrapi/mixins/media.py", line 230, in media_info_v1
result = self.private_request(f"media/{media_pk}/info/")
File "/Users/nicolasrulli/Library/Python/3.9/lib/python/site-packages/instagrapi/mixins/private.py", line 535, in private_request
raise e
File "/Users/nicolasrulli/Library/Python/3.9/lib/python/site-packages/instagrapi/mixins/private.py", line 520, in private_request
self._send_private_request(endpoint, **kwargs)
File "/Users/nicolasrulli/Library/Python/3.9/lib/python/site-packages/instagrapi/mixins/private.py", line 384, in _send_private_request
raise LoginRequired(response=e.response, **last_json)
instagrapi.exceptions.LoginRequired: login_required
Let me know what is causing this.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions