Exemple #1
0
def rescanLibrary(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'LIBRARY_ID' in response:
            library = strip_tags(response['LIBRARY_ID'])
            if Library.objects.filter(id=library).count() == 1:
                library = Library.objects.get(id=library)

                # Check if the library is not used somewhere else
                if library.playlist.isScanned:
                    # Delete all the old tracks
                    library.playlist.delete()

                    # Recreating playlist
                    playlist = Playlist()
                    playlist.name = library.name
                    playlist.user = request.user
                    playlist.isLibrary = True
                    playlist.save()
                    library.playlist = playlist
                    library.save()

                    # Scan library
                    data = scanLibrary(library, playlist, library.convertID3)
                else:
                    data = errorCheckMessage(False, "rescanError")
            else:
                data = errorCheckMessage(False, "dbError")
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Exemple #2
0
def newLibrary(request):
    if request.method == 'POST':
        user = request.user
        if checkPermission(["LIBR"], user):
            response = json.loads(request.body)
            if 'URL' in response and 'NAME' in response:
                dirPath = response['URL']
                if os.path.isdir(dirPath):
                    # Removing / at the end of the dir path if present
                    if dirPath.endswith("/"):
                        dirPath = dirPath[:-1]
                    library = Library()
                    library.path = dirPath
                    playlist = Playlist()
                    playlist.user = user
                    playlist.isLibrary = True
                    playlist.name = strip_tags(response['NAME'])
                    playlist.save()
                    library.playlist = playlist
                    library.save()
                    data = {
                        'LIBRARY_ID': library.id,
                        'LIBRARY_NAME': library.playlist.name,
                    }
                    data = {**data, **errorCheckMessage(True, None)}
                else:
                    data = errorCheckMessage(False, "dirNotFound")
            else:
                data = errorCheckMessage(False, "badFormat")
        else:
            data = errorCheckMessage(False, "permissionError")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Exemple #3
0
def newPlaylist(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'NAME' in response:
            playlist = Playlist()
            playlist.name = strip_tags(response['NAME'])
            playlist.user = request.user
            playlist.save()
            data = {
                'PLAYLIST_ID': playlist.id,
                'NAME': playlist.name,
            }
            data = {**data, **errorCheckMessage(True, None)}
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Exemple #4
0
def newLibrary(request):
    if request.method == 'POST':
        user = request.user
        if checkPermission(["LIBR"], user):
            response = json.loads(request.body)
            if 'URL' in response and 'NAME' in response:
                dirPath = response['URL']
                if os.path.isdir(dirPath):
                    # Removing / at the end of the dir path if present
                    if dirPath.endswith("/"):
                        dirPath = dirPath[:-1]
                    library = Library()
                    library.path = dirPath
                    playlist = Playlist()
                    playlist.user = user
                    playlist.isLibrary = True
                    playlist.name = strip_tags(response['NAME'])
                    playlist.save()
                    library.playlist = playlist
                    library.save()
                    data = {
                        'INFO': {
                            'ID': library.id,
                            'NAME': library.playlist.name,
                            'DESCRIPTION': library.playlist.description,
                            'IS_PUBLIC': library.playlist.isPublic,
                            'IS_LIBRARY': library.playlist.isLibrary,
                            'TOTAL_TRACK': "TO BE IMPLEMENTED",
                            'TOTAL_DURATION': "TO BE IMPLEMENTED",
                            'AVERAGE_BITRATE': "TO BE IMPLEMENTED",
                            'OWNER': library.playlist.user.username
                        }
                    }
                    data = {**data, **errorCheckMessage(True, None, newLibrary)}
                else:
                    data = errorCheckMessage(False, ErrorEnum.DIR_NOT_FOUND, newLibrary)
            else:
                data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, newLibrary, user)
        else:
            data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, newLibrary, user)
    else:
        data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, newLibrary)
    return JsonResponse(data)
Exemple #5
0
def initialScan(request):
    print("Asked for initial scan")
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'LIBRARY_ID' in response:
            library = Library.objects.get(id=response['LIBRARY_ID'])
            if os.path.isdir(library.path):
                playlist = Playlist()
                playlist.name = library.name
                playlist.user = request.user
                playlist.isLibrary = True
                playlist.save()
                data = scanLibrary(library, playlist, library.convertID3)
            else:
                data = errorCheckMessage(False, "dirNotFound")
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Exemple #6
0
def newPlaylist(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        user = request.user
        if checkPermission(["PLST"], user):
            if 'PLAYLIST_NAME' in response:
                playlist = Playlist()
                playlist.name = strip_tags(response['PLAYLIST_NAME'])
                playlist.user = request.user
                playlist.save()
                data = {
                    'PLAYLIST_ID': playlist.id,
                    'PLAYLIST_NAME': playlist.name,
                }
                data = {**data, **errorCheckMessage(True, None, newPlaylist)}
            else:
                data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, newPlaylist, user)
        else:
            data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, newPlaylist, user)
    else:
        data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, newPlaylist)
    return JsonResponse(data)
