Metadata-Version: 2.4
Name: sociallens
Version: 0.1.0
Summary: Official Python client for the SocialLens API: public social media profile, post and comment data from 9 networks as JSON.
Author: SocialLens
License: MIT
Project-URL: Homepage, https://sociallens.dev
Project-URL: Documentation, https://sociallens.dev/docs
Project-URL: Pricing, https://sociallens.dev/pricing
Project-URL: Repository, https://github.com/sociallensapi/sociallens-python
Keywords: sociallens,social media api,follower count,instagram,tiktok,youtube,x,reddit,linkedin,threads,pinterest,facebook
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.25
Dynamic: license-file

# SocialLens Python SDK

Official Python client for the [SocialLens API](https://sociallens.dev/docs). It reads public profile, post and comment data from 9 social networks (Instagram, TikTok, YouTube, X, Facebook, LinkedIn, Threads, Pinterest and Reddit) and returns the API's JSON as plain dictionaries.

- One request format for every network.
- Public data only. No login to any social account.
- Pay as you go: 1 credit per delivered read, and errors are never charged. A new account starts with 250 free reads, no card.

## Install

```bash
pip install sociallens
```

Python 3.8 or newer. The only dependency is `requests`.

## Quickstart

```python
from sociallens import SocialLens

client = SocialLens("YOUR_API_KEY")  # or set SOCIALLENS_API_KEY and call SocialLens()

profile = client.profile("instagram", "nasa")
print(profile["followers"], profile["is_exact"])

post = client.post("tiktok", "https://www.tiktok.com/@tiktok/video/7106594312292453675")
print(post["likes"], post["views"], post["comments"])

page = client.profile_posts("youtube", "mkbhd", limit=5)
for item in page["posts"]:
    print(item["published_at"], item["likes"], item["views"])
```

Get a free key at https://sociallens.dev/register.

## Methods

| Method | Endpoint | Networks |
| --- | --- | --- |
| `ping()` | `GET /v1/ping` | Checks the key. Free |
| `profile(platform, username)` | `GET /v1/profile` | All 9 |
| `post(platform, url)` | `GET /v1/post` | All except LinkedIn |
| `profile_posts(platform, username, limit=10)` | `GET /v1/profile-posts` | All 9 |
| `post_comments(platform, url, limit=20)` | `GET /v1/post-comments` | TikTok, Instagram, YouTube, Facebook, Reddit |
| `followers(username, cursor=None)` | `GET /v1/followers` | TikTok |
| `following(username, cursor=None)` | `GET /v1/following` | TikTok |

`platform` is one of `tiktok`, `instagram`, `youtube`, `facebook`, `x`, `reddit`, `linkedin`, `threads`, `pinterest`. The same list is exported as `sociallens.PLATFORMS`, and `sociallens.COVERAGE` maps each method to the networks it supports.

Optional keyword arguments on the read methods:

- `fresh=True` skips the short read cache.
- `cache_max_age="1d"` (or `3d`, `7d`, `14d`, `30d`) lets the source answer from its own stored copy.
- `type="person"` on `profile` reads a LinkedIn person when the username is a name. The default is a company. A full LinkedIn URL needs no `type`.

`username` is the public handle, with or without the `@`. On Reddit it is the subreddit name. Facebook and LinkedIn also accept the full URL.

## Reading the result

Every method returns the JSON body as a `dict`, unchanged. The field reference is in the [API documentation](https://sociallens.dev/docs).

```python
profile = client.profile("tiktok", "some_handle")

if not profile["exists"]:
    print("No such profile")       # a delivered answer, not an error
elif profile.get("private"):
    print("Private profile")
else:
    print(profile["followers"])    # exact when profile["is_exact"] is True
```

- `exists: false` is a result, not an exception.
- `is_exact` is `False` when the network only publishes a rounded number, for example YouTube subscribers.
- A counter the network does not publish is `None`, never `0`.
- `fetched_at` is the time of the read, in UTC.
- `credits` shows what the call charged and the balance left.

Two networks answer `profile` with a different shape. `client.profile("reddit", "science")` returns a subreddit with `active_users` (weekly) and `weekly_contributions`, because Reddit has no follower count. `client.profile("linkedin", "shopify")` returns a company with `followers` and `employee_count`.

## Errors

An error status raises an exception. No error status is ever charged.

| Exception | Status and code |
| --- | --- |
| `BadRequestError` | 400 `bad_request` |
| `AuthenticationError` | 401 `invalid_token` |
| `InsufficientCreditsError` | 402 `no_credits`, `free_tier_exhausted` |
| `PermissionDeniedError` | 403 `email_not_verified` |
| `AccountBusyError` | 409 `account_busy` |
| `RateLimitError` | 429 `rate_limited` |
| `NotAvailableError` | 501 `platform_not_available` |
| `SourceUnavailableError` | 503 `source_unavailable` |
| `APIConnectionError` | No HTTP answer: network error or timeout |

All HTTP errors inherit from `APIError` and carry `status`, `code`, `message`, `body` and `retry_after`. Everything inherits from `SocialLensError`.

```python
from sociallens import APIError, RateLimitError, SocialLens

client = SocialLens("YOUR_API_KEY")

try:
    data = client.profile("tiktok", "nasa")
except RateLimitError as error:
    print("Retry in", error.retry_after, "seconds")
except APIError as error:
    print(error.status, error.code, error.message)
```

## Retries, timeouts and pace

- `409`, `429` and `503` are retried up to `max_retries` times (default 2), waiting the `Retry-After` the API sends. Those statuses are never charged.
- Network errors and timeouts raise `APIConnectionError` and are not retried, because the read may have completed on the server.
- The API processes the requests of one account one at a time and allows 60 requests per minute per key. Run reads in sequence: a parallel pool does not finish sooner.

```python
client = SocialLens("YOUR_API_KEY", timeout=90, max_retries=3)
```

## Paging a TikTok follower list

Each page is one credit, so set your own ceiling.

```python
cursor, pages = None, 0
while pages < 3:
    page = client.followers("nasa", cursor=cursor)
    for user in page["users"]:
        print(user["username"], user["followers"])
    pages += 1
    if not page["has_more"]:
        break
    cursor = page["next_cursor"]
```

## Billing

A delivered answer (HTTP 200) costs 1 credit, and every error costs nothing. For profiles, "does not exist" and "private" are delivered answers. The rules for posts that are not found differ by network and are listed in the [documentation](https://sociallens.dev/docs).

## Links

- API documentation: https://sociallens.dev/docs
- Pricing: https://sociallens.dev/pricing
- Guides: https://sociallens.dev/blog

## License

MIT
