tooxie / shiva-server

A RESTful API to your music collection
http://shiva.readthedocs.org/en/latest/intro.html
MIT License
556 stars 51 forks source link

Music served through URL #53

Closed nishantmodak closed 11 years ago

nishantmodak commented 11 years ago

I have a MediaDir configured as

MediaDir(root='/home/ec2-user', dirs=('/music',), url='http://<ipaddres>:9002/'),

So, when the client loads, it shows up all the music correctly. But when played, it does not play because shiva-server returns a 404.

 [09/Apr/2013 11:13:05] "GET /music/file_name.mp3 HTTP/1.1" 404 

Indexing of these files works fine.

All of this works well, if url is localhost.

tooxie commented 11 years ago

If you want to serve files through Shiva you should leave out the url parameter. It's not recommended, though, to use Shiva as file server. In fact, that functionality will be removed eventually.

What you can do instead is use python's SimpleHTTPServer:

$ python -m SimpleHTTPServer

and point your MediaDir to it:

MediaDir(root='/home/ec2-user', dirs=('/music',), url='http://127.0.0.1:8000/'),

Let me know if this doesn't work.

nishantmodak commented 11 years ago

I thought about this proxy-ing requests. But I think the problem lies here in the first response from server

"stream_uri": "http://127.0.0.1:8000/music/file_name.mp3"

(the location where shiva-client requests for direct files).

Based on the :url field this stream_uri is generated. Now even, if I have a SimpleHTTPServer. it wont be able to locate the file at /music.

Thanks for taking the time to help me out on this. Am I missing something crucial?

tooxie commented 11 years ago

I think you are using an old version, could you please update? You will have to update the client as well, because the /track object changes. You won't have stream_uri but files instead.

Not sure that will fix it, but let's get that out of the way.

nishantmodak commented 11 years ago

I updated both but not able to set it up.

Traceback (most recent call last):
  File "/usr/bin/shiva-indexer", line 9, in <module>
    load_entry_point('Shiva==0.1.0', 'console_scripts', 'shiva-indexer')()
  File "/usr/lib/python2.7/site-packages/distribute-0.6.36-py2.7.egg/pkg_resources.py", line 343, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/site-packages/distribute-0.6.36-py2.7.egg/pkg_resources.py", line 2309, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/site-packages/distribute-0.6.36-py2.7.egg/pkg_resources.py", line 2020, in load
    raise ImportError("%r has no %r attribute" % (entry,attr))
ImportError: <module 'shiva.indexer' from '/home/ec2-user/shiva-server/shiva/indexer.pyc'> has no 'main' attribute

Figuring it out. Shall keep you posted.

tooxie commented 11 years ago

Ouch, that is a bug, yes :sweat_smile:

That file got lost during a rebase and seems that I recovered an old version of it. Will issue a fix later today. I'll let you know.

tooxie commented 11 years ago

Ok, https://github.com/tooxie/shiva-server/commit/86d3282ed6531c6a6d8329ce1c65062bff37612a fixes that.

nishantmodak commented 11 years ago

Strange, the server does start now. with 'shiva-server' but on starting the shiva-client. It just returns 404's.

nishantmodak commented 11 years ago

Now the client is up. But getting this on shiva-server

Traceback (most recent call last):
  File "/home/ec2-user/shiva-server/lib/python2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ec2-user/shiva-server/lib/python2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ec2-user/shiva-server/lib/python2.7/site-packages/Flask_RESTful-0.1.2-py2.7.egg/flask_restful/__init__.py", line 119, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/ec2-user/shiva-server/lib/python2.7/site-packages/Flask-0.9-py2.7.egg/flask/views.py", line 83, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/ec2-user/shiva-server/lib/python2.7/site-packages/Flask_RESTful-0.1.2-py2.7.egg/flask_restful/__init__.py", line 195, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/ec2-user/shiva-server/shiva/resources.py", line 87, in get
    return self.get_full_tree(artist)
  File "/home/ec2-user/shiva-server/shiva/resources.py", line 118, in get_full_tree
    _artist['albums'].append(albums.get_full_tree(album))
  File "/home/ec2-user/shiva-server/shiva/resources.py", line 204, in get_full_tree
    _album['tracks'].append(tracks.get_full_tree(track))
  File "/home/ec2-user/shiva-server/shiva/resources.py", line 304, in get_full_tree
    _track = marshal(track, self.get_resource_fields())
  File "/home/ec2-user/shiva-server/lib/python2.7/site-packages/Flask_RESTful-0.1.2-py2.7.egg/flask_restful/__init__.py", line 241, in marshal
    return OrderedDict(items)
  File "/usr/lib64/python2.7/collections.py", line 51, in __init__
    self.__update(*args, **kwds)
  File "/home/ec2-user/shiva-server/lib64/python2.7/_abcoll.py", line 499, in update
    for key, value in other:
  File "/home/ec2-user/shiva-server/lib/python2.7/site-packages/Flask_RESTful-0.1.2-py2.7.egg/flask_restful/__init__.py", line 240, in <genexpr>
    for k, v in fields.items())
  File "/home/ec2-user/shiva-server/shiva/fields.py", line 28, in output
    for mimetype in converter.get_mimetypes():
  File "/home/ec2-user/shiva-server/shiva/converter.py", line 36, in get_mimetypes
    yield MimeType(mimetype)
  File "/home/ec2-user/shiva-server/shiva/mimetype.py", line 21, in __init__
    self.set_mimetype(mimetype)
  File "/home/ec2-user/shiva-server/shiva/mimetype.py", line 36, in set_mimetype
    self.acodec = _mimetype['acodec']