Exemple #7
0
class AppTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        super(AppTestCase, cls).setUpClass()

        logging.disable(logging.CRITICAL)

    def setUp(self):
        self.username = '******'
        self.email = '*****@*****.**'
        self.password = '******'
        self.user = User.objects.create_user(username=self.username,
                                             email=self.email,
                                             password=self.password)
        self.client = Client()
        self.client.login(username=self.username, password=self.password)

        self.anonymous_client = Client()

        self.playlist = Playlist(user=self.user)
        self.playlist.save()

        self.channel = Channel.objects.create(user=self.user,
                                              title='Testing Playlist',
                                              duration='150',
                                              group='The best group',
                                              path='no path')
        self.channel.playlists.add(self.playlist)

        self.sample_m3u8 = '\n'.join([
            '#EXTM3U', '#EXTINF:0,BBC NEWS', '#EXTGRP:News',
            'http://example.com/bbc-news-tv.m3u8', '#EXTINF:0,Fox NEWS',
            '#EXTGRP:News', 'http://example.com/fox-news-tv.m3u8',
            '#EXTINF:Invalid channel',
            'http://example.com/invalid-channel.m3u8'
        ])

    def test_urls(self):
        urls = [
            'index', 'create-playlist', 'new-channel', 'channels', 'login',
            'logout'
        ]

        social_auth_redirect_urls = [
            '/login/facebook/',
            '/login/vk-oauth2/',
        ]

        for url in urls:
            response = self.client.get(reverse(url), follow=True)
            self.assertEqual(response.status_code,
                             200,
                             msg='Unable to open: %s' % url)

        for url in social_auth_redirect_urls:
            response = self.client.get(url)
            self.assertEqual(response.status_code,
                             302,
                             msg='Unable to open: %s' % url)

    def test_playlist(self):
        self.assertGreater(self.playlist.count, 0)

    def test_playlist_public_link(self):
        self.assertIsNotNone(self.playlist.public_link)

        response = self.client.get(self.playlist.public_link)
        self.assertEqual(response.status_code, 200)

    def test_channel_link(self):
        self.assertIsNotNone(self.channel.get_absolute_url())

    def test_channel_update(self):
        response = self.client.get(self.channel.get_absolute_url())
        self.assertEqual(response.status_code, 200)

        response = self.anonymous_client.get(self.channel.get_absolute_url())
        self.assertEqual(response.status_code, 302)
        self.assertIn('login',
                      response.url,
                      msg='Not redirected to login view')

    def test_load_from_file(self):
        m3u8_file = SimpleUploadedFile("playlist.m3u8",
                                       str.encode(self.sample_m3u8),
                                       content_type='application/x-mpegURL')
        load_m3u8_from_file(m3u8_file, self.playlist, remove_existed=True)

        self.assertEqual(self.playlist.count, 2)

    @requests_mock.mock()
    def test_load_remote_m3u8(self, m):

        mocked_path = 'http://example.com/playlist.m3u8'
        m.get(mocked_path, text=self.sample_m3u8)

        load_remote_m3u8(mocked_path, self.playlist, remove_existed=True)

        self.assertEqual(self.playlist.count, 2)

    def test_simple_extinf(self):
        channel_string = '#EXTINF:-1,RTV 4 HD'
        chf = M3U8ChannelFactory()
        chf.process_line(channel_string)
        self.assertEqual('-1', chf.duration)
        self.assertEqual('RTV 4 HD', chf.title)

    def test_bytestring_extinf(self):
        channel_string = b'#EXTINF:-1 tvg-id="Omreop Fryslan NL" tvg-name="Omrop Fryslan NL" ' \
                         b'tvg-logo="http://1.1.1.1/picons/omropfryslannl.png" ' \
                         b'group-title="Netherland",Omrop Fryslan NL'
        chf = M3U8ChannelFactory()
        chf.process_line(channel_string)
        self.assertEqual('-1', chf.duration)
        self.assertEqual('Netherland', chf.extra_data.get('group-title'))
        self.assertEqual('Omreop Fryslan NL', chf.extra_data.get('tvg-ID'))
        self.assertEqual('Omrop Fryslan NL', chf.extra_data.get('tvg-name'))
        self.assertEqual('http://1.1.1.1/picons/omropfryslannl.png',
                         chf.extra_data.get('tvg-logo'))

    def test_simple_extinf_without_title(self):
        channel_string = '#EXTINF:25,'
        chf = M3U8ChannelFactory()
        chf.process_line(channel_string)
        self.assertEqual('25', chf.duration)
        self.assertEqual('', chf.title)

    def test_complex_extinf(self):
        channel_string = '#EXTINF:-1 ' \
                         'tvg-id="12" ' \
                         'tvg-name="Cinema Pro ARB" ' \
                         'tvg-logo="http://m3u8.pzbz.ru/logo.png" ' \
                         'group-title="Arab Countries",Cinema Pro ARB'
        chf = M3U8ChannelFactory()
        chf.process_line(channel_string)
        self.assertEqual(
            '-1',
            chf.duration,
        )
        self.assertEqual(
            'Cinema Pro ARB',
            chf.title,
        )
        self.assertEqual('12', chf.extra_data['tvg-ID'])
        self.assertEqual('Cinema Pro ARB', chf.extra_data['tvg-name'])
        self.assertEqual('http://m3u8.pzbz.ru/logo.png',
                         chf.extra_data['tvg-logo'])
        self.assertEqual('Arab Countries', chf.extra_data['group-title'])

    def test_bad_extinf(self):
        channel_string = '#EXTINF:Cool, but no duration'
        chf = M3U8ChannelFactory()
        chf.process_line(channel_string)

        self.assertFalse(chf.is_complete())

    def test_url_replace_tags(self):

        factory = RequestFactory()
        request = factory.get('/list/?q=HD&page=2')
        res_url_query = QueryDict(url_replace(request, 'page', 3))

        self.assertEqual({'q': 'HD', 'page': '3'}, res_url_query.dict())

        request = factory.get('/list')
        res_url = url_replace(request, 'page', 3)

        self.assertEqual('page=3', res_url)