|
| 1 | +import logging |
| 2 | +from urllib.parse import urlparse |
| 3 | + |
| 4 | +import urllib3 |
| 5 | +from urllib3.exceptions import InsecureRequestWarning |
| 6 | + |
| 7 | +from instagrapi.mixins.account import AccountMixin |
| 8 | +from instagrapi.mixins.album import DownloadAlbumMixin, UploadAlbumMixin |
| 9 | +from instagrapi.mixins.auth import LoginMixin |
| 10 | +from instagrapi.mixins.bloks import BloksMixin |
| 11 | +from instagrapi.mixins.challenge import ChallengeResolveMixin |
| 12 | +from instagrapi.mixins.clip import DownloadClipMixin, UploadClipMixin |
| 13 | +from instagrapi.mixins.collection import CollectionMixin |
| 14 | +from instagrapi.mixins.comment import CommentMixin |
| 15 | +from instagrapi.mixins.direct import DirectMixin |
| 16 | +from instagrapi.mixins.explore import ExploreMixin |
| 17 | +from instagrapi.mixins.fbsearch import FbSearchMixin |
| 18 | +from instagrapi.mixins.fundraiser import FundraiserMixin |
| 19 | +from instagrapi.mixins.hashtag import HashtagMixin |
| 20 | +from instagrapi.mixins.highlight import HighlightMixin |
| 21 | +from instagrapi.mixins.igtv import DownloadIGTVMixin, UploadIGTVMixin |
| 22 | +from instagrapi.mixins.insights import InsightsMixin |
| 23 | +from instagrapi.mixins.location import LocationMixin |
| 24 | +from instagrapi.mixins.media import MediaMixin |
| 25 | +from instagrapi.mixins.multiple_accounts import MultipleAccountsMixin |
| 26 | +from instagrapi.mixins.note import NoteMixin |
| 27 | +from instagrapi.mixins.notification import NotificationMixin |
| 28 | +from instagrapi.mixins.password import PasswordMixin |
| 29 | +from instagrapi.mixins.photo import DownloadPhotoMixin, UploadPhotoMixin |
| 30 | +from instagrapi.mixins.private import PrivateRequestMixin |
| 31 | +from instagrapi.mixins.public import ( |
| 32 | + ProfilePublicMixin, |
| 33 | + PublicRequestMixin, |
| 34 | + TopSearchesPublicMixin, |
| 35 | +) |
| 36 | +from instagrapi.mixins.share import ShareMixin |
| 37 | +from instagrapi.mixins.signup import SignUpMixin |
| 38 | +from instagrapi.mixins.story import StoryMixin |
| 39 | +from instagrapi.mixins.timeline import ReelsMixin |
| 40 | +from instagrapi.mixins.totp import TOTPMixin |
| 41 | +from instagrapi.mixins.track import TrackMixin |
| 42 | +from instagrapi.mixins.user import UserMixin |
| 43 | +from instagrapi.mixins.video import DownloadVideoMixin, UploadVideoMixin |
| 44 | + |
| 45 | +urllib3.disable_warnings(InsecureRequestWarning) |
| 46 | + |
| 47 | +# Used as fallback logger if another is not provided. |
| 48 | +DEFAULT_LOGGER = logging.getLogger("instagrapi") |
| 49 | + |
| 50 | + |
| 51 | +class Client( |
| 52 | + PublicRequestMixin, |
| 53 | + ChallengeResolveMixin, |
| 54 | + PrivateRequestMixin, |
| 55 | + TopSearchesPublicMixin, |
| 56 | + ProfilePublicMixin, |
| 57 | + LoginMixin, |
| 58 | + ShareMixin, |
| 59 | + TrackMixin, |
| 60 | + FbSearchMixin, |
| 61 | + HighlightMixin, |
| 62 | + DownloadPhotoMixin, |
| 63 | + UploadPhotoMixin, |
| 64 | + DownloadVideoMixin, |
| 65 | + UploadVideoMixin, |
| 66 | + DownloadAlbumMixin, |
| 67 | + NotificationMixin, |
| 68 | + UploadAlbumMixin, |
| 69 | + DownloadIGTVMixin, |
| 70 | + UploadIGTVMixin, |
| 71 | + MediaMixin, |
| 72 | + UserMixin, |
| 73 | + InsightsMixin, |
| 74 | + CollectionMixin, |
| 75 | + AccountMixin, |
| 76 | + DirectMixin, |
| 77 | + LocationMixin, |
| 78 | + HashtagMixin, |
| 79 | + CommentMixin, |
| 80 | + StoryMixin, |
| 81 | + PasswordMixin, |
| 82 | + SignUpMixin, |
| 83 | + DownloadClipMixin, |
| 84 | + UploadClipMixin, |
| 85 | + ReelsMixin, |
| 86 | + ExploreMixin, |
| 87 | + BloksMixin, |
| 88 | + TOTPMixin, |
| 89 | + MultipleAccountsMixin, |
| 90 | + NoteMixin, |
| 91 | + FundraiserMixin, |
| 92 | +): |
| 93 | + proxy = None |
| 94 | + |
| 95 | + def __init__( |
| 96 | + self, |
| 97 | + settings: dict = {}, |
| 98 | + proxy: str = None, |
| 99 | + delay_range: list = None, |
| 100 | + logger=DEFAULT_LOGGER, |
| 101 | + **kwargs, |
| 102 | + ): |
| 103 | + |
| 104 | + super().__init__(**kwargs) |
| 105 | + |
| 106 | + self.settings = settings |
| 107 | + self.logger = logger |
| 108 | + self.delay_range = delay_range |
| 109 | + |
| 110 | + self.set_proxy(proxy) |
| 111 | + |
| 112 | + self.init() |
| 113 | + |
| 114 | + def set_proxy(self, dsn: str): |
| 115 | + if dsn: |
| 116 | + assert isinstance( |
| 117 | + dsn, str |
| 118 | + ), f'Proxy must been string (URL), but now "{dsn}" ({type(dsn)})' |
| 119 | + self.proxy = dsn |
| 120 | + proxy_href = "{scheme}{href}".format( |
| 121 | + scheme="http://" if not urlparse(self.proxy).scheme else "", |
| 122 | + href=self.proxy, |
| 123 | + ) |
| 124 | + self.public.proxies = self.private.proxies = { |
| 125 | + "http": proxy_href, |
| 126 | + "https": proxy_href, |
| 127 | + } |
| 128 | + return True |
| 129 | + self.public.proxies = self.private.proxies = {} |
| 130 | + return False |
0 commit comments