Permalink
Browse files

Handling to get ALL Updated Anime IDs, in the cases where there are a…

… lot
  • Loading branch information...
da3dsoul committed Oct 17, 2018
1 parent 5dd1253 commit 65087727b9455c18ef650c140fdf5d0536008b80
Showing with 48 additions and 15 deletions.
  1. +42 −6 Shoko.Server/AniDBHelper.cs
  2. +6 −9 Shoko.Server/AniDB_API/Commands/AniDBCommand_GetUpdated.cs
@@ -501,26 +501,62 @@ public void UpdateMyListStats()
}
}

public void GetUpdated(ref List<int> updatedAnimeIDs, ref long startTime)
/// <summary>
/// Gets the list of updated AnimeIDs. This may use more than one call to get them all, and therefore a lot of time
/// </summary>
/// <param name="updatedAnimeIDs">The updated IDs</param>
/// <param name="lastUpdateTime">The time that the last anime was updated</param>
public void GetUpdated(ref List<int> updatedAnimeIDs, ref long lastUpdateTime)
{
//startTime = 0;
updatedAnimeIDs = new List<int>();

if (!Login()) return;

lock (lockAniDBConnections)
{
AniDBCommand_GetUpdated cmdUpdated = new AniDBCommand_GetUpdated();
cmdUpdated.Init(startTime.ToString());
cmdUpdated.Init(lastUpdateTime.ToString());
SetWaitingOnResponse(true);
enHelperActivityType ev = cmdUpdated.Process(ref soUdp, ref remoteIpEndPoint, curSessionID,
new UnicodeEncoding(true, false));
SetWaitingOnResponse(false);

if (ev == enHelperActivityType.GotUpdated && cmdUpdated.RecordCount > 0)
if (ev != enHelperActivityType.GotUpdated) return;

int records = cmdUpdated.RecordCount;
if (records <= 0) return;

lastUpdateTime = long.Parse(cmdUpdated.LastUpdateTime);
updatedAnimeIDs.AddRange(cmdUpdated.AnimeIDList);

// we got them all
if (records <= 200) return;

// while loop it to be sure
while (records > 200)
{
startTime = long.Parse(cmdUpdated.StartTime);
updatedAnimeIDs = cmdUpdated.AnimeIDList;
// reinit with last update time provided
cmdUpdated = new AniDBCommand_GetUpdated();
cmdUpdated.Init(lastUpdateTime.ToString());
// get the rest (assuming RecordCount was <= 400
SetWaitingOnResponse(true);
ev = cmdUpdated.Process(ref soUdp, ref remoteIpEndPoint, curSessionID,
new UnicodeEncoding(true, false));
SetWaitingOnResponse(false);

if (ev != enHelperActivityType.GotUpdated) return;

// update records with new count
records = cmdUpdated.RecordCount;
if (records <= 0) return;

lastUpdateTime = long.Parse(cmdUpdated.LastUpdateTime);

// if the first/last item overlap, then remove it so it isn't duplicated
if (cmdUpdated.AnimeIDList.Count > 0 && cmdUpdated.AnimeIDList[0] == updatedAnimeIDs.LastOrDefault())
cmdUpdated.AnimeIDList.RemoveAt(0);

updatedAnimeIDs.AddRange(cmdUpdated.AnimeIDList);
}
}
}
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
@@ -10,7 +11,7 @@ public class AniDBCommand_GetUpdated : AniDBUDPCommand, IAniDBUDPCommand
{
private static Logger logger = LogManager.GetCurrentClassLogger();

public string StartTime { get; set; }
public string LastUpdateTime { get; set; }
public int AniDBEntity { get; set; }
public int RecordCount { get; set; }
public List<int> AnimeIDList { get; set; }
@@ -67,13 +68,9 @@ public virtual enHelperActivityType GetStartEventType()
string[] flds = sDetails[1].Substring(0).Split('|');
AniDBEntity = int.Parse(flds[0]);
RecordCount = int.Parse(flds[1]);
StartTime = flds[2];
LastUpdateTime = flds[2];
AnimeIDListRaw = flds[3].Trim();
string[] aids = AnimeIDListRaw.Split(',');
foreach (string sid in aids)
{
AnimeIDList.Add(int.Parse(sid));
}
AnimeIDList = AnimeIDListRaw.Split(',').Select(int.Parse).ToList();
}

return enHelperActivityType.GotUpdated;
@@ -94,9 +91,9 @@ public AniDBCommand_GetUpdated()
public void Init(string startTime)
{
RecordCount = 0;
this.StartTime = startTime;
this.LastUpdateTime = startTime;

commandText = string.Format("UPDATED entity=1&time={0}", this.StartTime);
commandText = string.Format("UPDATED entity=1&time={0}", this.LastUpdateTime);
//commandText = "UPDATED entity=1&age=1";

commandID = "UPDATED ";

0 comments on commit 6508772

Please sign in to comment.