This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from sks444/sks
Import 100 greatest singers of all time
- Loading branch information
Showing
14 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
'movies', | ||
'novels', | ||
'python_libraries', | ||
'singers', | ||
'songs', | ||
'writers', | ||
'rest_framework', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from django.contrib import admin | ||
from singers.models import Singer | ||
|
||
admin.site.register(Singer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class SingersConfig(AppConfig): | ||
name = 'singers' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from requests_html import HTMLSession | ||
|
||
from singers.models import Singer | ||
|
||
session = HTMLSession() | ||
r = session.get('http://www.imdb.com/list/ls000050684/') | ||
singers = r.html.find('.lister-item') | ||
|
||
|
||
def import_data(i, singer): | ||
try: | ||
rank = i | ||
url = singer.absolute_links.pop() | ||
name = singer.find('a')[1].text | ||
best_song = singer.find('a')[2].text | ||
summary = singer.find('p')[1].text | ||
image_url = singer.find('img')[0].attrs.get('src') | ||
except Exception as ex: | ||
print(str(ex)) | ||
try: | ||
c, created = Singer.objects.get_or_create( | ||
rank=rank, | ||
url=url, | ||
name=name, | ||
best_song=best_song, | ||
summary=summary, | ||
image_url=image_url, | ||
) | ||
if created: | ||
c.save() | ||
print('\nSinger, {}, has been saved.'.format(c)) | ||
except Exception as ex: | ||
print('\n\nSomething went wrong saving this singer: {}\n{}' | ||
.format(name, str(ex))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from singers.get_authors import singers, import_data | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Import Singers data' | ||
|
||
SINGERS = staticmethod(singers) | ||
IMPORT_DATA = staticmethod(import_data) | ||
|
||
def handle(self, *args, **options): | ||
i = 1 | ||
for singer in self.SINGERS: | ||
self.IMPORT_DATA(i, singer) | ||
i = i + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.12 on 2018-04-15 18:05 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Singer', | ||
fields=[ | ||
('rank', models.IntegerField(primary_key=True, serialize=False)), | ||
('name', models.CharField(max_length=300, null=True)), | ||
('url', models.URLField(null=True)), | ||
('image_url', models.URLField(null=True)), | ||
('summary', models.TextField(null=True)), | ||
('best_song', models.CharField(max_length=500, null=True)), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.db import models | ||
|
||
|
||
class Singer(models.Model): | ||
rank = models.IntegerField(primary_key=True) | ||
name = models.CharField(max_length=300, null=True) | ||
url = models.URLField(null=True) | ||
image_url = models.URLField(null=True) | ||
summary = models.TextField(null=True) | ||
best_song = models.CharField(max_length=500, null=True) | ||
|
||
def __str__(self): | ||
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from rest_framework import serializers | ||
from . import models | ||
|
||
|
||
class SingerSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = models.Singer | ||
fields = '__all__' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('^singers/$', views.ListSinger.as_view()), | ||
url('^singer/(?P<pk>\d+)/$', views.DetailSinger.as_view()), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ListSinger(generics.ListCreateAPIView): | ||
queryset = models.Singer.objects.all() | ||
serializer_class = serializers.SingerSerializer | ||
|
||
|
||
class DetailSinger(generics.RetrieveUpdateDestroyAPIView): | ||
queryset = models.Singer.objects.all() | ||
serializer_class = serializers.SingerSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters