2024-11-22 02:14:01 +08:00
|
|
|
import os
|
2024-12-18 03:06:40 +08:00
|
|
|
import logging
|
2024-11-22 02:14:01 +08:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from instagrapi import Client
|
|
|
|
|
|
|
|
from utils.tbProcessor import easyExceptionHandler
|
|
|
|
from utils.const import DEVICE
|
|
|
|
|
2024-12-18 03:06:40 +08:00
|
|
|
# logging
|
|
|
|
iglog = logging.getLogger("ig")
|
|
|
|
iglog.setLevel(level=logging.DEBUG)
|
|
|
|
|
2024-12-18 00:35:17 +08:00
|
|
|
cl:Client = None
|
|
|
|
|
|
|
|
# init
|
|
|
|
def init(askcl:Client) -> None:
|
|
|
|
global cl
|
|
|
|
cl = askcl
|
|
|
|
|
|
|
|
|
2024-11-22 02:14:01 +08:00
|
|
|
# login
|
|
|
|
def login() -> int:
|
|
|
|
# Env
|
|
|
|
ACCOUNT_USERNAME = os.getenv("ACCOUNT_USERNAME", None).strip()
|
|
|
|
ACCOUNT_PASSWORD = os.getenv("ACCOUNT_PASSWORD", None).strip()
|
|
|
|
|
|
|
|
# session
|
|
|
|
session = None
|
|
|
|
if os.path.exists("./session.json"):
|
|
|
|
session = cl.load_settings("session.json")
|
|
|
|
|
|
|
|
cl.delay_range = [2, 5]
|
|
|
|
# login with sessionid
|
|
|
|
cl.set_device(DEVICE)
|
|
|
|
sessionSuccess = True
|
|
|
|
if session:
|
2024-12-18 03:06:40 +08:00
|
|
|
iglog.info("Trying logging in with session")
|
2024-11-22 02:14:01 +08:00
|
|
|
try:
|
|
|
|
cl.set_settings(session)
|
|
|
|
cl.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)
|
|
|
|
cl.get_timeline_feed()
|
|
|
|
except:
|
|
|
|
sessionSuccess = False
|
|
|
|
else:
|
|
|
|
sessionSuccess = False
|
|
|
|
|
|
|
|
# login with username and password
|
|
|
|
if not sessionSuccess:
|
2024-12-18 03:06:40 +08:00
|
|
|
iglog.info("Trying logging in with username and password")
|
2024-11-22 02:14:01 +08:00
|
|
|
try:
|
|
|
|
old_session = cl.get_settings()
|
|
|
|
cl.set_settings({})
|
|
|
|
cl.set_uuids(old_session["uuids"])
|
|
|
|
cl.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)
|
|
|
|
cl.get_timeline_feed()
|
|
|
|
except:
|
2024-12-18 03:06:40 +08:00
|
|
|
iglog.error("Cannot log in")
|
2024-11-22 02:14:01 +08:00
|
|
|
return 0
|
|
|
|
|
|
|
|
cl.dump_settings("session.json")
|
|
|
|
|
|
|
|
# return
|
|
|
|
username = cl.account_info().dict()["username"]
|
2024-12-18 03:06:40 +08:00
|
|
|
iglog.info("Logged as %s"%username)
|
2024-11-22 02:14:01 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
# Get account info
|
|
|
|
def account_info() -> dict | None:
|
2024-12-18 03:06:40 +08:00
|
|
|
iglog.info("Fetching account info")
|
2024-11-22 02:14:01 +08:00
|
|
|
try:
|
|
|
|
info = cl.account_info().dict()
|
|
|
|
return info
|
|
|
|
except Exception as e:
|
|
|
|
easyExceptionHandler(e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# Get media info
|
|
|
|
def media_info(code:str) -> dict | None:
|
|
|
|
try:
|
|
|
|
pk = cl.media_pk_from_code(code)
|
|
|
|
info = cl.media_info(pk).dict()
|
|
|
|
return info
|
|
|
|
except Exception as e:
|
|
|
|
easyExceptionHandler(e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# Upload media
|
2024-12-18 00:35:17 +08:00
|
|
|
def upload_media(content:str, paths:List[str]) -> dict | None:
|
2024-11-22 02:14:01 +08:00
|
|
|
try:
|
|
|
|
# uplaod
|
2024-12-18 03:06:40 +08:00
|
|
|
if len(paths) == 0:
|
|
|
|
return None
|
2024-11-22 02:14:01 +08:00
|
|
|
elif len(paths) == 1:
|
2024-12-18 00:35:17 +08:00
|
|
|
media = cl.photo_upload(path=paths[0], caption=content).dict()
|
2024-11-22 02:14:01 +08:00
|
|
|
else:
|
2024-12-18 03:06:40 +08:00
|
|
|
media = cl.album_upload(paths=paths, caption=content).dict()
|
2024-11-22 02:14:01 +08:00
|
|
|
|
|
|
|
return media
|
|
|
|
except Exception as e:
|
|
|
|
easyExceptionHandler(e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# Delete Media
|
|
|
|
def delete_media(code:str) -> int:
|
|
|
|
try:
|
|
|
|
media_pk = str(cl.media_pk_from_code(code))
|
|
|
|
media_id = cl.media_id(media_pk)
|
|
|
|
cl.media_delete(media_id)
|
|
|
|
return 0
|
|
|
|
except Exception as e:
|
|
|
|
easyExceptionHandler(e)
|
2024-12-18 03:06:40 +08:00
|
|
|
return 1
|