public static void writeProperties(string _path) { //this method assumes that in you have scraped a number of Scratch files //it will then put all the corresponding properties in /properties DirectoryInfo d = new DirectoryInfo(_path); FileInfo[] Files = d.GetFiles(); //Getting files int i = 0; foreach (FileInfo file in Files) { //get the id: string id = Path.GetFileNameWithoutExtension(file.Name); string projectURL = @"https://scratch.mit.edu/projects/" + id + "/?x=" + DateTime.Now.ToString(); //we are adding a fake quety string to prevent the browser form loading from the cache and getting old data var HTML = JSONGetter.GetJSON(projectURL); if (HTML != null) { if (isShared(HTML)) { string pathForProperties = _path + "properties\\properties.sb"; JSONGetter.writeStringToFile(id + ",", pathForProperties, true, false); FindCountandWritetoFile(HTML, "fav-count", pathForProperties); FindCountandWritetoFile(HTML, "love-count", pathForProperties); FindCountandWritetoFile(HTML, "icon views", pathForProperties); FindCountandWritetoFile(HTML, "icon remix-tree", pathForProperties); FindCountandWritetoFile(HTML, "Shared:", pathForProperties); FindCountandWritetoFile(HTML, "Modified:", pathForProperties); FindUserWritetoFile(HTML, pathForProperties); } else { string pathForProperties = _path + "properties\\notShared.sb"; JSONGetter.writeStringToFile(id, pathForProperties, true, true); } } Console.WriteLine(i.ToString()); i++; } }
private static void FindUserWritetoFile(string HTML, string pathForProperties) { var toFind = "id=\"owner"; var found = HTML.IndexOf(toFind); if (found != -1) { var endofSpan = HTML.IndexOf("</span>", found); var item = HTML.Substring(found + toFind.Length + 2, endofSpan - found - toFind.Length - 2); var itemNoSpaces = item.Replace(" ", "").Replace(" ", "").Replace("\n", ""); JSONGetter.writeStringToFile(itemNoSpaces, pathForProperties, true, true); } }
private static void FindCountandWritetoFile(string HTML, string toFind, string pathForProperties) { var found = HTML.IndexOf(toFind); if (found != -1) { var endofSpan = HTML.IndexOf("</span>", found); var item = HTML.Substring(found + toFind.Length + 2, endofSpan - found - toFind.Length - 2); var itemNoSpacesandComma = item.Replace(" ", "").Replace(" ", "").Replace("\n", "") + ","; if (itemNoSpacesandComma == ",") { itemNoSpacesandComma = "0,"; } JSONGetter.writeStringToFile(itemNoSpacesandComma, pathForProperties, true, false); } }
static ArrayList flatten(ref int order, JsonArray scripts, ref string scopeType, ref string scopeName, ref int indent, string path, string id, ref int maxIndent) { var result = new ArrayList(); if (scopeName[0] != '"') { //not in quotes? add them scopeName = "\"" + scopeName + "\""; } //by default we add the order, type of the scope (scene, sprite, or proc) the name of the scope and the indent string toPrint = scopeType + "," + scopeName + "," + indent.ToString(); bool added = false; bool addOrder = true; foreach (var innerScript in scripts) { //if the script is primitive, we just print it. if (innerScript is JsonPrimitive) { if (addOrder) { toPrint += "," + order + "," + innerScript; order = order + 1; addOrder = false; } else { toPrint += "," + innerScript; } added = true; //it could be that there will be more primitives (arguments) so we only print at the end } if (innerScript is JsonArray) { if (AllOneField((JsonArray)innerScript)) { if (innerScript.Count == 0) { //this is an empy array if (addOrder) { toPrint += "," + order + ",[]"; order = order + 1; addOrder = false; } else { toPrint += ",[]"; } } else { int j = indent + 1; if (j > maxIndent) { maxIndent = j; } foreach (var item in flatten(ref order, (JsonArray)(innerScript), ref scopeType, ref scopeName, ref j, id, path, ref maxIndent)) { result.Add(item); } } } else { if (innerScript.Count > 0 && innerScript[0].ToString() == "\"procDef\"") { //first save this definition to a separate file string procdef = id + "," + scopeName + ",procDef," + innerScript[1].ToString() + "," + innerScript[2].Count.ToString(); //procdef plus name of the proc plus number of arguments JSONGetter.writeStringToFile(procdef, path + "output\\procedures.csv", true); toPrint += ",procdef"; //now set the other blocks to the scope of this proc scopeType = "procDef"; scopeName = innerScript[1].ToString(); added = true; } else { int j = indent + 1; if (j > maxIndent) { maxIndent = j; } foreach (var item in flatten(ref order, (JsonArray)(innerScript), ref scopeType, ref scopeName, ref j, id, path, ref maxIndent)) { result.Add(item); } } } } } if (added) { result.Add(toPrint); } return(result); }