-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstaAPI.py
63 lines (50 loc) · 1.88 KB
/
InstaAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import requests
from typing import List
class InstaPost:
def __init__(self, thumbnail: str, download: str):
self.thumbnail = thumbnail
self.download = download
def __repr__(self):
return f"InstaPost(thumbnail='{self.thumbnail}', download='{self.download}')"
class InstaAPI:
"""
A Python Wrapper for Instagram Downloader API on RapidAPI.
"""
BASE_URL = "https://test-api646.p.rapidapi.com/"
def __init__(self, key: str):
"""
Initializes the InstaAPI client. 🛠️
:param key: Your RapidAPI key.
"""
if not key:
raise ValueError("API key is required! 😢")
self.key = key
self.headers = {
"X-RapidAPI-Key": self.key,
"X-RapidAPI-Host": "test-api646.p.rapidapi.com"
}
def get_links(self, url: str) -> List[InstaPost]:
"""
Fetches links for an Instagram post. 🖼️
:param url: The Instagram post URL.
:return: A list of InstaPost objects.
:raises Exception: If the API call fails.
"""
params = {"url": url}
try:
res = requests.get(self.BASE_URL, headers=self.headers, params=params)
res.raise_for_status()
data = res.json()
if data.get("status") != "success":
raise ValueError(f"API Error: {data.get('message', 'No details provided 😕')}")
posts = [
InstaPost(item["thumbnail"], item["download"])
for item in data.get("data", [])
]
return posts
except requests.exceptions.RequestException as err:
raise Exception(f"Request failed: {err} 🚨")
except ValueError as err:
raise Exception(f"API Error: {err} 🚫")
def __repr__(self):
return f"<InstaAPI(host={self.headers['X-RapidAPI-Host']})>"