Skip to content

Commit

Permalink
Improve Instaloder Login
Browse files Browse the repository at this point in the history
  • Loading branch information
JoTec2002 committed Nov 11, 2024
1 parent f43f7f9 commit b60e9a8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ create a new recipe with associated image or video assets.
3. Generate a Instagram Session File (!thats the most tricky step). A Helper Script is
provided https://github.com/JoTec2002/InstagramToMealie/blob/main/helpers/instaloader_login_helper.py ! It's just
copied from the [Instaloader Docs](https://instaloader.github.io/troubleshooting.html).
1. Download the
script: https://raw.githubusercontent.com/JoTec2002/InstagramToMealie/refs/heads/main/helpers/instaloader_login_helper.py
2. Login to Instagram in Firefox
3. Execute the snippet, e.g. with python instaloader_login_helper.py
4. Copy the File that was generated by the Script to a known location. (this file will later be mounted to the
docker container. It can be generated on a different System and than copied to the target System)

### Installation

Expand Down Expand Up @@ -80,7 +86,7 @@ Install InstagramToMealie using one of the following methods:

Deploy it via docker-compose allongside your mealie installation

Docker compose example:
Docker compose example (With Instagram Session File):

```yaml
services:
Expand All @@ -96,14 +102,35 @@ services:
INSTA_USER: "instagram username"
MEALIE_API_KEY: "MEALIE API KEY"
MEALIE_URL: "YOU LOCAL MEALIE INSTALLATION"
MEALIE_OPENAI_REQUEST_TIMEOUT: 60
MEALIE_OPENAI_REQUEST_TIMEOUT: 60 #optional default 60
volumes:
- "./session-file:/app/session-file" #The instagram session file you created in the prerequisits
depends_on:
mealie:
condition: service_healthy
```
Docker compose example (With Instagram username and password)<br>!!! On the Instagram Account 2FA MUST be disabled. You
Probably need multiple attempts to get ths working. Login on other Systems parallel to not trip the instagram bot
detector. This is not Recommended!
````yaml
services:
InstagramToMealie:
build:
context: .
dockerfile: Dockerfile
image: instagramtomealie:latest
ports:
- "9001:9001"
environment:
INSTA_USER: "instagram username"
MEALIE_API_KEY: "MEALIE API KEY"
MEALIE_URL: "YOU LOCAL MEALIE INSTALLATION"
MEALIE_OPENAI_REQUEST_TIMEOUT: 60 #optional default 60
INSTA_PWD: "Cleartext Instagram Password"
````
---
## Contributing
Expand Down
17 changes: 12 additions & 5 deletions helpers/instadownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
import instaloader
import pyotp
from instaloader import Post, TwoFactorAuthRequiredException
from instaloader import Post, TwoFactorAuthRequiredException, BadCredentialsException


class InstaDownloader:
Expand All @@ -13,11 +13,18 @@ def __init__(self):
dirname_pattern="downloads/{target}", )
try:
user = os.environ.get('INSTA_USER')
# self.loader.login(os.environ.get("INSTA_USER"), os.environ.get("INSTA_PWD"))
self.loader.load_session_from_file(user, "./session-file")
except TwoFactorAuthRequiredException:
if os.path.isfile("./session-file"):
self.loader.load_session_from_file(user, "./session-file")
else:
self.loader.login(os.environ.get("INSTA_USER"), os.environ.get("INSTA_PWD"))
except TwoFactorAuthRequiredException: # Probably not going to work https://github.com/instaloader/instaloader/issues/1217
print(os.environ.get("INSTA_TOTP_SECRET"))
totp = pyotp.TOTP(os.environ.get("INSTA_TOTP_SECRET"))
self.loader.two_factor_login(totp.now())
print(totp.now())
try:
self.loader.two_factor_login(totp.now())
except BadCredentialsException:
self.loader.two_factor_login(totp.now())

print(self.loader.test_login())

Expand Down
11 changes: 9 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@
if os.path.isfile("./session-file"):
print("Session file exists")
else:
print("Session file does not exist")
exit(1)
if "INSTA_PWD" in os.environ:
if "INSTA_TOTP_SECRET" in os.environ:
print("Insta PWD and TOTP SECRET are set in ENV - trying to login but will possibly fail")
else:
print(
"Insta PWD is set but TOTP SECRET is not set - continue but will fail if 2FA is configured for instagram account")
else:
print("Neither session-file nor Insta PWD is configured - recomending session file")
exit(1)

if "MEALIE_OPENAI_REQUEST_TIMEOUT" in os.environ:
print(os.environ.get("MEALIE_OPENAI_REQUEST_TIMEOUT"))
Expand Down

0 comments on commit b60e9a8

Please sign in to comment.