Skip to content

Commit aaac012

Browse files
committed
Faster and more reliable fetching of asset IDs with atom API
1 parent 1716c8a commit aaac012

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

library/Sporecast.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading;
56
using System.Xml.Linq;
67

@@ -16,40 +17,36 @@ public Sporecast(long id)
1617
}
1718

1819
/// <summary>
19-
/// Gets a collection of IDs for all of this sporecast's assets. Maximum speed of 500 creations per second, to reduce impact on the server.
20+
/// Gets a collection of IDs for all of this sporecast's assets.
2021
/// </summary>
2122
public Queue<long> getAllAssetIds()
2223
{
2324
var server = new SporeServer();
2425

2526
var assetIds = new Queue<long>();
2627

27-
for (int i = 0; ; i += 500)
28-
{
29-
var doc = server.getAssetsForSporecast(Id, i, 500).Element("assets");
3028

31-
if(doc is null)
32-
{
33-
break;
34-
}
29+
var doc = server.getSporecastFeed(Id).Element("{http://www.w3.org/2005/Atom}feed");//.Elements().FirstOrDefault()?.Elements().FirstOrDefault();
30+
Console.WriteLine(doc?.ToString());
3531

36-
foreach (var asset in doc.Elements("asset"))
32+
if (doc is not null)
33+
{
34+
foreach (var asset in doc.Elements("{http://www.w3.org/2005/Atom}entry"))
3735
{
38-
long assetId = long.Parse(asset.Element("id")!.Value);
36+
string entryId = asset.Element("{http://www.w3.org/2005/Atom}id")!.Value;
37+
long assetId = long.Parse(entryId.Split('/')[1]);
3938

4039
assetIds.Enqueue(assetId);
4140

4241
Console.WriteLine($"Found asset ID {assetId} for sporecast {Id}");
4342
}
44-
45-
// Check if the number of retrieved creations is less than 500, if it is, exit loop
46-
int retrievedCount = int.Parse(doc.Element("count")!.Value);
47-
if (retrievedCount < 500) break;
48-
// Pause for a second, so we don't upset the server
49-
else Thread.Sleep(1000);
43+
Console.WriteLine($"Found {assetIds.Count} assets for sporecast {Id}");
44+
}
45+
else
46+
{
47+
Console.WriteLine($"Found no assets for sporecast {Id}, feed did not exist");
5048
}
5149

52-
Console.WriteLine($"Found {assetIds.Count} assets for sporecast {Id}");
5350

5451
return assetIds;
5552
}

0 commit comments

Comments
 (0)