django-social-video-publisher
A reusable Django app that connects users' social accounts through official OAuth and publishes video through official platform APIs. It tracks what each account can do, makes uploads idempotent, and reports only what the platform confirmed.
Version 0.1 ships a YouTube adapter. The adapter interface lets you add your own platforms.
Supports Python 3.11 to 3.14 and Django 5.2, 6.0 and 6.1. Celery and DRF are optional.
Alpha. The test suite and crash tests use mocks and a local stand-in built from Google's documentation. This version has not yet uploaded to real YouTube, so try it on a test channel first.
Five-minute YouTube quickstart
-
Install the package:
pip install "django-social-video-publisher[celery,drf]"
-
In Google Cloud, enable YouTube Data API v3 and create an OAuth client of type "Web application". Add your redirect URI.
-
Generate an encryption key and keep it outside the database:
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
-
Configure Django:
INSTALLED_APPS = [..., "django.contrib.contenttypes", "social_video_publisher"] SOCIAL_VIDEO_PUBLISHER = { "TOKEN_ENCRYPTION_KEYS": [os.environ["SOCIAL_TOKEN_KEY"]], "MEDIA_OPENER": "myproject.media.open_media", "PLATFORMS": { "youtube": { "CLIENT_ID": os.environ["YOUTUBE_CLIENT_ID"], "CLIENT_SECRET": os.environ["YOUTUBE_CLIENT_SECRET"], "REDIRECT_URI": "https://app.example/oauth/youtube", }, }, }
-
Supply the opener. The package never touches storage itself. Return a readable, seekable binary stream from any backend, because resuming an upload needs
seek:from django.core.files.storage import default_storage def open_media(ref): return default_storage.open(ref, "rb")
-
Run
python manage.py migrate, then connect an account and publish:from social_video_publisher import services from social_video_publisher.errors import OutcomeUnknown from social_video_publisher.types import MediaSpec, PublishMetadata url = services.start_connection(request.user, "youtube") # redirect the user here account = services.complete_connection(state, code, owner=request.user) media = MediaSpec( ref="videos/a.mp4", mime_type="video/mp4", size_bytes=size, duration_seconds=42.0, width=1080, height=1920, kind="short", ) meta = PublishMetadata( title="My Short", visibility="private", made_for_kids=False, contains_synthetic_media=True ) try: publication = services.publish(account, media, meta, idempotency_key="video-123") except OutcomeUnknown as exc: publication = exc.publication # reconcile; never upload again publication = services.reconcile(publication) # call until published or failed
publish returns a publication in processing. It becomes published only after a later
reconcile sees the video on the platform with its processing finished.
YouTube audit and quota limits
These facts come from Google's documentation as checked on 4 Nov 2026. They change often, so check the revision history.
Uploads from unaudited projects. From 28 Jul 2020 to 2 Nov 2026, YouTube locked uploads from
unverified projects to private. Google reversed that on 2 Nov 2026. This package still keeps
uploads private by default until you set PROJECT_AUDITED: True, because a public video from an
unreviewed setup is the costlier mistake. That is this package's policy, not a YouTube rule. Set
FORCE_PRIVATE_WHEN_UNAUDITED: False to turn it off. The reason appears in capabilities and in
Publication.visibility_reason.
Privacy is always sent. YouTube makes a video public when privacyStatus is omitted, so the
adapter always sends it.
Quota is per Google Cloud project, shared by all your users.
| Bucket | Default | Used by |
|---|---|---|
| Video Uploads | 100 videos.insert calls a day |
publish |
| Default | 10,000 units a day | channels.list, playlistItems.list and videos.list at 1 unit each |
Days reset at midnight Pacific time. QuotaLedger reserves quota before each call and raises
QuotaExhausted with the reset time. It tracks only calls made through this package, so calls
from other software on the same Google project are not counted. Quota is released after a definite
failure and kept after OutcomeUnknown. A reconcile reserves its worst case of 5 units.
OAuth. youtube.upload is a sensitive scope. Apps left in Google's "Testing" mode get
short-lived refresh tokens, so plan for OAuth verification.
Shorts. There is no API flag. YouTube classifies vertical or square videos of up to three
minutes as Shorts. With kind="short" the adapter validates shape and
SHORTS_MAX_DURATION_SECONDS.
Description marker. The adapter appends [ref:<hash>] to each description so it can find an
upload after a lost connection. It is visible to viewers and contains only a hash.
How failures behave
| Error | Meaning | What to do |
|---|---|---|
AuthExpired |
Tokens no longer work | Ask the owner to reconnect |
CapabilityUnavailable |
The account cannot do this | Show reason |
QuotaExhausted |
No quota left | Retry the same key after reset_at |
RateLimited |
Slow down | Retry the same key after retry_after |
ValidationFailed |
Bad input; nothing was sent | Fix issues |
PlatformRejected |
A definite no | Retry the same key only if retryable |
OutcomeUnknown |
It may have succeeded | Call reconcile; never upload again |
OAuthStateInvalid |
State unknown, expired, reused or not this owner's | Restart the connection |
TokenDecryptionError |
No configured key fits | Restore the key that encrypted the value |
All of them inherit from SocialVideoPublisherError.
Publication status moves through pending, uploading, processing and published, or ends in
failed. outcome_unknown waits for reconcile, which adopts the video if it finds the marker
and marks the publication failed after RECONCILE_GRACE_SECONDS if it does not.
The same idempotency key always refers to the same publication. A worker that dies mid-upload
leaves a persisted session. Calling publish again with the same key after the lease expires
resumes that session, and never creates a second video. Using a key for different content raises
ValidationFailed.
Settings reference
All settings live in the SOCIAL_VIDEO_PUBLISHER dict and are validated at startup. Unknown keys are an
error.
| Key | Default | Purpose |
|---|---|---|
OWNER_MODEL |
AUTH_USER_MODEL |
app_label.Model that owns connected accounts |
TOKEN_ENCRYPTION_KEYS |
required | Fernet keys. The first encrypts; all can decrypt |
MEDIA_OPENER |
required | Dotted path to callable(ref) -> binary stream |
PLATFORMS |
{} |
Per-platform settings, see below |
OAUTH_STATE_TTL_SECONDS |
600 |
How long a connection attempt stays valid |
UPLOAD_LEASE_SECONDS |
900 |
How long a silent worker keeps an upload before another can resume it |
RECONCILE_GRACE_SECONDS |
3600 |
How long to look for a marker before marking failed |
DRF_OWNER_RESOLVER |
request user | Dotted path to callable(request) -> owner |
PLATFORMS["youtube"]:
| Key | Default | Purpose |
|---|---|---|
CLIENT_ID, CLIENT_SECRET, REDIRECT_URI |
required | Google OAuth client |
PROJECT_AUDITED |
False |
Whether the project passed Google's compliance audit |
FORCE_PRIVATE_WHEN_UNAUDITED |
True |
Keep uploads private until audited |
DAILY_UPLOAD_LIMIT |
100 |
Upload bucket size. Raise it after Google grants more |
DAILY_UNIT_LIMIT |
10000 |
Default bucket size |
SHORTS_MAX_DURATION_SECONDS |
180 |
Longest video accepted as kind="short" |
CHUNK_SIZE_BYTES |
8 MiB | Must be a multiple of 256 KiB |
RETRY_BACKOFF_SECONDS |
1.0 |
Base delay when a chunk gets a 5xx response |
ADAPTER |
built-in | Dotted path to replace the adapter |
Rotating keys. Add the new key first in the list, keeping the old one. Run
python manage.py social_video_publisher_rotate_keys, then remove the old key.
Optional extras
DRF. path("social/", include("social_video_publisher.contrib.drf.urls")) adds owner-scoped
endpoints for connecting, listing and disconnecting accounts, and reading capabilities and
publications. There is deliberately no browser-redirect callback. Point REDIRECT_URI at your
frontend and have it post state and code with the signed-in user's credentials, which stops an
attacker from tricking someone into linking their channel to the attacker's account. If you call
complete_connection yourself, pass owner= for the same reason.
Celery. social_video_publisher.contrib.celery.tasks has publish_task and reconcile_task. They
add no retries and set no queue, so both stay your decision.
Signals. account_connected, account_disconnected and publication_status_changed fire
after the transaction commits.
Writing your own adapter
Implement social_video_publisher.adapters.PlatformAdapter, then register it:
from social_video_publisher.adapters import register_adapter
register_adapter("myplatform", MyAdapter) # called with the PLATFORMS["myplatform"] dict
Add a PLATFORMS["myplatform"] entry so the platform is enabled. What the service layer relies on:
- Report honestly.
publishreturns aPublishResultonly for what the platform confirmed. RaiseOutcomeUnknownwhen the request may have succeeded. RaisePlatformRejectedfor a definite no, withretryable=Trueif the same request could work later. - Support recovery. Embed
request.markersomewherereconcilecan search. Callrequest.on_checkpoint(token)before sending bytes, and resume fromrequest.resume_token. - Handle both reconcile forms.
publication_refis a media ID, ormarker:<value>when no ID is known. Returnstatus="outcome_unknown"when a marker is not found yet. - Declare quota.
quota_for(operation)returns a list ofQuotaCharge, and can be empty. Operations areconnect,publish,reconcileandhealth. - Raise
AuthExpiredwhen the token is rejected. The service refreshes it once and retries. - Keep secrets out of errors and logs.
TokenBundlealready hides tokens fromrepr.
Development
pip install -e ".[celery,drf,dev]"
pytest # SQLite
TEST_POSTGRES_HOST=localhost pytest # also runs the concurrent-quota test
ruff check . && mypy
Test fixtures in tests/fixtures are written by hand from Google's API reference, not recorded
from live traffic. Run them against a real test channel before relying on a new API behaviour.
License
MIT
Metadata
Release files for django-social-video-publisher 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_social_video_publisher-0.1.0.tar.gz | 39.9 kB | With provenance | Details for django_social_video_publisher-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_social_video_publisher-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for django_social_video_publisher-0.1.0-py3-none-any.whl |
Total release size: 78.9 kB
Security for django-social-video-publisher 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.