diff --git a/awesome100/settings.py b/awesome100/settings.py index 23d61c1..e5ea26d 100644 --- a/awesome100/settings.py +++ b/awesome100/settings.py @@ -50,6 +50,7 @@ 'python_libraries', 'singers', 'songs', + 'tv_shows', 'writers', 'rest_framework', ] diff --git a/awesome100/urls.py b/awesome100/urls.py index 3755c0c..0565832 100644 --- a/awesome100/urls.py +++ b/awesome100/urls.py @@ -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) diff --git a/templates/index.html b/templates/index.html index 2aafa97..b14b4bd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -11,6 +11,7 @@

Welcome

  • 100 greatest authors of all time
  • 100 greatest movies of all time
  • 100 greatest singers of all time +
  • 100 greatest tv_shows of all time diff --git a/tv_shows/__init__.py b/tv_shows/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tv_shows/admin.py b/tv_shows/admin.py new file mode 100644 index 0000000..c671e04 --- /dev/null +++ b/tv_shows/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from tv_shows.models import TvShow + +admin.site.register(TvShow) diff --git a/tv_shows/apps.py b/tv_shows/apps.py new file mode 100644 index 0000000..c25b2ca --- /dev/null +++ b/tv_shows/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class TvShowsConfig(AppConfig): + name = 'tv_shows' diff --git a/tv_shows/get_tv_shows.py b/tv_shows/get_tv_shows.py new file mode 100644 index 0000000..49b2133 --- /dev/null +++ b/tv_shows/get_tv_shows.py @@ -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))) diff --git a/tv_shows/management/commands/import_best_tv_shows.py b/tv_shows/management/commands/import_best_tv_shows.py new file mode 100644 index 0000000..14e91f4 --- /dev/null +++ b/tv_shows/management/commands/import_best_tv_shows.py @@ -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 diff --git a/tv_shows/migrations/0001_initial.py b/tv_shows/migrations/0001_initial.py new file mode 100644 index 0000000..8bf5d7f --- /dev/null +++ b/tv_shows/migrations/0001_initial.py @@ -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)), + ], + ), + ] diff --git a/tv_shows/migrations/__init__.py b/tv_shows/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tv_shows/models.py b/tv_shows/models.py new file mode 100644 index 0000000..fea69a9 --- /dev/null +++ b/tv_shows/models.py @@ -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 diff --git a/tv_shows/serializers.py b/tv_shows/serializers.py new file mode 100644 index 0000000..b47cda7 --- /dev/null +++ b/tv_shows/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from . import models + + +class TvShowSerializer(serializers.ModelSerializer): + class Meta: + model = models.TvShow + fields = '__all__' diff --git a/tv_shows/urls.py b/tv_shows/urls.py new file mode 100644 index 0000000..88b00e2 --- /dev/null +++ b/tv_shows/urls.py @@ -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\d+)/$', views.DetailTvShow.as_view()), +] diff --git a/tv_shows/views.py b/tv_shows/views.py new file mode 100644 index 0000000..a5a7263 --- /dev/null +++ b/tv_shows/views.py @@ -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