Skip to main content

An app to manage tweets in a Django project

Project description

django-tweets

Create and delete tweets in a Django project.

This packages takes the advantage of the tweepy functionalies to connect it to a Django Backend.

The tweets objects can have media files as well.

Set up

Twitter Account

  1. Make sure you have a Twitter account.
  2. Go to the Twitter Developer Portal.
  3. Create a Project and an App
  4. Make sure your App has read and write permissions.
  5. Genenerate the necessary secrets and token.

django-tweets

  1. Install from PyPI
python -m pip install django-tweets
  1. Add the package to your settins INSTALLED_APPS
INSTALLED_APPS = [
    ...
    "django_tweets",
    ...
]
  1. Add the following settings to your Django project.
Django setting Description Required
TWITTER_API_KEY Twitter API OAuth 1.0a Consumer Key Yes
TWITTER_API_KEY_SECRET Twitter API OAuth 1.0a Consumer Secret Yes
TWITTER_BEARER_TOKEN Twitter API OAuth 2.0 Bearer Token / Access Token Yes
TWITTER_ACCESS_TOKEN Twitter API OAuth 1.0a Access Token Yes
TWITTER_ACCESS_TOKEN_SECRET Twitter API OAuth 1.0a Access Token Secret Yes
DJANGO_TWEETS_SYNC_DELETE Synchronize object deletion with Twitter API. This is activated by default. No

Example:

import os
from dotenv import load_dotenv
load_dotenv()

...

# django-tweets
# Consumer Keys
TWITTER_API_KEY = os.environ.get("TWITTER_API_KEY")
TWITTER_API_KEY_SECRET = os.environ.get("TWITTER_API_KEY_SECRET")
# Authentication Tokens
TWITTER_BEARER_TOKEN = os.environ.get("TWITTER_BEARER_TOKEN")
TWITTER_ACCESS_TOKEN = os.environ.get("TWITTER_ACCESS_TOKEN")
TWITTER_ACCESS_TOKEN_SECRET = os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")
# OAuth 2.0 Client ID and Client Secret
TWITTER_CLIENT_ID = os.environ.get("TWITTER_CLIENT_ID")
TWITTER_CLIENT_SECRET = os.environ.get("TWITTER_CLIENT_SECRET")
  1. Run migrations
python manage.py migrate

Usage

Create a simple tweet

from django_tweets.models import Tweet

# create a tweet in the db
tweet = Tweet.objects.create(text="Hi, this is my tweet using django-tweets and tweepy")

# publish it
tweet.publish()

Create a simple tweet with a media file

from pathlib import Path
from django.core.files.base import ContentFile
from django_tweets.models import Tweet, MediaFile

# create a media file
path = Path("path/to/my/file.jpg")

with open(path, "rb") as f:
    f.seek(0)
    contents = f.read()

mediafile = MediaFile.objects.create(title="nice photo")
mediafile.file.save(path.name, ContentFile(contents))
# upload to Twitter
mediafile = mediafile.upload_file()

# create a tweet in the db
tweet = Tweet.objects.create(text="My tweet with a file")

# add the media file to the tweet object
tweet.files.add(mediafile)

# publish it
tweet.publish()

Questions

  • How Can I get the read and write permission for my app? Answer:
  1. In the Twitter Developer Portal, click on gear icon of the project app

Gear icon app settings

  1. Then go User authentication settings and click on the Edit button.

Edit user auth settings

  1. Configure the the form and submit.

User authentication settings

About

🐣 django_tweets

©Django is a registered trademark of the Django Software Foundation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-tweets-0.0.3.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

django_tweets-0.0.3-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file django-tweets-0.0.3.tar.gz.

File metadata

  • Download URL: django-tweets-0.0.3.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for django-tweets-0.0.3.tar.gz
Algorithm Hash digest
SHA256 520b434ac609ca03288d3ca68a40e7e25fb8e1f32a5a634a052e268bb01747b0
MD5 da11b3b34df4732c9be3f81823f5dc5b
BLAKE2b-256 0103a752a5b7c0ba43f6fcf91f7ed7780d82863beb9c826345b52ea173b01a2c

See more details on using hashes here.

Provenance

File details

Details for the file django_tweets-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: django_tweets-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for django_tweets-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9496f46c570217483e8e675c501bf9d050a72e4aacfd4caa11eec5c860bcebc0
MD5 cfc62a3f5468c70553c4fd5c99a47ff6
BLAKE2b-256 1470df8d61add21c1369946bbb0c5306560433349d0e84020b0a17cb6c6bd99e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page