<commit_before>﻿/*-
 * Copyright (c) 2014 Software Development Solutions, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using libspotifydotnet;
using System.Diagnostics;
using Anotar.NLog;
using System.Reflection;
using System.IO;

namespace SpotifyClient
{
    public static class Spotify
    {
        private delegate bool Test();

        public delegate void MainThreadMessageDelegate(object[] args);

        private static AutoResetEvent _programSignal;
        private static AutoResetEvent _mainSignal;
        private static Queue<MainThreadMessage> _mq = new Queue<MainThreadMessage>();
        private static bool _shutDown = false;
        private static object _syncObj = new object();
        private static object _initSync = new object();
        private static bool _initted = false;
        private static bool _isRunning = false;
        private static bool _isLoggedIn = false;
        private static Action<IntPtr> d_notify = new Action<IntPtr>(Session_OnNotifyMainThread);
        private static Action<IntPtr> d_on_logged_in = new Action<IntPtr>(Session_OnLoggedIn);
        private static Thread _t;
        private static object _loginLock = new object();

        private static readonly int REQUEST_TIMEOUT = 20;
        private static readonly string LOG_MODULE = "Spotify";

        private class MainThreadMessage
        {
            public MainThreadMessageDelegate d;
            public object[] payload;
        }

        static Spotify()
        {
            Session.OnNotifyMainThread += d_notify;
            Session.OnLoggedIn += d_on_logged_in;
        }

        public static bool IsRunning
        {
            get { return _isRunning; }
        }

        public static bool IsLoggedIn
        {
            get { return _isLoggedIn; }
        }

        public static bool Login(string appname, string username, string password)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var resourceName = "spotify_appkey.key";
            
            byte[] appkey = null;
            using (Stream stream = assembly.GetManifestResourceStream(assembly.GetName().Name + '.' + resourceName))
            {
                if (stream != null)
                {
                    appkey = new byte[stream.Length];
                    stream.Read(appkey, 0, appkey.Length);
                }
            }

            if (appkey == null)
            {
                LogTo.Debug("Appkey not embedded, trying to read from spotify_appkey.key");
                appkey = File.ReadAllBytes(resourceName);
            }

            lock (_initSync)
            {
                if (!_initted)
                    throw new ApplicationException("Spotify message thread not initialized");
            }

            lock (_loginLock)
            {
                if (_isLoggedIn)
                    return true;

                postMessage(Session.Login, new object[] { appkey, username, password, appname });

                _programSignal.WaitOne();

                if (Session.LoginError != libspotify.sp_error.OK)
                {
                    Log.Error(Plugin.LOG_MODULE, "Login failed: {0}", libspotify.sp_error_message(Session.LoginError));<commit_msg>Print actual name of resource after failed fetch<commit_after>﻿/*-
 * Copyright (c) 2014 Software Development Solutions, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using libspotifydotnet;
using System.Diagnostics;
using Anotar.NLog;
using System.Reflection;
using System.IO;

namespace SpotifyClient
{
    public static class Spotify
    {
        private delegate bool Test();

        public delegate void MainThreadMessageDelegate(object[] args);

        private static AutoResetEvent _programSignal;
        private static AutoResetEvent _mainSignal;
        private static Queue<MainThreadMessage> _mq = new Queue<MainThreadMessage>();
        private static bool _shutDown = false;
        private static object _syncObj = new object();
        private static object _initSync = new object();
        private static bool _initted = false;
        private static bool _isRunning = false;
        private static bool _isLoggedIn = false;
        private static Action<IntPtr> d_notify = new Action<IntPtr>(Session_OnNotifyMainThread);
        private static Action<IntPtr> d_on_logged_in = new Action<IntPtr>(Session_OnLoggedIn);
        private static Thread _t;
        private static object _loginLock = new object();

        private static readonly int REQUEST_TIMEOUT = 20;
        private static readonly string LOG_MODULE = "Spotify";

        private class MainThreadMessage
        {
            public MainThreadMessageDelegate d;
            public object[] payload;
        }

        static Spotify()
        {
            Session.OnNotifyMainThread += d_notify;
            Session.OnLoggedIn += d_on_logged_in;
        }

        public static bool IsRunning
        {
            get { return _isRunning; }
        }

        public static bool IsLoggedIn
        {
            get { return _isLoggedIn; }
        }

        public static bool Login(string appname, string username, string password)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var resourceName = "spotify_appkey.key";
            
            byte[] appkey = null;
            using (Stream stream = assembly.GetManifestResourceStream(assembly.GetName().Name + '.' + resourceName))
            {
                if (stream != null)
                {
                    appkey = new byte[stream.Length];
                    stream.Read(appkey, 0, appkey.Length);
                }
            }

            if (appkey == null)
            {
                LogTo.Debug("Appkey not embedded, trying to read from {0}", resourceName);
                appkey = File.ReadAllBytes(resourceName);
            }

            lock (_initSync)
            {
                if (!_initted)
                    throw new ApplicationException("Spotify message thread not initialized");
            }

            lock (_loginLock)
            {
                if (_isLoggedIn)
                    return true;

                postMessage(Session.Login, new object[] { appkey, username, password, appname });

                _programSignal.WaitOne();

                if (Session.LoginError != libspotify.sp_error.OK)
                {
                    Log.Error(Plugin.LOG_MODULE, "Login failed: {0}", libspotify.sp_error_message(Session.LoginError));
{'fraction_non_alphanumeric': 0.06805944432479001, 'fraction_numerical': 0.0022614688778806805, 'mean_word_length': 2.3820101966496723, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2325051', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2187, 'n_words': 835}