[PYTHON] Save tweet data with Django

The guy who saves Twitter tweet data with Django

I added a function to save tweet data to the client I made last time. It's not smart at all as a method, but it also serves as a report that it worked well.

Last time> http://qiita.com/Gen6/items/11fa5265053da95fcf0b



    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

Write models.py

The part I've been stumbling upon (last year) in Django is now called models.py. I wasn't sure what I was actually doing, but it's like defining a so-called database table.


from django.db import models

class Supermodel(models.Model):
    user_name = models.CharField(max_length=140)
    user_id = models.CharField(max_length=140)
    user_img = models.CharField(max_length=140)
    user_text = models.TextField(null=True)
    user_created_at = models.CharField(max_length=140)

    def __str__(self):
        return self.user_name

I want to save the user name, ID, thumbnail, text body, date and time from the tweet data, so it is written like this. I was wondering what the name Supermodel was, but I wrote it, so I can't help it.

Write admin.py

I also wanted to check if it actually works from the Django management screen, so I will write it here as well.


from django.contrib import admin
from myapp.models import Supermodel

class SupermodelAdmin(admin.ModelAdmin):
    list_display = ('id','user_id','user_name','user_img','user_text','user_created_at')



$ python manage.py makemigrations myapp
$ python manage.py migrate

If successful, proceed to the next. To be honest, I'm not completely sure about this, so I think it's better to read other people's descriptions.

Edit views.py


from requests_oauthlib import OAuth1Session
import time, calendar
import datetime
import json
import re
import os
import requests
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

from django.http.response import HttpResponse
from django.shortcuts import render
from myapp.models import Supermodel

def index(request):

    msg = request.GET.get('words')

    C_KEY = '**************************'
    C_SECRET = '**************************'
    A_KEY = '**************************'
    A_SECRET = '**************************'

    url = 'https://api.twitter.com/1.1/statuses/update.json'
    params = {'status': msg,'lang': 'ja'}
    tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
    req = tw.post(url, params = params)

    url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'
    params = {'count': 1}
    req = tw.get(url, params = params)

    if req.status_code == 200:
        timeline = json.loads(req.text)
        limit = req.headers['x-rate-limit-remaining']

        for tweet in timeline:
            Text = (tweet['text'])
            User = (tweet['user']['screen_name'])
            Name = (tweet['user']['name'])
            Img = (tweet['user']['profile_image_url'])
            Created_at = YmdHMS(tweet['created_at'])

            data = Supermodel()
            data.user_id = User
            data.user_name = Name
            data.user_img = Img
            data.user_text = Text
            data.user_created_at = Created_at

            Message = {
                'Words': msg,
                'timeline': timeline,
                'API_limit': limit,
                'Text': Text,
                'User': User,
                'Name': Name,
                'Img': Img,
                'Created_at': Created_at,

            return render(request, 'index.html', Message)

        Error = {
            'Error_message': 'API restricted',
        return render(request, 'index.html', Error)

def YmdHMS(created_at):
    time_utc = time.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y')
    unix_time = calendar.timegm(time_utc)
    time_local = time.localtime(unix_time)
    return int(time.strftime('%Y%m%d%H%M%S', time_local))

Suimasen with a dirty code is now complete.


You can check it on localhost / admin. It's really easy. Actually.

If you use the search API, you can also import the search results into the database.

Recommended Posts

Save tweet data with Django
Internationalization with django
CRUD with Django
Save multiple models in one form with Django
Generate and post dummy image data with Django
Divide your data into project-like units with Django (3)
I tried to save the data with discord
Save data to flash with STM32 Nucleo Board
Divide your data into project-like units with Django
Authenticate Google with Django
Data analysis with python 2
Upload files with Django
Development digest with Django
Save memory with `` __slots__``
Output PDF with Django
Markdown output with Django
Twitter OAuth with Django
Reading data with TensorFlow
Getting Started with Django 1
Send email with Django
File upload with django
Data visualization with pandas
Data manipulation with Pandas!
Use LESS with Django
Shuffle data with pandas
Pooling mechanize with Django
Data Augmentation with openCV
Use MySQL with Django
Normarize data with Scipy
Data analysis with Python
Start today with Django
LOAD DATA with PyMysql
Getting Started with Django 2
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Embed audio data with Jupyter
Graph Excel data with matplotlib (1)
Get started with Django! ~ Tutorial ⑤ ~
Artificial data generation with numpy
Minimal website environment with django
Create an API with Django
Extract Twitter data with CSV
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Save images with web scraping
Getting Started with Python Django (1)
Create a homepage with django
Clustering ID-POS data with LDA
Get started with Django! ~ Tutorial ④ ~
Learn new data with PaintsChainer
Getting Started with Python Django (4)
Binarize photo data with OpenCV
Graph Excel data with matplotlib (2)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Tweet with image in Python
Data acquired by Django releted
Do AES encryption with DJango
django data input dumpdata loaddata
Getting Started with Python Django (6)
Combine two images with Django
Getting Started with Django with PyCharm