KeyError: 'acodec'

Looks like I am going down a :rabbit2: hole here.

tooxie commented 11 years ago

Yes, I reproduced it. I better sit down tonight and make it all work properly instead of hotfixing it.

Support for converting files to different formats (other than mp3) was introduced, was a big change. Bugs were expected. Sorry for the inconveniences :sweat:

tooxie commented 11 years ago

Well, after an important refactor I got it to work :feelsgood:

Could you please try the bettermime branch? It changes the way mimetypes work. All you need to do is copy (and uncomment) the example lines to your local.py file. Remember to import the MimeType object.

nishantmodak commented 11 years ago

This isn't an easy going.

First, It errors out saying 'no module named exceptions' . I circumnavigated that for the moment by commenting resources.py Line 12. and converter.py line 8

After that it indexes appropriately. But fails again after starting the shiva-server . giving the above error again. . I am guessing if this has to do with Installation of ffmpeg with some specific flags, prerequisites?

tooxie commented 11 years ago

That's weird, seems like an incomplete push from my side... sorry for that. Anyway, the exceptions.py file is in place now.

Which error do you mean, exactly? The KeyError: 'acodec' should be gone by now, and the ImportError: <module 'shiva.indexer' from '/home/ec2-user/shiva-server/shiva/indexer.pyc'> has no 'main' attribute as well.

Remember to add the MIMETYPES setting:

MIMETYPES = (
    MimeType(type='audio', subtype='mp3', extension='mp3',
             acodec='libmp3lame'),
    MimeType(type='audio', subtype='ogg', extension='ogg',
             acodec='libvorbis'),
)

And the proper MediaDir configuration:

MediaDir(root='/home/ec2-user', dirs=('/music',), url='http://127.0.0.1:8000/'),

You also need ffmpeg intalled in your system. I have it running right now, both the server and the client; it converts and streams properly. Could you please update and post the errors you are still getting?

nishantmodak commented 11 years ago

Contents of local.py

MEDIA_DIRS = (
    # Examples.
    # MediaDir(root='/srv/http', dirs=('/music',), url='http://127.0.0.1/'),
    MediaDir('/home/ec2-user/music'),
    # If in doubt, read the source.
)
MIMETYPES = (
    MimeType(type='audio', subtype='mp3', extension='mp3',
             acodec='libmp3lame'),
    MimeType(type='audio', subtype='ogg', extension='ogg',
             acodec='libvorbis'),
)

