public UnexpectedAniDBResponseException(string message, AniDBUDPReturnCode code, string response) : base(
         $"Unexpected AniDB Response: {message} -- {code} | {response}")
 {
     Response   = response;
     ReturnCode = code;
     Message    = message;
 }
Пример #2
0
        protected override UDPBaseResponse <ResponseVote> ParseResponse(AniDBUDPReturnCode code, string receivedData)
        {
            string[] parts = receivedData.Split('|');
            if (parts.Length != 4)
            {
                throw new UnexpectedAniDBResponseException("Incorrect Number of Parts Returned", code, receivedData);
            }
            if (!int.TryParse(parts[1], out int value))
            {
                throw new UnexpectedAniDBResponseException("Value should be an int, but it's not", code, receivedData);
            }
            if (!int.TryParse(parts[2], out int type))
            {
                throw new UnexpectedAniDBResponseException("Vote type should be an int, but it's not", code, receivedData);
            }
            if (!int.TryParse(parts[3], out int id))
            {
                throw new UnexpectedAniDBResponseException("ID should be an int, but it's not", code, receivedData);
            }

            return(new UDPBaseResponse <ResponseVote> {
                Code = code, Response = new ResponseVote
                {
                    EntityName = parts[0],
                    Value = value,
                    Type = (VoteType)type,
                    EntityID = id
                }
            });
        }
Пример #3
0
        protected override UDPBaseResponse <ResponseMyListStats> ParseResponse(AniDBUDPReturnCode code, string receivedData)
        {
            var parsedData = receivedData.Split('|').Select(a => !long.TryParse(a.Trim(), out var result) ? 0 : result)
                             .ToArray();

            // 222 MYLIST STATS
            // 281|3539|4025|1509124|0|0|0|0|100|100|0|3|5|170|23|0|4001

            ResponseMyListStats stats = new ResponseMyListStats
            {
                Anime               = (int)parsedData[0],
                Episodes            = (int)parsedData[1],
                Files               = (int)parsedData[2],
                SizeOfFiles         = parsedData[3],
                AddedAnime          = (int)parsedData[4],
                AddedEpisodes       = (int)parsedData[5],
                AddedFiles          = (int)parsedData[6],
                AddedGroups         = (int)parsedData[7],
                LeechPercent        = (int)parsedData[8],
                GloryPercent        = (int)parsedData[9],
                ViewedPercent       = (int)parsedData[10],
                MyListPercent       = (int)parsedData[11],
                ViewedMyListPercent = (int)parsedData[12],
                EpisodesViewed      = (int)parsedData[13],
                Votes               = (int)parsedData[14],
                Reviews             = (int)parsedData[15],
                ViewedLength        = parsedData[16]
            };

            return(new UDPBaseResponse <ResponseMyListStats> {
                Code = code, Response = stats
            });
        }
Пример #4
0
 protected override UDPBaseResponse <Void> ParseResponse(AniDBUDPReturnCode code, string receivedData)
 {
     if (code != AniDBUDPReturnCode.PONG)
     {
         throw new UnexpectedAniDBResponseException(code, receivedData);
     }
     return(new UDPBaseResponse <Void> {
         Code = code
     });
 }
Пример #5
0
 protected override UDPBaseResponse <Void> ParseResponse(AniDBUDPReturnCode code, string receivedData)
 {
     switch (code)
     {
     case AniDBUDPReturnCode.MYLIST_ENTRY_EDITED:
     case AniDBUDPReturnCode.NO_SUCH_MYLIST_ENTRY:
         return(new UDPBaseResponse <Void> {
             Code = code
         });
     }
     throw new UnexpectedAniDBResponseException(code, receivedData);
 }
Пример #6
0
        protected override UDPBaseResponse <ResponseLogin> ParseResponse(AniDBUDPReturnCode code, string receivedData)
        {
            int i = receivedData.IndexOf("LOGIN", StringComparison.Ordinal);

            if (i < 0)
            {
                throw new UnexpectedAniDBResponseException(code, receivedData);
            }
            string sessionID = receivedData.Substring(0, i - 1).Trim();

            return(new UDPBaseResponse <ResponseLogin>
            {
                Response = new ResponseLogin {
                    SessionID = sessionID
                }, Code = code
            });
        }
