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
mysite/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
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.
myapp/models.py
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.
I also wanted to check if it actually works from the Django management screen, so I will write it here as well.
myapp/admin.py
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')
admin.site.register(Supermodel,SupermodelAdmin)
$ 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.
myapp/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
data.save()
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)
else:
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