Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
diezo committed Nov 18, 2023
1 parent d18cf01 commit 21bd829
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 81 deletions.
25 changes: 3 additions & 22 deletions ensta/BaseHost.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
from collections.abc import Generator
from .lib.Commons import (
refresh_csrf_token,
update_app_id,
update_homepage_source,
update_session,
format_identifier,
format_url,
format_username
Expand Down Expand Up @@ -40,7 +37,7 @@
class BaseHost:
request_session: requests.Session = None
homepage_source: str = None
insta_app_id: str = None
insta_app_id: str = "936619743392459"
preferred_color_scheme: str = "dark"
x_ig_www_claim: str = None
csrf_token: str = None
Expand All @@ -49,32 +46,16 @@ class BaseHost:

def __init__(self, session_id: str, proxy: dict[str, str] | None = None) -> None:
self.x_ig_www_claim = "hmac." + "".join(random.choices(string.ascii_letters + string.digits + "_-", k=48))
update_session(self)
self.request_session = requests.Session()

if proxy is not None: self.request_session.proxies.update(proxy)

update_homepage_source(self)
update_app_id(self)

self.guest = Guest(
homepage_source=self.homepage_source,
app_id=self.insta_app_id,
proxy=proxy
)

self.guest = Guest(proxy=proxy)
self.request_session.cookies.set("sessionid", session_id)

if not self.authenticated():
raise SessionError("SessionID is incorrect or expired.")

def update_homepage_source(self) -> None:
temp_homepage_source = self.request_session.get("https://www.instagram.com/").text.strip()

if temp_homepage_source == "":
raise NetworkError("Couldn't load instagram homepage.")

self.homepage_source = temp_homepage_source

def authenticated(self) -> bool:
refresh_csrf_token(self)
request_headers = {
Expand Down
22 changes: 6 additions & 16 deletions ensta/Guest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from json import JSONDecodeError
import random
import requests
from .lib.Commons import (
update_session,
update_homepage_source,
update_app_id,
refresh_csrf_token,
format_username,
format_uid
Expand All @@ -19,20 +15,14 @@
class Guest:
request_session: requests.Session = None
homepage_source: str = None
insta_app_id: str = None
insta_app_id: str = "936619743392459"
preferred_color_scheme: str = "dark"
csrf_token: str = None

def __init__(self, homepage_source: str = None, app_id: str | int = None, proxy: dict[str, str] | None = None) -> None:
update_session(self)

def __init__(self, proxy: dict[str, str] | None = None) -> None:
self.request_session = requests.Session()
if proxy is not None: self.request_session.proxies.update(proxy)

if homepage_source is not None: self.homepage_source = homepage_source
else: update_homepage_source(self)

if app_id is not None: self.insta_app_id = str(app_id)
else: update_app_id(self)

def username_availability(self, username: str) -> bool | None:
username = format_username(username)
refresh_csrf_token(self)
Expand All @@ -47,7 +37,7 @@ def username_availability(self, username: str) -> bool | None:
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/x-www-form-urlencoded",
"sec-ch-prefers-color-scheme": random.choice(["light", "dark"]),
"sec-ch-prefers-color-scheme": self.preferred_color_scheme,
"sec-ch-ua": ua,
"sec-ch-ua-full-version-list": ua,
"sec-ch-ua-mobile": "?0",
Expand Down Expand Up @@ -83,7 +73,7 @@ def profile(self, username: str, __session__: requests.Session | None = None) ->
request_headers: dict = {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"sec-ch-prefers-color-scheme": random.choice(["light", "dark"]),
"sec-ch-prefers-color-scheme": self.preferred_color_scheme,
"sec-ch-ua": ua,
"sec-ch-ua-full-version-list": ua,
"sec-ch-ua-mobile": "?0",
Expand Down
39 changes: 0 additions & 39 deletions ensta/lib/Commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
import requests
import requests.cookies
from requests.cookies import RequestsCookieJar
from .Exceptions import NetworkError


def update_app_id(self) -> None:
app_id_occurrence_string = "\"APP_ID\":\""
app_id_first_occurrence = self.homepage_source.index(app_id_occurrence_string)
app_id_raw_text = self.homepage_source[
app_id_first_occurrence + len(app_id_occurrence_string): app_id_first_occurrence + 30]
self.insta_app_id = app_id_raw_text[: app_id_raw_text.index("\"")]


def refresh_csrf_token(self) -> None:
Expand All @@ -31,36 +22,6 @@ def refresh_csrf_token(self) -> None:
cookie_jar.set(key, final_cookies[key])


def update_session(self) -> None:
self.request_session = requests.Session()


def update_homepage_source(self) -> None:
request_headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "en-US,en;q=0.9",
"sec-ch-prefers-color-scheme": random.choice(["light", "dark"]),
"sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
"sec-ch-ua-full-version-list": "\"Not.A/Brand\";v=\"8.0.0.0\", \"Chromium\";v=\"114.0.5735.110\", \"Google Chrome\";v=\"114.0.5735.110\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-ch-ua-platform-version": "\"15.0.0\"",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"viewport-width": "1475",
"Referrer-Policy": "strict-origin-when-cross-origin"
}
temp_homepage_source = self.request_session.get("https://www.instagram.com/", headers=request_headers).text.strip()

if temp_homepage_source != "":
self.homepage_source = temp_homepage_source
else:
raise NetworkError("Couldn't load instagram homepage.")


def format_username(username: str) -> str:
return username.replace(" ", "").lower()

Expand Down
3 changes: 0 additions & 3 deletions ensta/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from .Commons import (
update_session,
update_homepage_source,
update_app_id,
refresh_csrf_token,
format_username,
format_uid,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path

long_description = (Path(__file__).parent / "README.md").read_text(encoding="utf-8")
version = "5.0.1"
version = "5.0.2"

setup(
name="ensta",
Expand Down

0 comments on commit 21bd829

Please sign in to comment.