Beispiel #1
0
        public override bool Insert(string json)
        {
            var data = JsonConvert.DeserializeObject <RootObject>(json);

            if (data == null || !data.Songlog.Any())
            {
                return(false);
            }

            if (data.pageSize < data.Songlog.Count)
            {
                var log = new ImportInfo();
                log.Messages.Add("data.pageSize < data.Songlog.Count: " + data.Id + " => " + data.pageSize + " < " + data.Songlog.Count);
                ImportLog.Add(log);
            }

            using (var db = new SrfPlayListContext(OptionsBuilder.Options))
            {
                foreach (var songLogData in data.Songlog)
                {
                    // check for already imported songlog
                    var songlog = db.Songlogs.Local.FirstOrDefault(s => s.id.Equals(songLogData.id));
                    if (songlog == default(Songlog))
                    {
                        songlog = db.Songlogs.FirstOrDefault(s => s.id.Equals(songLogData.id));
                    }
                    if (songlog != default(Songlog))
                    {
                        continue;
                    }

                    var artist = Artist.Add(db, songLogData);
                    var song   = Song.Add(db, songLogData, artist);

                    // checked for existing above
                    songlog = Songlog.Add(db, songLogData, song);

                    var log = new ImportInfo
                    {
                        Artist     = artist.name,
                        Song       = song.title,
                        PlayedDate = songlog.playedDate
                    };
                    ImportLog.Add(log);
                }

                db.SaveChanges();
            }

            return(true);
        }
Beispiel #2
0
        public override void Import(Guid channelId = default(Guid))
        {
            var makeInitialImport = CreateDb();

            var isChannelIdGiven = channelId != default(Guid);

            using (var wc = new WebClient())
            {
                foreach (var channelInfo in ChannelList)
                {
                    var dt      = new DateTime(2000, 01, 01);
                    var dtDelta = dt.AddDays(7);

                    if (!makeInitialImport)
                    {
                        if (isChannelIdGiven && !channelInfo.ChannelId.Equals(channelId))
                        {
                            continue;
                        }

                        var lastUpdate = GetLastUpdateTime(channelInfo.ChannelId);

                        if (lastUpdate != null)
                        {
                            dt      = new DateTime(lastUpdate.Value.Year, lastUpdate.Value.Month, lastUpdate.Value.Day).AddDays(-1);
                            dtDelta = dt.AddDays(7);
                        }
                    }

                    do
                    {
                        var fromDate = String.Format("{0}-{1}-{2}T00%3A00%3A00", dt.Year, dt.Month.ToString().PadLeft(2, '0'), dt.Day.ToString().PadLeft(2, '0'));
                        var toDate   = String.Format("{0}-{1}-{2}T23%3A59%3A59", dtDelta.Year, dtDelta.Month.ToString().PadLeft(2, '0'), dtDelta.Day.ToString().PadLeft(2, '0'));
                        var url      = String.Format(@"https://ws.srf.ch/songlog/log/channel/{0}.json?callback=songLogPollerCallback_musik&fromDate={1}&toDate={2}&page.size=10000&page.page=0&page.sort=playedDate&page.sort.dir=asc", channelInfo.ChannelId, fromDate, toDate);
                        var jsonText = wc.DownloadString(url);

                        jsonText = jsonText.Remove(jsonText.LastIndexOf(';'), 1).Remove(jsonText.LastIndexOf(')'), 1).Remove(0, jsonText.IndexOf('(') + 1);

                        var msg = $"Inserting stuff for timespan {dt.ToShortDateString()} - {dtDelta.ToShortDateString()}";
                        var log = new ImportInfo {
                            ChannelName = channelInfo.ChannelName
                        };
                        log.Messages.Add(msg);
                        ImportLog.Add(log);

                        if (!Insert(jsonText))
                        {
                            var logNothingInserted = new ImportInfo {
                                ChannelName = channelInfo.ChannelName
                            };

                            logNothingInserted.Messages.Add($"Nothing inserted for timespan {dt.ToShortDateString()} - {dtDelta.ToShortDateString()}");
                            ImportLog.Add(logNothingInserted);
                        }

                        dt      = dt.AddDays(8);
                        dtDelta = dt.AddDays(7);
                    } while (DateTime.Now > dt.AddDays(-8));

                    InsertOrUpdateMetaInfo(channelInfo.ChannelId, channelInfo.ChannelName);
                }
            }
        }