Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class XapDownloader
- {
- private const string ZuneAppStartup = "http://catalog.zune.net/v3.2/en-US/apps/?clientType=WinMobile%207.0&store=Zest&orderby=downloadRank";
- private const string ZuneAppDetails = "http://catalog.zune.net/v3.2/en-US/apps/";
- private string currentWorkingFolder;
- public void Download()
- {
- string runName = DateTime.Now.ToFileTime().ToString();
- currentWorkingFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase), @"Downloads\", runName);
- Directory.CreateDirectory(currentWorkingFolder.Replace(@"file:\", string.Empty));
- DoWebRequest(ZuneAppStartup, GetListResponse);
- }
- private string nextRequestUrl;
- private List<string> IDsToRetrieve = new List<string>();
- private void GetListResponse(XDocument doc)
- {
- nextRequestUrl =
- "http://catalog.zune.net" +
- doc.Root
- .DescendantNodes()
- .OfType<XElement>()
- .First(n => n.Name.LocalName == "link" && n.Attribute("rel").Value == "next")
- .Attribute("href")
- .Value;
- IDsToRetrieve.AddRange(
- doc.Root
- .DescendantNodes()
- .OfType<XElement>()
- .Where(n => n.Name.LocalName == "entry")
- .Select(entry =>
- entry.Descendants()
- .First(n => n.Name.LocalName == "id")
- .Value
- )
- );
- while (IDsToRetrieve.Count != 0)
- {
- var id = IDsToRetrieve.First();
- DoWebRequest(ZuneAppDetails + id.Replace("urn:uuid:", string.Empty), GetItemResponse);
- IDsToRetrieve.Remove(id);
- }
- if (!string.IsNullOrEmpty(nextRequestUrl))
- {
- DoWebRequest(nextRequestUrl, GetListResponse);
- }
- }
- private int locationInRanking = 0;
- private void GetItemResponse(XDocument doc)
- {
- var name =
- doc.Root
- .DescendantNodes()
- .OfType<XElement>()
- .First(n => n.Name.LocalName == "title")
- .Value;
- var xapLocation = doc.Root
- .DescendantNodes()
- .OfType<XElement>()
- .Where(n => n.Name.LocalName == "entry")
- .Select(entry =>
- entry.DescendantNodes()
- .OfType<XElement>()
- .First(n => n.Name.LocalName == "url")
- .Value
- )
- .First();
- var path = Path.Combine(currentWorkingFolder, locationInRanking.ToString("D4") + " - " + name + ".xap").Replace(@"file:\", string.Empty);
- try
- {
- new WebClient().DownloadFile(xapLocation, path);
- Console.WriteLine("Got " + path);
- }
- catch
- {
- Console.WriteLine("Failed to get " + path);
- }
- locationInRanking++;
- }
- private void DoWebRequest(string Url, Action<XDocument> Response)
- {
- var request = HttpWebRequest.Create(Url);
- var response = request.GetResponse();
- Response(XDocument.Load(response.GetResponseStream()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement