@danhachuel/thunderbolt 0.6.35 → 0.6.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/integrations/instagram_public.py +33 -14
- package/package.json +1 -1
|
@@ -139,12 +139,40 @@ def _fetch_web_profile_user(username: str) -> dict[str, Any] | None:
|
|
|
139
139
|
f'https://www.instagram.com/api/v1/users/web_profile_info/?username={username}',
|
|
140
140
|
f'https://i.instagram.com/api/v1/users/web_profile_info/?username={username}',
|
|
141
141
|
)
|
|
142
|
+
def profile_quality(user: Mapping[str, Any]) -> int:
|
|
143
|
+
return sum(bool(user.get(key)) for key in ('biography', 'edge_follow', 'edge_followed_by', 'edge_owner_to_timeline_media', 'username'))
|
|
144
|
+
|
|
145
|
+
def merge_profile(base: dict[str, Any] | None, candidate: Mapping[str, Any]) -> dict[str, Any]:
|
|
146
|
+
merged = dict(base or {})
|
|
147
|
+
for key, value in candidate.items():
|
|
148
|
+
if value not in (None, '', [], {}):
|
|
149
|
+
if isinstance(value, Mapping) and isinstance(merged.get(key), Mapping):
|
|
150
|
+
merged[key] = {**merged[key], **value}
|
|
151
|
+
else:
|
|
152
|
+
merged[key] = value
|
|
153
|
+
return merged
|
|
154
|
+
|
|
155
|
+
best_user: dict[str, Any] | None = None
|
|
156
|
+
for endpoint in endpoints:
|
|
157
|
+
try:
|
|
158
|
+
response = requests.get(endpoint, headers=headers, timeout=15)
|
|
159
|
+
if response.status_code >= 400:
|
|
160
|
+
continue
|
|
161
|
+
payload = response.json()
|
|
162
|
+
except (requests.RequestException, ValueError, AttributeError):
|
|
163
|
+
continue
|
|
164
|
+
user = ((payload.get('data') or {}).get('user') if isinstance(payload, dict) else None)
|
|
165
|
+
if isinstance(user, dict):
|
|
166
|
+
best_user = merge_profile(best_user, user)
|
|
167
|
+
if profile_quality(best_user) >= 4:
|
|
168
|
+
return best_user
|
|
169
|
+
|
|
142
170
|
curl = shutil.which('curl')
|
|
143
171
|
if curl:
|
|
144
172
|
for endpoint in endpoints:
|
|
145
173
|
try:
|
|
146
174
|
completed = subprocess.run(
|
|
147
|
-
[curl, '-L', '--max-time', '20', '-sS', '-A', headers['User-Agent'], '-H', f"x-ig-app-id: {headers['x-ig-app-id']}", '-H', 'Accept
|
|
175
|
+
[curl, '-L', '--max-time', '20', '-sS', '-A', headers['User-Agent'], '-H', f"x-ig-app-id: {headers['x-ig-app-id']}", '-H', 'Accept', 'application/json, text/plain, */*', endpoint],
|
|
148
176
|
capture_output=True,
|
|
149
177
|
text=True,
|
|
150
178
|
encoding='utf-8',
|
|
@@ -155,21 +183,12 @@ def _fetch_web_profile_user(username: str) -> dict[str, Any] | None:
|
|
|
155
183
|
payload = json.loads(completed.stdout) if completed.returncode == 0 and completed.stdout else None
|
|
156
184
|
user = ((payload.get('data') or {}).get('user') if isinstance(payload, dict) else None)
|
|
157
185
|
if isinstance(user, dict):
|
|
158
|
-
|
|
186
|
+
best_user = merge_profile(best_user, user)
|
|
187
|
+
if profile_quality(best_user) >= 4:
|
|
188
|
+
return best_user
|
|
159
189
|
except (OSError, subprocess.SubprocessError, ValueError):
|
|
160
190
|
continue
|
|
161
|
-
|
|
162
|
-
try:
|
|
163
|
-
response = requests.get(endpoint, headers=headers, timeout=15)
|
|
164
|
-
if response.status_code >= 400:
|
|
165
|
-
continue
|
|
166
|
-
payload = response.json()
|
|
167
|
-
except (requests.RequestException, ValueError, AttributeError):
|
|
168
|
-
continue
|
|
169
|
-
user = ((payload.get('data') or {}).get('user') if isinstance(payload, dict) else None)
|
|
170
|
-
if isinstance(user, dict):
|
|
171
|
-
return user
|
|
172
|
-
return None
|
|
191
|
+
return best_user
|
|
173
192
|
|
|
174
193
|
|
|
175
194
|
def _country_from_bloks(value: Any) -> str:
|
package/package.json
CHANGED