Exception below, when the client requests for album details

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/lib/python2.7/site-packages/flask_restful/__init__.py", line 119, in wrapper
    resp = resource(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/flask/views.py", line 83, in view
    return self.dispatch_request(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/flask_restful/__init__.py", line 195, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/ec2-user/shiva-server/shiva/resources.py", line 87, in get
    return self.get_full_tree(artist)
  File "/home/ec2-user/shiva-server/shiva/resources.py", line 118, in get_full_tree
    _artist['albums'].append(albums.get_full_tree(album))
  File "/home/ec2-user/shiva-server/shiva/resources.py", line 204, in get_full_tree
    _album['tracks'].append(tracks.get_full_tree(track))
  File "/home/ec2-user/shiva-server/shiva/resources.py", line 304, in get_full_tree
    _track = marshal(track, self.get_resource_fields())
  File "/usr/lib/python2.7/site-packages/flask_restful/__init__.py", line 241, in marshal
    return OrderedDict(items)
  File "/usr/lib64/python2.7/collections.py", line 51, in __init__
    self.__update(*args, **kwds)
  File "/usr/lib64/python2.7/_abcoll.py", line 499, in update
    for key, value in other:
  File "/usr/lib/python2.7/site-packages/flask_restful/__init__.py", line 240, in <genexpr>
    for k, v in fields.items())
  File "/home/ec2-user/shiva-server/shiva/fields.py", line 31, in output
    converter = ConverterClass(track.path, mimetype)
  File "/home/ec2-user/shiva-server/shiva/converter.py", line 27, in __init__
    self.set_mimetype(mimetype)
  File "/home/ec2-user/shiva-server/shiva/converter.py", line 49, in set_mimetype
    if _mime.matches(mimetype):
AttributeError: 'str' object has no attribute 'matches'
tooxie commented 11 years ago

Did you update your client as well? Are you sure you have the MIMETYPES setting defined only once? Do you have a string as well in that tuple? Could you please check the files local.py and project.py?

The set_mimetype method of the converter.py file iterates through all the mimetypes defined in the settings and calls the matches() method of each to find out if the string version of the mimetype (i.e., 'audio/mp3') matches any of the objects in the MIMETYPES tuple.

There must be a string somewhere in that tuple, which obviously doesn't have the matches method.

tooxie commented 11 years ago

Also, make sure to delete every pyc file find . -name '*.pyc' -delete, just in case.

tooxie commented 11 years ago

The branch was merged into master and seems to be working fine. I'll close this issue. Reopen it if you still encounter this error. Thanks.

nishantmodak commented 11 years ago

Now, keep getting an error on the client.

TypeError: Cannot call method 'substr' of null
    at Object.Shiva.Player.setTrack (http://localhost:9001/js/controllers.js:278:18)
    at Object.Shiva.Playlist.addOne (http://localhost:9001/js/controllers.js:136:26)
    at http://localhost:9001/js/angular-1.1.1/angular.js:6201:19
    at http://localhost:9001/js/angular-1.1.1/angular.js:12805:13
    at Object.Scope.$eval (http://localhost:9001/js/angular-1.1.1/angular.js:7848:28)
    at Object.Scope.$apply (http://localhost:9001/js/angular-1.1.1/angular.js:7928:23)
    at HTMLAnchorElement.<anonymous> (http://localhost:9001/js/angular-1.1.1/angular.js:12804:17)
    at http://localhost:9001/js/angular-1.1.1/angular.js:1926:10
    at Array.forEach (native)
    at forEach (http://localhost:9001/js/angular-1.1.1/angular.js:110:11) 

Path is null

path = track.files[mime];
       if (path.substr(0, 1) === '/') {
           path = '/api' + path;
       }

Since, track structure is as

Object {files: Object, album: Object, length: 2394, number: 0, title: "Savitri.Book1.Canto 1"…}
$$hashKey: "006"
album: Object
artist: Object
bitrate: 239920
files: Object
audio/mp3: null
audio/ogg: null
__proto__: Object
id: 1
length: 2394
number: 0
slug: "savitri-book1-canto-1-1"
title: "Savitri.Book1.Canto 1"
uri: "/track/1"
__proto__: Object
tooxie commented 11 years ago

Could you please paste the MIMETYPES config exactly as you have it in your local.py? That behaviour is really strange... :confused:

nishantmodak commented 11 years ago

Following is the entire content of local.py

# -*- coding: utf-8 -*-
from shiva.media import MediaDir, MimeType

SECRET_KEY = ''  # Set this to something secret.
MEDIA_DIRS = (
    # Examples.
    # MediaDir(root='/srv/http', dirs=('/music',), url='http://127.0.0.1/'),
    MediaDir('/home/nm/savitri-mp3'),
    # If in doubt, read the source.
)
# Add your own mimetypes here.
MIMETYPES = (
    MimeType(type='audio', subtype='mp3', extension='mp3',
             acodec='libmp3lame'),
    MimeType(type='audio', subtype='ogg', extension='ogg',
             acodec='libvorbis'),
)
LASTFM_API_KEY = ''
SCRAPERS = {
    'lyrics': (
        'azlyrics.AZLyrics',
        'metrolyrics.MetroLyrics',
        'letrascanciones.MP3Lyrics',
    ),
}
METROLYRICS_API_KEY = '1234567890123456789012345678901234567890'
BANDSINTOWN_APP_ID = 'SHIVA_APP_ID'
# Here you can redefine anything you set in your settings.project file, like
# the database URI.
tooxie commented 11 years ago

Files won't be served through Shiva directly anymore, instead, I added a file server that will run independently. Please checkout the fileserver branch. You will need to run the following:

$ python setup.py develop
$ shiva-fileserver

That will put a file server to listen in the port 8001, dedicated to serving files. Please let me know if this works.

nishantmodak commented 11 years ago

The setup.py from fileserver branch isn't updated with file-server requirements?

nishantmodak commented 11 years ago

7e26371a84e2c7d137a0330567d0178b498ca442 Closed!

:happy:

tooxie commented 11 years ago

Ufff... finally!! :beers:

Thanks!