Use aiograpi instead of instagrapi

This commit is contained in:
Oliver Hattshire 2025-10-27 00:32:58 -03:00
parent 742f5b4b21
commit e2a58b7216
5 changed files with 2062 additions and 19 deletions

View file

@ -26,7 +26,7 @@ custom_readme_tail: ''
debian_bundle: true debian_bundle: true
dependencies: dependencies:
- slidge>=0.3,<0.4 - slidge>=0.3,<0.4
- instagrapi>=2.2,<3 - aiograpi@git+https://github.com/subzeroid/aiograpi@965689e
dev_dependencies: [] dev_dependencies: []
intersphinx_mappings: [] intersphinx_mappings: []
lint_excludes: [] lint_excludes: []
@ -42,8 +42,8 @@ repology: ''
requires_lottie: false requires_lottie: false
silo: silo:
lib: lib:
name: instagrapi name: aiograpi
url: https://github.com/subzeroid/instagrapi url: https://github.com/subzeroid/aiograpi
name: Instagram name: Instagram
url: https://instagram.com/ url: https://instagram.com/
uv_sources: [] uv_sources: []

View file

@ -1,14 +1,15 @@
import asyncio import asyncio
from instagrapi import Client from aiograpi import Client
from instagrapi.exceptions import ( from aiograpi.exceptions import (
BadPassword, BadPassword,
ChallengeRequired, ChallengeRequired,
FeedbackRequired, FeedbackRequired,
TwoFactorRequired, TwoFactorRequired,
) )
from slidge import BaseGateway, GatewayUser from slidge import BaseGateway
from slidge.command.register import RegistrationType, TwoFactorNotRequired from slidge.command.register import RegistrationType, TwoFactorNotRequired
from slidge.db import GatewayUser
from slixmpp import JID from slixmpp import JID
from .utils import get_session_file from .utils import get_session_file
@ -35,7 +36,7 @@ class Gateway(BaseGateway):
session_file = get_session_file(user_jid.bare) session_file = get_session_file(user_jid.bare)
client = Client() client = Client()
try: try:
_ = await asyncio.to_thread(client.login, await client.login(
registration_form["username"], registration_form["username"],
registration_form["password"] registration_form["password"]
) )
@ -50,22 +51,26 @@ class Gateway(BaseGateway):
except Exception as e: except Exception as e:
raise ValueError("Could not authenticate: %s - %s", e, e.args) raise ValueError("Could not authenticate: %s - %s", e, e.args)
else: else:
_ = await asyncio.to_thread(client.dump_settings, session_file) await self._save_settings(client, session_file)
raise TwoFactorNotRequired raise TwoFactorNotRequired
async def validate_two_factor_code(self, user: GatewayUser, code: str): async def validate_two_factor_code(self, user: GatewayUser, code: str):
session_file = get_session_file(user.jid.bare) session_file = get_session_file(user.jid.bare)
client = self.instagram_client[user.jid.bare] client = self.instagram_client[user.jid.bare]
try: try:
if await asyncio.to_thread(client.login, if await client.login(
user.legacy_module_data["username"], user.legacy_module_data["username"],
user.legacy_module_data["password"], user.legacy_module_data["password"],
verification_code=code): verification_code=code):
if not await asyncio.to_thread(client.dump_settings, session_file): await self._save_settings(client, session_file)
raise IOError("Could not save session file: %s - %s", session_file)
except ChallengeRequired as e: except ChallengeRequired as e:
raise ValueError("Browser Challenge Required", e, e.args) raise ValueError("Browser Challenge Required", e, e.args)
except FeedbackRequired as e: except FeedbackRequired as e:
raise ValueError("Action moderated, account may be blocked", e, e.args) raise ValueError("Action moderated, account may be blocked", e, e.args)
except Exception as e: except Exception as e:
raise ValueError("Could not authenticate: %s - %s", e, e.args) raise ValueError("Could not authenticate: %s - %s", e, e.args)
@staticmethod
async def _save_settings(client: Client, session_file: str):
if not await asyncio.to_thread(client.dump_settings, session_file):
raise IOError("Could not save session file: %s - %s", session_file)

View file

@ -1,6 +1,6 @@
import asyncio import asyncio
import instagrapi import aiograpi
from slidge import BaseSession from slidge import BaseSession
from .utils import get_session_file from .utils import get_session_file
@ -12,14 +12,12 @@ class Session(BaseSession):
super().__init__(*a, **kw) super().__init__(*a, **kw)
async def login(self): async def login(self):
client = instagrapi.Client() self.client = aiograpi.Client()
session_file = get_session_file(self.user_jid.bare) session_file = get_session_file(self.user_jid.bare)
client.load_settings(session_file) await asyncio.to_thread(self.client.load_settings, session_file)
await asyncio.to_thread(client.login, await self.client.login(
self.user.legacy_module_data["username"], self.user.legacy_module_data["username"],
self.user.legacy_module_data["password"]) self.user.legacy_module_data["password"])
self.log.info( return f"Connected as '{self.client.user_id} <{self.client.username}>'"
"Logged in: %s", str(self.user_jid.bare)
)

View file

@ -13,8 +13,8 @@ requires-python = ">= 3.11"
keywords = ["xmpp", "chat", "instagram", "gateway", "bridge", "instant messaging"] keywords = ["xmpp", "chat", "instagram", "gateway", "bridge", "instant messaging"]
version = "0.0.0a1" version = "0.0.0a1"
dependencies = [ dependencies = [
"aiograpi@git+https://github.com/subzeroid/aiograpi@965689e",
"slidge>=0.3,<0.4", "slidge>=0.3,<0.4",
"instagrapi>=2.2,<3",
] ]
[project.scripts] [project.scripts]
@ -39,6 +39,9 @@ include = ["Insthidge"]
name = "pypi" name = "pypi"
url = "https://pypi.org/simple" url = "https://pypi.org/simple"
[tool.uv.sources]
aiograpi = { git = "https://github.com/subzeroid/aiograpi", rev = "965689e" }
[tool.mypy] [tool.mypy]
files = ["Insthidge"] files = ["Insthidge"]
check_untyped_defs = true check_untyped_defs = true

2037
uv.lock generated Normal file

File diff suppressed because it is too large Load diff