Skip to content

Commit 5434549

Browse files
committed
spidr
1 parent e844e05 commit 5434549

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

AppInit.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public static AppInit conf
7474

7575
public bool syncsport = true;
7676

77+
public bool syncspidr = false;
78+
7779
public int maxreadfile = 200;
7880

7981
public Evercache evercache = new Evercache() { enable = true, validHour = 1, maxOpenWriteTask = 2000 };

Controllers/SyncController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public JsonResult Configuration()
2020
return Json(new
2121
{
2222
fbd = true,
23+
spidr = true,
2324
version = 2
2425
});
2526
}
@@ -41,7 +42,7 @@ public ActionResult FdbKey(string key)
4142
}
4243

4344
[Route("/sync/fdb/torrents")]
44-
public ActionResult FdbTorrents(long time, long start = -1)
45+
public ActionResult FdbTorrents(long time, long start = -1, bool spidr = false)
4546
{
4647
if (!AppInit.conf.opensync || time == 0)
4748
return Json(new { nextread = false, collections = new List<Collection>() });
@@ -62,7 +63,7 @@ public ActionResult FdbTorrents(long time, long start = -1)
6263

6364
foreach (var t in FileDB.OpenRead(item.Key))
6465
{
65-
if (start != -1 && start > t.Value.updateTime.ToFileTimeUtc())
66+
if (spidr || (start != -1 && start > t.Value.updateTime.ToFileTimeUtc()))
6667
{
6768
torrent.TryAdd(t.Key, new TorrentDetails()
6869
{
@@ -115,7 +116,7 @@ public ActionResult FdbTorrents(long time, long start = -1)
115116
}
116117
}
117118

118-
return Json(new { nextread, collections });
119+
return Json(new { nextread, countread, take, collections });
119120
}
120121

121122

Engine/SyncCron.cs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public static class SyncCron
1414
{
1515
static long lastsync = -1, starsync = -1;
1616

17-
async public static Task Run()
17+
#region Torrents
18+
async public static Task Torrents()
1819
{
1920
await Task.Delay(20_000);
2021

@@ -55,19 +56,16 @@ async public static Task Run()
5556
else if (root.collections.Count > 0)
5657
{
5758
reset = true;
58-
var torrents = new List<TorrentBaseDetails>();
59+
var torrents = new List<TorrentBaseDetails>(root.countread);
5960

6061
foreach (var collection in root.collections)
6162
{
6263
foreach (var torrent in collection.Value.torrents)
6364
{
64-
if (torrent.Value.types == null)
65+
if (AppInit.conf.synctrackers != null && torrent.Value.trackerName != null && !AppInit.conf.synctrackers.Contains(torrent.Value.trackerName))
6566
continue;
6667

67-
if (AppInit.conf.synctrackers != null && !AppInit.conf.synctrackers.Contains(torrent.Value.trackerName))
68-
continue;
69-
70-
if (!AppInit.conf.syncsport && torrent.Value.types.Contains("sport"))
68+
if (!AppInit.conf.syncsport && torrent.Value.types != null && torrent.Value.types.Contains("sport"))
7169
continue;
7270

7371
torrents.Add(torrent.Value);
@@ -118,14 +116,16 @@ async public static Task Run()
118116

119117
FileDB.SaveChangesToFile();
120118
File.WriteAllText("lastsync.txt", lastsync.ToString());
119+
120+
Console.WriteLine("sync: end");
121121
}
122122
else
123123
{
124124
await Task.Delay(TimeSpan.FromMinutes(1));
125125
continue;
126126
}
127127
}
128-
catch
128+
catch (Exception ex)
129129
{
130130
try
131131
{
@@ -136,12 +136,62 @@ async public static Task Run()
136136
}
137137
}
138138
catch { }
139+
140+
Console.WriteLine("sync: error / " + ex.Message);
139141
}
140142

141-
Console.WriteLine("sync: end");
142143
await Task.Delay(1000 * Random.Shared.Next(60, 300));
143144
await Task.Delay(1000 * 60 * (20 > AppInit.conf.timeSync ? 20 : AppInit.conf.timeSync));
144145
}
145146
}
147+
#endregion
148+
149+
#region Spidr
150+
async public static Task Spidr()
151+
{
152+
while (true)
153+
{
154+
await Task.Delay(1000 * Random.Shared.Next(60, 300));
155+
await Task.Delay(1000 * 60 * 120);
156+
157+
try
158+
{
159+
if (!string.IsNullOrWhiteSpace(AppInit.conf.syncapi) && AppInit.conf.syncspidr)
160+
{
161+
long lastsync_spidr = -1;
162+
163+
var conf = await HttpClient.Get<JObject>($"{AppInit.conf.syncapi}/sync/conf");
164+
if (conf != null && conf.ContainsKey("spidr") && conf.Value<bool>("spidr"))
165+
{
166+
Console.WriteLine($"\n\nsync_spidr: start / {DateTime.Now}");
167+
168+
next: var root = await HttpClient.Get<Models.Sync.v2.RootObject>($"{AppInit.conf.syncapi}/sync/fdb/torrents?time={lastsync_spidr}&spidr=true", timeoutSeconds: 300, MaxResponseContentBufferSize: 100_000_000);
169+
170+
Console.WriteLine($"sync_spidr: time={lastsync_spidr}");
171+
172+
if (root?.collections != null && root.collections.Count > 0)
173+
{
174+
foreach (var collection in root.collections)
175+
FileDB.AddOrUpdate(collection.Value.torrents.Values);
176+
177+
lastsync_spidr = root.collections.Last().Value.fileTime;
178+
179+
if (root.nextread)
180+
goto next;
181+
}
182+
183+
Console.WriteLine("sync_spidr: end");
184+
}
185+
}
186+
else
187+
{
188+
await Task.Delay(TimeSpan.FromMinutes(1));
189+
continue;
190+
}
191+
}
192+
catch (Exception ex) { Console.WriteLine("sync_spidr: error / " + ex.Message); }
193+
}
194+
}
195+
#endregion
146196
}
147197
}

Models/Sync/v2/RootObject.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ public class RootObject
66
{
77
public bool nextread { get; set; }
88

9+
public int take { get; set; }
10+
11+
public int countread { get; set; }
12+
913
public List<Collection> collections { get; set; }
1014
}
1115
}

Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public class Program
1212
{
1313
public static void Main(string[] args)
1414
{
15-
ThreadPool.QueueUserWorkItem(async _ => await SyncCron.Run());
15+
ThreadPool.QueueUserWorkItem(async _ => await SyncCron.Torrents());
16+
ThreadPool.QueueUserWorkItem(async _ => await SyncCron.Spidr());
17+
1618
ThreadPool.QueueUserWorkItem(async _ => await StatsCron.Run());
1719

1820
ThreadPool.QueueUserWorkItem(async _ => await FileDB.Cron());

0 commit comments

Comments
 (0)