public static bool PatchPbw(PebbleApp app, string timelineDomain, string saveLocation) { try { PebbleApp_Release release = app.latest_release.GetRelease(); //Download the PBW file from the server. using (FileStream pbw = new FileStream(saveLocation, FileMode.Create)) { //Download and copy to this stream. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(release.pbw_file); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) { stream.CopyTo(pbw); } //Rewind to the beginning. pbw.Position = 0; //Open the ZIP file. using (var zip = new ZipArchive(pbw, ZipArchiveMode.Update, true)) { //Check if the "pebble-js-app.js" file exists. var js = zip.GetEntry("pebble-js-app.js"); //Work on it if it exists. if (js != null) { //Open. using (var jsFile = js.Open()) { //Read in the javascript. byte[] buf = new byte[jsFile.Length]; jsFile.Read(buf, 0, buf.Length); string jsData = Encoding.UTF8.GetString(buf); //Do find and replace. jsData = jsData.Replace("timeline-api.getpebble.com", timelineDomain); //Write this back. buf = Encoding.UTF8.GetBytes(jsData); jsFile.Position = 0; jsFile.SetLength(buf.Length); jsFile.Write(buf, 0, buf.Length); //Close and save the file. } } } } return(true); } catch (Exception ex) { Console.WriteLine("FATAL ERROR WHILE PATCHING " + app.id + " (" + app.title + ") - " + ex.Message + " @ " + ex.StackTrace + "\r\n\r\n"); File.Delete(saveLocation); return(false); } }
static void Main(string[] args) { Console.WriteLine("Downloading app data..."); //Download all apps. string next = SOURCE_URL; JsonSerializerSettings sett = new JsonSerializerSettings(); List <PebbleApp> apps = new List <PebbleApp>(); List <string> validIds = new List <string>(); while (next != null) { var page = RunAppHttpApi(sett, next); next = page.next; //add apps apps.AddRange(page.data); } //Now, patch all. Console.WriteLine("Patching PBWs..."); int done = 0; int failed = 0; Parallel.For(0, apps.Count, (int i) => { PebbleApp app = apps[i]; bool ok = PatchPbw(app, PATCHED_ENDPOINT, OUTPUT_PATH.Replace("{id}", app.id)); if (ok) { validIds.Add(app.id); } else { failed++; } done++; Console.Title = "RPWS PBW Patcher - " + done.ToString() + " done - " + failed.ToString() + " failed - " + ((int)(((float)done / (float)apps.Count) * 100)).ToString() + "% finished"; }); Console.WriteLine("Complete. Saving database."); string db = JsonConvert.SerializeObject(validIds.ToArray()); File.WriteAllText(OUTPUT_DB, db); Console.WriteLine("All finished."); Console.ReadLine(); }