Пример #7
0
        protected override UDPBaseResponse <ResponseCalendar> ParseResponse(AniDBUDPReturnCode code, string receivedData)
        {
            if (code == AniDBUDPReturnCode.CALENDAR_EMPTY)
            {
                return new UDPBaseResponse <ResponseCalendar> {
                           Response = null, Code = code
                }
            }
            ;

            var calendar = new ResponseCalendar
            {
                Next25Anime     = new List <ResponseCalendar.CalendarEntry>(),
                Previous25Anime = new List <ResponseCalendar.CalendarEntry>()
            };

            foreach (var parts in receivedData.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
                     .Select(line => line.Split('\n')))
            {
                if (parts.Length != 3)
                {
                    continue;
                }
                if (!int.TryParse(parts[0], out int animeID))
                {
                    continue;
                }
                if (!int.TryParse(parts[1], out int epochElapsed))
                {
                    continue;
                }
                if (!int.TryParse(parts[2], out int flagInt))
                {
                    continue;
                }
                var flags = (ResponseCalendar.CalendarFlags)flagInt;

                var date  = Commons.Utils.AniDB.GetAniDBDateAsDate(epochElapsed);
                var entry = new ResponseCalendar.CalendarEntry
                {
                    AnimeID     = animeID,
                    ReleaseDate = date,
                    DateFlags   = flags
                };
                bool known = !flags.HasFlag(ResponseCalendar.CalendarFlags.StartUnknown) &&
                             !flags.HasFlag(ResponseCalendar.CalendarFlags.StartDayUnknown) &&
                             !flags.HasFlag(ResponseCalendar.CalendarFlags.StartMonthDayUnknown);
                if (known && date.HasValue && date.Value < DateTime.UtcNow.Date)
                {
                    calendar.Previous25Anime.Add(entry);
                }
                else
                {
                    calendar.Next25Anime.Add(entry);
                }
            }

            return(new UDPBaseResponse <ResponseCalendar>
            {
                Response = calendar, Code = code
            });
        }
    }
Пример #8
0
 protected abstract UDPBaseResponse <T> ParseResponse(AniDBUDPReturnCode code, string receivedData);
 public UnexpectedAniDBResponseException(AniDBUDPReturnCode code, string response) : base(
         $"Unexpected AniDB Response: {code} | {response}")
 {
     Response   = response;
     ReturnCode = code;
 }
Пример #10
0
        protected override UDPBaseResponse <ResponseAddFile> ParseResponse(AniDBUDPReturnCode code, string receivedData)
        {
            switch (code)
            {
            case AniDBUDPReturnCode.MYLIST_ENTRY_ADDED:
            {
                /* Response Format
                 * {int4 mylist id of new entry}
                 */
                // parse the MyList ID
                string[] arrResult = receivedData.Split('\n');
                if (arrResult.Length >= 2)
                {
                    int.TryParse(arrResult[1], out int myListID);
                    return(new UDPBaseResponse <ResponseAddFile>
                        {
                            Code = code,
                            Response = new ResponseAddFile
                            {
                                MyListID = myListID,
                                State = State,
                                IsWatched = IsWatched,
                                WatchedDate = WatchedDate
                            }
                        });
                }
                break;
            }

            case AniDBUDPReturnCode.FILE_ALREADY_IN_MYLIST:
            {
                /* Response Format
                 * {int4 lid}|{int4 fid}|{int4 eid}|{int4 aid}|{int4 gid}|{int4 date}|{int2 state}|{int4 viewdate}|{str storage}|{str source}|{str other}|{int2 filestate}
                 */
                //file already exists: read 'watched' status
                string[] arrResult = receivedData.Split('\n');
                if (arrResult.Length >= 2)
                {
                    string[] arrStatus   = arrResult[1].Split('|');
                    bool     hasMyListID = int.TryParse(arrStatus[0], out int myListID);
                    if (!hasMyListID)
                    {
                        throw new UnexpectedAniDBResponseException
                              {
                                  Message    = "MyListID was not provided. Use AniDBMyList_RequestAddEpisode for generic files.",
                                  Response   = receivedData,
                                  ReturnCode = code
                              }
                    }
                    ;


                    AniDBFile_State state = (AniDBFile_State)int.Parse(arrStatus[6]);

                    int  viewdate = int.Parse(arrStatus[7]);
                    bool watched  = viewdate > 0;

                    DateTime?watchedDate = null;
                    if (watched)
                    {
                        DateTime utcDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                        utcDate = utcDate.AddSeconds(viewdate);

                        watchedDate = utcDate.ToLocalTime();
                    }

                    return(new UDPBaseResponse <ResponseAddFile>
                        {
                            Code = code,
                            Response = new ResponseAddFile
                            {
                                MyListID = myListID,
                                State = state,
                                IsWatched = watched,
                                WatchedDate = watchedDate
                            }
                        });
                }
                break;
            }
            }
            throw new UnexpectedAniDBResponseException(code, receivedData);
        }
    }
Пример #11
0
 protected override UDPBaseResponse <Void> ParseResponse(AniDBUDPReturnCode code, string receivedData)
 {
     return(new UDPBaseResponse <Void> {
         Code = code
     });
 }