Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

Commit

Permalink
Import top 100 TV shows of all time
Browse files Browse the repository at this point in the history
Also view the api on api/tv_shows

Closes #32
  • Loading branch information
shrikrishna committed Apr 16, 2018
1 parent 712475b commit 648f09f
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions awesome100/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'python_libraries',
'singers',
'songs',
'tv_shows',
'writers',
'rest_framework',
]
Expand Down
1 change: 1 addition & 0 deletions awesome100/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
url('api/', include('authors.urls')),
url('api/', include('movies.urls')),
url('api/', include('singers.urls')),
url('api/', include('tv_shows.urls')),

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ <h1>Welcome</h1>
<li><a href="/api/authors/">100 greatest authors of all time</a>
<li><a href="/api/movies/">100 greatest movies of all time</a>
<li><a href="/api/singers/">100 greatest singers of all time</a>
<li><a href="/api/tv_shows/">100 greatest tv_shows of all time</a>
</ul>
</body>
</html>
Empty file added tv_shows/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions tv_shows/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from tv_shows.models import TvShow

admin.site.register(TvShow)
5 changes: 5 additions & 0 deletions tv_shows/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class TvShowsConfig(AppConfig):
name = 'tv_shows'
45 changes: 45 additions & 0 deletions tv_shows/get_tv_shows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from requests_html import HTMLSession

from tv_shows.models import TvShow

session = HTMLSession()
r = session.get('http://www.imdb.com/list/ls004729995/?sort=moviemeter,asc&st_dt=&mode=detail&page=1')
tv_shows = r.html.find('.lister-item')


def import_data(i, tv_show):
try:
rank = i
name = tv_show.find('h3')[0].find('a')[0].text
url = ('http://www.imdb.com'
+ tv_show.find('h3')[0].find('a')[0].attrs.get('href'))
summary = tv_show.find('p')[1].text
rating = tv_show.find('strong')[0].text
image_url = tv_show.find('img')[0].attrs.get('loadlate')
genre = tv_show.find('.genre')[0].text
duration = tv_show.find('.runtime')[0].text
actors_and_directors = tv_show.find('.text-muted')[2].text
votes_and_gross = tv_show.find('.text-muted')[3].text
other_info = tv_show.find('.list-description')[0].text
except Exception as ex:
print(str(ex))
try:
c, created = TvShow.objects.get_or_create(
rank=rank,
url=url,
name=name,
summary=summary,
image_url=image_url,
rating=rating,
genre=genre,
duration=duration,
actors_and_directors=actors_and_directors,
votes_and_gross=votes_and_gross,
other_info=other_info,
)
if created:
c.save()
print('\nTvShow, {}, has been saved.'.format(c))
except Exception as ex:
print('\n\nSomething went wrong saving this tv_show: {}\n{}'
.format(name, str(ex)))
16 changes: 16 additions & 0 deletions tv_shows/management/commands/import_best_tv_shows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.core.management.base import BaseCommand

from tv_shows.get_tv_shows import tv_shows, import_data


class Command(BaseCommand):
help = 'Import movies data'

TV_SHOWS = staticmethod(tv_shows)
IMPORT_DATA = staticmethod(import_data)

def handle(self, *args, **options):
i = 1
for tv_show in self.TV_SHOWS:
self.IMPORT_DATA(i, tv_show)
i = i + 1
33 changes: 33 additions & 0 deletions tv_shows/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.12 on 2018-04-16 05:03
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='TvShow',
fields=[
('rank', models.IntegerField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=300, null=True)),
('url', models.URLField(null=True)),
('summary', models.TextField(null=True)),
('image_url', models.URLField(null=True)),
('rating', models.FloatField(null=True)),
('year_of_release', models.CharField(max_length=100, null=True)),
('genre', models.TextField(null=True)),
('duration', models.CharField(max_length=100, null=True)),
('actors_and_directors', models.TextField(null=True)),
('votes_and_gross', models.TextField(null=True)),
('other_info', models.TextField(null=True)),
],
),
]
Empty file added tv_shows/migrations/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions tv_shows/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.db import models


class TvShow(models.Model):
rank = models.IntegerField(primary_key=True)
name = models.CharField(max_length=300, null=True)
url = models.URLField(null=True)
summary = models.TextField(null=True)
image_url = models.URLField(null=True)
rating = models.FloatField(null=True)
year_of_release = models.CharField(max_length=100, null=True)
genre = models.TextField(null=True)
duration = models.CharField(max_length=100, null=True)
actors_and_directors = models.TextField(null=True)
votes_and_gross = models.TextField(null=True)
other_info = models.TextField(null=True)

def __str__(self):
return self.name
8 changes: 8 additions & 0 deletions tv_shows/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import serializers
from . import models


class TvShowSerializer(serializers.ModelSerializer):
class Meta:
model = models.TvShow
fields = '__all__'
8 changes: 8 additions & 0 deletions tv_shows/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import url

from . import views

urlpatterns = [ # Ignore PycodestyleBear (W605)
url('^tv_shows/$', views.ListTvShow.as_view()),
url('^tv_show/(?P<pk>\d+)/$', views.DetailTvShow.as_view()),
]
15 changes: 15 additions & 0 deletions tv_shows/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# todos/views.py
from rest_framework import generics

from . import models
from . import serializers


class ListTvShow(generics.ListCreateAPIView):
queryset = models.TvShow.objects.all()
serializer_class = serializers.TvShowSerializer


class DetailTvShow(generics.RetrieveUpdateDestroyAPIView):
queryset = models.TvShow.objects.all()
serializer_class = serializers.TvShowSerializer

0 comments on commit 648f09f

Please sign in to comment.