Exemplo n.º 1
0
        public List<MusiсDir> SupplementMusicDirs(IDictionary<long, string> mdirs)
        {
            var newDirs = new List<MusiсDir>();
            var newOffsets = new List<DownloadOffset>(); 
            foreach (var key in mdirs.Keys.Where(id => Context.MusiсDirs.Count(md => md.Gid == id) == 0))
            {
                var name = mdirs[key];

                var dir = new MusiсDir();
                dir.Gid = key;
                dir.NameDir = mdirs[key];
                dir.NegativeRating = 0;
                dir.PositiveRating = 0;
                newDirs.Add(dir);

                var offset = new DownloadOffset();
                offset.MusiсDir = dir;
                offset.Offset = 0;
                newOffsets.Add(offset);
            }
            Context.MusiсDirs.AddRange(newDirs);
            Context.DownloadOffsets.AddRange(newOffsets);
            Context.SaveChanges();
            return newDirs;
        }
Exemplo n.º 2
0
Arquivo: VKApi.cs Projeto: rk10/EMusic
        public static void GetGroupTracks(MusiсDir dir, long offset, Action<IList<Track>> callback)
        {
            var method = "audio.get";

            var param = new Dictionary<string, object>();
            param["offset"] = offset.ToString();
            param["count"] = 100;
            param["owner_id"] = "-" + dir.Gid;
            param["version"] = @"5.34";

            using (var wc = new WebClient())
            {
                //wc.Proxy.Credentials = CredentialCache.DefaultCredentials;
                wc.DownloadStringCompleted += (s, e) =>
                {
                    if (e.Result != null)
                    {
                        var result = new List<Track>();

                        var json = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(e.Result);

                        var root = json["response"] as ArrayList;

                        foreach (var dirInfo in root)
                            //if (dirInfo is Dictionary<string, object>)
                            {
                                var tmp = dirInfo as Dictionary<string, object>;

                                if (tmp != null && 
                                    tmp.ContainsKey("aid") && 
                                    tmp.ContainsKey("url") &&
                                    tmp.ContainsKey("artist") &&
                                    tmp.ContainsKey("duration") &&
                                    tmp.ContainsKey("title"))
                                {
                                    var track = new Track()
                                    {
                                        Tid = (int)tmp["aid"],
                                        MusicDirID = dir.MusicDirID,
                                        TrackAuthor = tmp["artist"] as string,
                                        TrackDuration = (int)tmp["duration"],
                                        TrackUrl = tmp["url"] as string,
                                        TrackName = tmp["title"] as string
                                    };
                                    result.Add(track);
                                }
                            }

                        callback(result);
                    }
                };
                wc.DownloadStringAsync(Request(method, param));
            }
        }
Exemplo n.º 3
0
 public void CloseDirForSearch(MusiсDir dir)
 {
     dir.CloseForSearch = 1;
     Context.SaveChanges();
 }
Exemplo n.º 4
0
        private void GetFromCash(MusiсDir dir, Action<Track> callback)
        {
            if (!_cashTracks.ContainsKey(dir.MusicDirID))
                _cashTracks[dir.MusicDirID] = new List<Track>();

            int offset = _cachOffsets.ContainsKey(dir.MusicDirID) ? _cachOffsets[dir.MusicDirID] : 0;

            if (_cashTracks == null || _cashTracks[dir.MusicDirID].Count == 0)
            {
                VKApi.GetGroupTracks(dir, offset, dict =>
                {
                    _cashTracks[dir.MusicDirID].AddRange(dict.Where(k => Context.Tracks.Count(t => t.Tid == k.Tid) == 0).ToList());

                    if (_cashTracks[dir.MusicDirID].Count == 0 && dict.Count < 100)
                        callback(null);
                    else
                    {
                        _cachOffsets[dir.MusicDirID] = offset;
                        GetFromCash(dir, callback);
                    }
                });
            }
            else
            {
                int index = _rand.Next(_cashTracks[dir.MusicDirID].Count);

                Track track = null;

                do
                {
                    if (_cashTracks[dir.MusicDirID].Count == 0)
                    {
                        track = null;
                        break;
                    }

                    track = _cashTracks[dir.MusicDirID][index];
                    _cashTracks[dir.MusicDirID].RemoveAt(index);
                    index = (index + 1) % _cashTracks[dir.MusicDirID].Count();
                }
                while (Context.Tracks.Count(t => t.Tid == track.Tid) > 0);

                if (track == null)
                {
                    _cachOffsets[dir.MusicDirID] = offset + 100;
                    GetFromCash(dir, callback);
                }
                else
                {
                    callback(track);
                }
            }
        }