Django package to sync and display personal Instagram media
BSD-3-CLAUSE License
A Django application that syncs one or more users Instagram posts to your project's database and media storage.
./manage.py sync_instagram_posts
)Instagram media is managed using Django admin console. Demo django project
poetry add django-instagram-profile
or using Pip
pip install django-instagram-profile
Dependencies will be installed automatically
Follow this guide.
If your app is intended for the general public to authenticate, then you will need to publish your app. Otherwise, you can get away with leaving your app in developer mode and adding your users as testers.
To add a test user:
INSTALLED_APPS = (
# ...
'instagram_profile',
)
INSTAGRAM_PROFILE = {
# You will get these from your registered instagram app
'app_id': '123456789012345',
'secret': '1234567890123456789012345678901234',
'redirect_url': 'https://www.example.com/your-admin/instagram_profile/profile/authorized',
}
OR if you prefer, create env.ini file inside your project directory with the following settings:
[instagram]
app_id = 123
secret = abc
redirect_url = https://www.example.com/your-admin/instagram_profile/profile/authorized
Run the database migrations
./manage.py migrate
Depending on your storage backend and current project you may need to configure media. Your project probably already has this configured.
Create a folder for Instagram media
/project_name/media/instagram
Configure correct paths for uploaded instagram media in settings.py
SITE_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'
Add media template context processor in the settings.py Also add template dir to the list of dirs
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'OPTIONS': {
'context_processors': [
'django.template.context_processors.media', # add this line
],
},
},
]
To serve media files during development need to add following lines to urls.py
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
django-instagram-profile comes with some public views to show your instagram posts. Depending on your project needs you may or may not want to use them.
If you want to utilise these them add url to display instagram media feed in urls.py:
urlpatterns = [
# ...
path('instagram/', include('instagram_profile.urls')), # add this line
]
If you have not publicly published your app, you will first need to setup your instagram user as a test user as described above.
Add a new profile in the Django admin, only setting the username field. (Instagram Sync > Profiles > Add)
After clicking "Save & Continue Editing", click "Authorize" in the top right of the profile form (depending on your needs, this might need to be done by your Instagram user). This will redirect you/them to Instagram. Accept all permissions.
Once you are authorized, you can sync posts by:
Run this command. You'll probably want to set up a crontab (or similar) to run this regularly.
./manage.py sync_instagram_posts
The long-term access codes will be automatically renewed if they will expire within 30 days. So if this command is run more frequently than every 30 days, authentication should continue in-definitely.
You can interface with the Post
model however you like. We recommend the excellent django-imagekit library, which will allow you to resize and change these images for however you like in your own code.