Example #1
0
        // non official API to retrieve current watching state
        public static myanimelist GetMALAnimeList()
        {
            try
            {
                if (string.IsNullOrEmpty(ServerSettings.MAL_Username) ||
                    string.IsNullOrEmpty(ServerSettings.MAL_Password))
                {
                    logger.Warn("Won't search MAL, MAL credentials not provided");
                    return(null);
                }

                string url = string.Format("https://myanimelist.net/malappinfo.php?u={0}&status=all&type=anime",
                                           ServerSettings.MAL_Username);
                string malAnimeListXML = SendMALAuthenticatedRequest(url);
                malAnimeListXML = ReplaceEntityNamesByCharacter(malAnimeListXML);

                XmlSerializer serializer   = new XmlSerializer(typeof(myanimelist));
                XmlDocument   docAnimeList = new XmlDocument();
                docAnimeList.LoadXml(malAnimeListXML);

                XmlNodeReader reader       = new XmlNodeReader(docAnimeList.DocumentElement);
                object        obj          = serializer.Deserialize(reader);
                myanimelist   malAnimeList = (myanimelist)obj;
                return(malAnimeList);
            }
            catch (Exception ex)
            {
                logger.Error(ex, ex.ToString());
                return(null);
            }
        }
Example #2
0
        /*public static void UpdateWatchedStatus(int animeID, enEpisodeType epType, int lastWatchedEpNumber)
         *      {
         *              try
         *              {
         *                      if (string.IsNullOrEmpty(ServerSettings.MAL_Username) || string.IsNullOrEmpty(ServerSettings.MAL_Password))
         *                              return;
         *
         *                      AniDB_EpisodeRepository repAniEps = new AniDB_EpisodeRepository();
         *                      List<AniDB_Episode> aniEps = repAniEps.GetByAnimeIDAndEpisodeTypeNumber(animeID, epType, lastWatchedEpNumber);
         *                      if (aniEps.Count == 0) return;
         *
         *                      AnimeEpisodeRepository repEp = new AnimeEpisodeRepository();
         *                      AnimeEpisode ep = repEp.GetByAniDBEpisodeID(aniEps[0].EpisodeID);
         *                      if (ep == null) return;
         *
         *                      MALHelper.UpdateMAL(ep);
         *              }
         *              catch (Exception ex)
         *              {
         *                      logger.Error( ex,ex.ToString());
         *              }
         *      }*/

        public static void UpdateMALSeries(SVR_AnimeSeries ser)
        {
            try
            {
                if (string.IsNullOrEmpty(ServerSettings.MAL_Username) ||
                    string.IsNullOrEmpty(ServerSettings.MAL_Password))
                {
                    return;
                }

                // Populate MAL animelist hashtable if isNeverDecreaseWatched set
                Hashtable   animeListHashtable = new Hashtable();
                myanimelist malAnimeList       = GetMALAnimeList();
                if (ServerSettings.MAL_NeverDecreaseWatchedNums)
                //if set, check watched number before update: take some time, as user anime list must be loaded
                {
                    if (malAnimeList != null && malAnimeList.anime != null)
                    {
                        for (int i = 0; i < malAnimeList.anime.Length; i++)
                        {
                            animeListHashtable.Add(malAnimeList.anime[i].series_animedb_id, malAnimeList.anime[i]);
                        }
                    }
                }

                // look for MAL Links
                List <CrossRef_AniDB_MAL> crossRefs = ser.GetAnime().GetCrossRefMAL();
                if (crossRefs == null || crossRefs.Count == 0)
                {
                    logger.Warn("Could not find MAL link for : {0} ({1})", ser.GetAnime().GetFormattedTitle(),
                                ser.GetAnime().AnimeID);
                    return;
                }

                List <SVR_AnimeEpisode> eps = ser.GetAnimeEpisodes();

                // find the anidb user
                List <SVR_JMMUser> aniDBUsers = RepoFactory.JMMUser.GetAniDBUsers();
                if (aniDBUsers.Count == 0)
                {
                    return;
                }

                SVR_JMMUser user = aniDBUsers[0];

                int score = 0;
                if (ser.GetAnime().UserVote != null)
                {
                    score = (int)(ser.GetAnime().UserVote.VoteValue / 100);
                }

                // e.g.
                // AniDB - Code Geass R2
                // MAL Equivalent = AniDB Normal Eps 1 - 25 / Code Geass: Hangyaku no Lelouch R2 / hxxp://myanimelist.net/anime/2904/Code_Geass:_Hangyaku_no_Lelouch_R2
                // MAL Equivalent = AniDB Special Eps 1 - 9 / Code Geass: Hangyaku no Lelouch R2 Picture Drama / hxxp://myanimelist.net/anime/5163/Code_Geass:_Hangyaku_no_Lelouch_R2_Picture_Drama
                // MAL Equivalent = AniDB Special Eps 9 - 18 / Code Geass: Hangyaku no Lelouch R2: Flash Specials / hxxp://myanimelist.net/anime/9591/Code_Geass:_Hangyaku_no_Lelouch_R2:_Flash_Specials
                // MAL Equivalent = AniDB Special Eps 20 / Code Geass: Hangyaku no Lelouch - Kiseki no Birthday Picture Drama / hxxp://myanimelist.net/anime/8728/Code_Geass:_Hangyaku_no_Lelouch_-_Kiseki_no_Birthday_Picture_Drama

                foreach (CrossRef_AniDB_MAL xref in crossRefs)
                {
                    // look for the right MAL id
                    int malID        = -1;
                    int epNumber     = -1;
                    int totalEpCount = -1;

                    List <string> fanSubGroups = new List <string>();

                    // for each cross ref (which is a series on MAL) we need to update the data
                    // so find all the episodes which apply to this cross ref
                    int lastWatchedEpNumber = 0;
                    int downloadedEps       = 0;

                    foreach (SVR_AnimeEpisode ep in eps)
                    {
                        int epNum = ep.AniDB_Episode.EpisodeNumber;
                        if (xref.StartEpisodeType == (int)ep.EpisodeTypeEnum && epNum >= xref.StartEpisodeNumber &&
                            epNum <= GetUpperEpisodeLimit(crossRefs, xref))
                        {
                            malID    = xref.MALID;
                            epNumber = epNum - xref.StartEpisodeNumber + 1;

                            // find the total episode count
                            if (totalEpCount < 0)
                            {
                                if (ep.EpisodeTypeEnum == enEpisodeType.Episode)
                                {
                                    totalEpCount = ser.GetAnime().EpisodeCountNormal;
                                }
                                if (ep.EpisodeTypeEnum == enEpisodeType.Special)
                                {
                                    totalEpCount = ser.GetAnime().EpisodeCountSpecial;
                                }
                                totalEpCount = totalEpCount - xref.StartEpisodeNumber + 1;
                            }

                            // any episodes here belong to the MAL series
                            // find the latest watched episod enumber
                            SVR_AnimeEpisode_User usrRecord = ep.GetUserRecord(user.JMMUserID);
                            if (usrRecord != null && usrRecord.WatchedDate.HasValue && epNum > lastWatchedEpNumber)
                            {
                                lastWatchedEpNumber = epNum;
                            }

                            List <CL_VideoDetailed> contracts = ep.GetVideoDetailedContracts(user.JMMUserID);

                            // find the latest episode number in the collection
                            if (contracts.Count > 0)
                            {
                                downloadedEps++;
                            }

                            foreach (CL_VideoDetailed contract in contracts)
                            {
                                if (!string.IsNullOrEmpty(contract.AniDB_Anime_GroupNameShort) &&
                                    !fanSubGroups.Contains(contract.AniDB_Anime_GroupNameShort))
                                {
                                    fanSubGroups.Add(contract.AniDB_Anime_GroupNameShort);
                                }
                            }
                        }
                    }

                    string fanSubs = "";
                    foreach (string fgrp in fanSubGroups)
                    {
                        if (!string.IsNullOrEmpty(fanSubs))
                        {
                            fanSubs += ",";
                        }
                        fanSubs += fgrp;
                    }

                    // determine status
                    int status = 1; //watching
                    if (animeListHashtable.ContainsKey(malID))
                    {
                        myanimelistAnime animeInList = (myanimelistAnime)animeListHashtable[malID];
                        status = animeInList.my_status;
                    }

                    // over-ride is user has watched an episode
                    // don't override on hold (3) or dropped (4) but do override plan to watch (6)
                    if (status == 6 && lastWatchedEpNumber > 0)
                    {
                        status = 1;                                         //watching
                    }
                    if (lastWatchedEpNumber == totalEpCount)
                    {
                        status = 2;                                      //completed
                    }
                    if (lastWatchedEpNumber > totalEpCount)
                    {
                        logger.Error("updateMAL, episode number > matching anime episode total : {0} ({1}) / {2}",
                                     ser.GetAnime().GetFormattedTitle(), ser.GetAnime().AnimeID, epNumber);
                        continue;
                    }

                    if (malID <= 0 || totalEpCount <= 0)
                    {
                        logger.Warn("Could not find MAL link for : {0} ({1})", ser.GetAnime().GetFormattedTitle(),
                                    ser.GetAnime().AnimeID);
                        continue;
                    }

                    string confirmationMessage = "";
                    if (animeListHashtable.ContainsKey(malID))
                    {
                        ModifyAnime(malID, lastWatchedEpNumber, status, score, downloadedEps, fanSubs);
                        confirmationMessage = string.Format(
                            "MAL successfully updated (MAL MODIFY), mal id: {0}, ep: {1}, score: {2}", malID,
                            lastWatchedEpNumber, score);
                    }
                    else
                    {
                        AddAnime(malID, lastWatchedEpNumber, status, score, downloadedEps, fanSubs);
                        confirmationMessage = string.Format(
                            "MAL successfully updated (MAL ADD), mal id: {0}, ep: {1}, score: {2}", malID,
                            lastWatchedEpNumber, score);
                    }
                    logger.Trace(confirmationMessage);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, ex.ToString());
            }
        }
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_MALDownloadStatusFromMAL");

            try
            {
                if (string.IsNullOrEmpty(ServerSettings.MAL_Username) || string.IsNullOrEmpty(ServerSettings.MAL_Password))
                {
                    return;
                }

                // find the latest eps to update
                AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();

                myanimelist mal = MALHelper.GetMALAnimeList();
                if (mal == null)
                {
                    return;
                }
                if (mal.anime == null)
                {
                    return;
                }

                CrossRef_AniDB_MALRepository repCrossRef = new CrossRef_AniDB_MALRepository();
                AniDB_EpisodeRepository      repAniEps   = new AniDB_EpisodeRepository();
                AnimeEpisodeRepository       repEp       = new AnimeEpisodeRepository();

                // find the anidb user
                JMMUserRepository repUsers   = new JMMUserRepository();
                List <JMMUser>    aniDBUsers = repUsers.GetAniDBUsers();
                if (aniDBUsers.Count == 0)
                {
                    return;
                }

                JMMUser user = aniDBUsers[0];


                foreach (myanimelistAnime malAnime in mal.anime)
                {
                    // look up the anime
                    CrossRef_AniDB_MAL xref = repCrossRef.GetByMALID(malAnime.series_animedb_id);
                    if (xref == null)
                    {
                        continue;
                    }

                    if (malAnime.series_animedb_id == 8107 || malAnime.series_animedb_id == 10737)
                    {
                        Console.Write("");
                    }

                    // check if this anime has any other links
                    List <CrossRef_AniDB_MAL> allXrefs = repCrossRef.GetByAnimeID(xref.AnimeID);
                    if (allXrefs.Count == 0)
                    {
                        continue;
                    }

                    // find the range of watched episodes that this applies to
                    int startEpNumber = xref.StartEpisodeNumber;
                    int endEpNumber   = GetUpperEpisodeLimit(allXrefs, xref);

                    List <AniDB_Episode> aniEps = repAniEps.GetByAnimeID(xref.AnimeID);
                    foreach (AniDB_Episode aniep in aniEps)
                    {
                        if (aniep.EpisodeType != xref.StartEpisodeType)
                        {
                            continue;
                        }

                        AnimeEpisode ep = repEp.GetByAniDBEpisodeID(aniep.EpisodeID);
                        if (ep == null)
                        {
                            continue;
                        }

                        int adjustedWatchedEps = malAnime.my_watched_episodes + xref.StartEpisodeNumber - 1;
                        int epNum = aniep.EpisodeNumber;

                        if (epNum < startEpNumber || epNum > endEpNumber)
                        {
                            continue;
                        }

                        AnimeEpisode_User usrRec = ep.GetUserRecord(user.JMMUserID);

                        if (epNum <= adjustedWatchedEps)
                        {
                            // update if the user doesn't have a record (means not watched)
                            // or it is currently un-watched
                            bool update = false;
                            if (usrRec == null)
                            {
                                update = true;
                            }
                            else
                            {
                                if (!usrRec.WatchedDate.HasValue)
                                {
                                    update = true;
                                }
                            }

                            if (update)
                            {
                                ep.ToggleWatchedStatus(true, true, DateTime.Now, user.JMMUserID, false);
                            }
                        }
                        else
                        {
                            bool update = false;
                            if (usrRec != null)
                            {
                                if (usrRec.WatchedDate.HasValue)
                                {
                                    update = true;
                                }
                            }

                            if (update)
                            {
                                ep.ToggleWatchedStatus(false, true, DateTime.Now, user.JMMUserID, false);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error processing CommandRequest_MALDownloadStatusFromMAL: {0}", ex.ToString());
                return;
            }
        }