Advertisement
Guest User

V@l€n

a guest
Dec 27th, 2010
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1.     public class XapDownloader
  2.     {
  3.         private const string ZuneAppStartup = "http://catalog.zune.net/v3.2/en-US/apps/?clientType=WinMobile%207.0&store=Zest&orderby=downloadRank";
  4.         private const string ZuneAppDetails = "http://catalog.zune.net/v3.2/en-US/apps/";
  5.         private string currentWorkingFolder;
  6.  
  7.         public void Download()
  8.         {
  9.             string runName = DateTime.Now.ToFileTime().ToString();
  10.             currentWorkingFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase), @"Downloads\", runName);
  11.             Directory.CreateDirectory(currentWorkingFolder.Replace(@"file:\", string.Empty));
  12.  
  13.             DoWebRequest(ZuneAppStartup, GetListResponse);
  14.  
  15.         }
  16.  
  17.         private string nextRequestUrl;
  18.         private List<string> IDsToRetrieve = new List<string>();
  19.  
  20.         private void GetListResponse(XDocument doc)
  21.         {
  22.             nextRequestUrl =
  23.                 "http://catalog.zune.net" +
  24.                 doc.Root
  25.                 .DescendantNodes()
  26.                 .OfType<XElement>()
  27.                 .First(n => n.Name.LocalName == "link" && n.Attribute("rel").Value == "next")
  28.                 .Attribute("href")
  29.                 .Value;
  30.  
  31.             IDsToRetrieve.AddRange(
  32.                 doc.Root
  33.                 .DescendantNodes()
  34.                 .OfType<XElement>()
  35.                 .Where(n => n.Name.LocalName == "entry")
  36.                 .Select(entry =>
  37.                     entry.Descendants()
  38.                     .First(n => n.Name.LocalName == "id")
  39.                     .Value
  40.                 )
  41.                 );
  42.  
  43.             while (IDsToRetrieve.Count != 0)
  44.             {
  45.                 var id = IDsToRetrieve.First();
  46.  
  47.                 DoWebRequest(ZuneAppDetails + id.Replace("urn:uuid:", string.Empty), GetItemResponse);
  48.  
  49.                 IDsToRetrieve.Remove(id);
  50.             }
  51.  
  52.             if (!string.IsNullOrEmpty(nextRequestUrl))
  53.             {
  54.                 DoWebRequest(nextRequestUrl, GetListResponse);
  55.             }
  56.         }
  57.  
  58.         private int locationInRanking = 0;
  59.  
  60.         private void GetItemResponse(XDocument doc)
  61.         {
  62.             var name =
  63.                 doc.Root
  64.                     .DescendantNodes()
  65.                     .OfType<XElement>()
  66.                     .First(n => n.Name.LocalName == "title")
  67.                     .Value;
  68.  
  69.             var xapLocation = doc.Root
  70.                 .DescendantNodes()
  71.                 .OfType<XElement>()
  72.                 .Where(n => n.Name.LocalName == "entry")
  73.                 .Select(entry =>
  74.                     entry.DescendantNodes()
  75.                     .OfType<XElement>()
  76.                     .First(n => n.Name.LocalName == "url")
  77.                     .Value
  78.                     )
  79.                 .First();
  80.  
  81.             var path = Path.Combine(currentWorkingFolder, locationInRanking.ToString("D4") + " - " + name + ".xap").Replace(@"file:\", string.Empty);
  82.             try
  83.             {
  84.                 new WebClient().DownloadFile(xapLocation, path);
  85.                 Console.WriteLine("Got " + path);
  86.             }
  87.             catch
  88.             {
  89.                 Console.WriteLine("Failed to get " + path);
  90.             }
  91.             locationInRanking++;
  92.         }
  93.  
  94.  
  95.         private void DoWebRequest(string Url, Action<XDocument> Response)
  96.         {
  97.             var request = HttpWebRequest.Create(Url);
  98.             var response = request.GetResponse();
  99.             Response(XDocument.Load(response.GetResponseStream()));
  100.         }
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement