38
loading...
This website collects cookies to deliver better user experience
create project
, then give your project a name, use case, and a description.access token
and access secret
token for your application at the bottom of the page displayed.# creating the project
python manage.py startproject TweetsDaily
# change to the project directory
cd TweetsDaily
#creating the app
python manage.py startapp App
settings.py
file and App
to the list of installed apps as seen below:INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'App'
]
urls.py
file in the project directory and edit as seen in the code below:from django.contrib import admin
from django.urls import path, include
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("App.urls")),
]
App
folder and create a new urls.py
file. Add the python code below to the file and save:from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
from . import views
app_name = "App"
urlpatterns = [
path("", views.index, name="index"),
]
index
view, which we will define in our views.py
file.models.py
file as see in the python code below:from django.db import models
# Create your models here.
from django.db import models
# Create your models here.
class Tweets(models.Model):
username= models.TextField()
tweet_number = models.IntegerField()
created_at = models.IntegerField()
time = models.IntegerField()
retweet_count= models.IntegerField()
def __str__(self):
return self.tweet_number
#making migrations
python manage.py makemigrations
#migrating the database
python manage.py migrate
views.py
file in the Django project is where we write the main logic for the application.views.py
file to import the required libraries.from django.shortcuts import render
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Cursor
from datetime import datetime, date, time, timedelta
from collections import Counter
from django.http import HttpResponse
from . models import Tweets
views.py
file and provide your real keys to initialize Tweepy.consumer_key="api_key"
consumer_secret="api_secret_key"
access_token="access_token"
access_token_secret="access_token_secret"
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
auth_api = API(auth,wait_on_rate_limit=True)
username=["Cristiano","BarackObama","rihanna","TheRock"]
views.py
file to build the index view. This view is triggered by loading the application in the browser:def index(request):
tweet_count = 0
for i in username:
try:
for status in Cursor(auth_api.user_timeline, id=i).items():
tweet_count = tweet_count + 1
if status.created_at.year == 2021 and status.created_at.month == 5:
tweets_save= Tweets.objects.create(username=i,tweet_number=tweet_count,created_at=status.created_at.day,time=status.created_at.hour,retweet_count=status.retweet_count)
tweets_save.save()
except:
pass
return HttpResponse('<h1>Loaded Tweets Data</h1>')
#running the application
python manage.py runserver
index
view to save all the tweet data in the database.Save
.new query
button, then save and run the SQL query below:SELECT
username,
(
CASE
WHEN (username = 'rihanna')
THEN (SUM(retweet_count) / 102300000 * 100)
WHEN (username = 'BarackObama')
THEN (SUM(retweet_count) / 129600000 * 100)
WHEN (username = 'TheRock')
THEN (SUM(retweet_count) / 15200000 * 100)
WHEN (username = 'Cristiano')
THEN (SUM(retweet_count) / 92400000 * 100)
END
) AS tweet_heat,
CONCAT('May ', created_at)
FROM
app_tweets
GROUP BY
created_at,
username
ORDER BY
created_at ASC
New Dashboard
button in the top menu. Then, click on the + Add
button, then select ‘chart.’Select Chart Data
on the blank dashboard provided.WITH alldates AS(
SELECT
distinct created_at
FROM
app_tweets
ORDER BY
1
),
alltimes AS(
SELECT
distinct time
FROM
app_tweets
ORDER BY
1
),
timetable AS(
SELECT
*
FROM
alldates,
alltimes
ORDER BY
1
)
SELECT
CONCAT('May ',t.created_at) AS day,
CONCAT(t.time,':00') AS hour,
COUNT(a.tweet_number) as tweets
FROM
timetable t
LEFT OUTER JOIN app_tweets a ON (a.username = 'BarackObama' AND a.created_at = t.created_at AND a.time = t.time)
GROUP BY t.time, t.created_at
ORDER BY t.created_at, t.time
day
field in the x-axis, hour
in the y-axis, and the tweets
in the z-axis.WITH alldates AS(
SELECT
distinct created_at
FROM
app_tweets
ORDER BY
1
),
alltimes AS(
SELECT
distinct time
FROM
app_tweets
ORDER BY
1
),
timetable AS(
SELECT
*
FROM
alldates,
alltimes
ORDER BY
1
)
SELECT
CONCAT('May ',t.created_at) AS day,
CONCAT(t.time,':00') AS hour,
SUM(a.retweet_count) as retweets
FROM
timetable t
LEFT OUTER JOIN app_tweets a ON (a.username = 'BarackObama' AND a.created_at = t.created_at AND a.time = t.time)
GROUP BY t.time, t.created_at
ORDER BY t.created_at, t.time
day
field in the x-axis, hour
in the y-axis, and the retweets
in the z-axis.WITH alldates AS(
SELECT
distinct created_at
FROM
app_tweets
ORDER BY
1
),
alltimes AS(
SELECT
distinct time
FROM
app_tweets
ORDER BY
1
),
timetable AS(
SELECT
*
FROM
alldates,
alltimes
ORDER BY
1
)
SELECT
CONCAT('May ',t.created_at) AS day,
CONCAT(t.time,':00') AS hour,
CAST((SUM(a.retweet_count)/COUNT(tweet_number)) AS UNSIGNED) as ratio
FROM
timetable t
LEFT OUTER JOIN app_tweets a ON (a.username = 'BarackObama' AND a.created_at = t.created_at AND a.time = t.time)
GROUP BY t.time, t.created_at
ORDER BY t.created_at, t.time
day
field in the x-axis, hour
in the y-axis, and the ratio
in the z-axis.