Browse Source

some work on handling bad inputs in the api and giving useful feedback in the extension

py3-upgrade
noah 2 years ago
parent
commit
5a8c424524
3 changed files with 28 additions and 13 deletions
  1. +15
    -10
      api/scraper.py
  2. +11
    -3
      api/views.py
  3. +2
    -0
      browser/src/js/background.js

+ 15
- 10
api/scraper.py View File

@ -68,16 +68,21 @@ class Scraper():
raise UnsupportedPlatformError("No supported platform found :(")
def bandcamp(self):
artist = self.soup.select("span[itemprop='byArtist'] > a")[0]
self.artist = artist.getText().strip()
title = self.soup.find("h2",
{'class': 'trackTitle', 'itemprop': 'name'})
self.title = title.getText().strip()
meta = self.soup.find("meta", property='og:video:secure_url')
embed = dict(meta.attrs)['content']
self.embed = embed.replace("tracklist=false", "tracklist=true")
try:
artist = self.soup.select("span[itemprop='byArtist'] > a")[0]
self.artist = artist.getText().strip()
title = self.soup.find("h2",
{'class': 'trackTitle', 'itemprop': 'name'})
self.title = title.getText().strip()
meta = self.soup.find("meta", property='og:video:secure_url')
embed = dict(meta.attrs)['content']
self.embed = embed.replace("tracklist=false", "tracklist=true")
except IndexError:
# this is the failure case for pages on the bandcamp site
# that aren't streamable
raise UnsupportedPlatformError("No eligible content found.")
return


+ 11
- 3
api/views.py View File

@ -3,7 +3,8 @@ from __future__ import unicode_literals
import json
import logging
from django.http import HttpResponse, Http404, HttpResponseForbidden
from django.http import HttpResponse, Http404, HttpResponseBadRequest,\
HttpResponseForbidden
from django.contrib.auth import get_user_model
from django.views import View
from django.views.decorators.csrf import csrf_exempt
@ -39,10 +40,17 @@ class Queue(ListCreateAPIView):
@method_decorator(csrf_exempt)
def post(self, request, format=None):
data = {"user": request.user.pk}
# previously here we listened for the UnsupportedPlatformError and
# re-raised it. not sure why. but that will either swallow other Errors
# or require great duplication eventually. ugh.
uri = request.data.get('uri')
if not uri:
return HttpResponseBadRequest("No valid uri was provided.")
try:
scrape = Scraper(request.data.get('uri'))
scrape = Scraper(uri)
except UnsupportedPlatformError:
raise
return HttpResponseBadRequest("Unrecognized platform.")
data.update(scrape.result())


+ 2
- 0
browser/src/js/background.js View File

@ -14,6 +14,8 @@ const onErr = function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status === 401) {
// some kind of error message would be good
UI.message({'action': 'force_login', 'msg': jqXHR.responseText})
} else if (jqXHR.status === 400) {
UI.message({'action': 'error', 'msg': jqXHR.responseText})
} else if (jqXHR.status === 415) {
UI.message({'action': 'error', 'msg': "No supported platform was found."})
} else {


Loading…
Cancel
Save