import url celanup

- only import first url
- pass empty string instead of undefined
- page can contain multiple videos, import as parts
This commit is contained in:
j 2016-08-10 10:49:29 +02:00
parent b59a9c3122
commit 8c928860e1
2 changed files with 80 additions and 74 deletions

View file

@ -89,8 +89,8 @@ def add_subtitles(item, media, tmp):
def download(item_id, url): def download(item_id, url):
item = Item.objects.get(public_id=item_id) item = Item.objects.get(public_id=item_id)
info = get_info(url) info = get_info(url)
if len(info) != 1: if not len(info):
return '%s contains %d videos' % (url, len(info)) return '%s contains no videos' % url
media = info[0] media = info[0]
cdir = os.path.abspath(os.curdir) cdir = os.path.abspath(os.curdir)
tmp = tempfile.mkdtemp().decode('utf-8') tmp = tempfile.mkdtemp().decode('utf-8')
@ -100,27 +100,33 @@ def download(item_id, url):
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True) stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
fname = list(os.listdir(tmp)) parts = list(os.listdir(tmp))
if fname: if parts:
fname = os.path.join(tmp, fname[0]) part = 1
oshash = ox.oshash(fname) for name in parts:
name = os.path.join(tmp, name)
oshash = ox.oshash(name)
f, created = models.File.objects.get_or_create(oshash=oshash) f, created = models.File.objects.get_or_create(oshash=oshash)
if created: if created:
f.data.name = f.get_path('data.' + fname.split('.')[-1]) f.data.name = f.get_path('data.' + name.split('.')[-1])
ox.makedirs(os.path.dirname(f.data.path)) ox.makedirs(os.path.dirname(f.data.path))
shutil.move(fname, f.data.path) shutil.move(name, f.data.path)
f.item = item f.item = item
f.info = ox.avinfo(f.data.path) f.info = ox.avinfo(f.data.path)
f.info['extension'] = media['extension'] f.info['extension'] = media['extension']
f.path = '%(title)s.%(extension)s' % media f.path = '%(title)s.%(extension)s' % media
f.parse_info() f.parse_info()
f.selected = True f.selected = True
if len(parts) > 1:
f.part = part
part += 1
f.save() f.save()
f.item.save() f.item.save()
f.extract_stream() f.extract_stream()
status = True status = True
else: else:
status = 'file exists' status = 'file exists'
if len(parts) == 1:
add_subtitles(f.item, media, tmp) add_subtitles(f.item, media, tmp)
else: else:
status = 'download failed' status = 'download failed'

View file

@ -84,7 +84,8 @@ pandora.ui.importMediaDialog = function(options) {
function addMedia(url, callback) { function addMedia(url, callback) {
pandora.api.getMediaUrlInfo({url: url}, function(result) { pandora.api.getMediaUrlInfo({url: url}, function(result) {
result.data.items.forEach(function(info) { // FIXME: support playlists / multiple items
var info = result.data.items[0];
var infoKeys = [ var infoKeys = [
'date', 'description', 'id', 'tags', 'date', 'description', 'id', 'tags',
'title', 'uploader', 'url' 'title', 'uploader', 'url'
@ -97,7 +98,7 @@ pandora.ui.importMediaDialog = function(options) {
value = info.tags; value = info.tags;
} else { } else {
infoKeys.forEach(function(infoKey) { infoKeys.forEach(function(infoKey) {
var infoValue = info[infoKey]; var infoValue = info[infoKey] || '';
if (key == 'year' && infoKey == 'date') { if (key == 'year' && infoKey == 'date') {
infoValue = infoValue.substr(0, 4); infoValue = infoValue.substr(0, 4);
} }
@ -138,7 +139,6 @@ pandora.ui.importMediaDialog = function(options) {
}); });
}); });
}); });
});
}; };
function getInfo(url, callback) { function getInfo(url, callback) {
@ -183,7 +183,7 @@ pandora.ui.importMediaDialog = function(options) {
if (items.length) { if (items.length) {
// FIXME: support playlists / multiple items // FIXME: support playlists / multiple items
var info = items[0]; var info = items[0];
$info.append($('<img>').css({ info.thumbnail && $info.append($('<img>').css({
position: 'absolute', position: 'absolute',
width: '248px' width: '248px'
}).attr('src', info.thumbnail)); }).attr('src', info.thumbnail));
@ -196,7 +196,7 @@ pandora.ui.importMediaDialog = function(options) {
width: '248px' width: '248px'
}).html( }).html(
'<span style="font-weight: bold">' + info.title '<span style="font-weight: bold">' + info.title
+ '</span><br/><br/>' + info.description + '</span><br/><br/>' + (info.description || '')
)); ));
that.enableButton('import'); that.enableButton('import');
} else { } else {