static string RecipePage(HttpListenerRequest request, HttpListenerResponse result, SessionInfo session) { PreferenceInfo preferences = preferences_[session.User.Username]; if (null == preferences) { preferences = new PreferenceInfo(); } Dictionary <string, int> preferenceLookup = new Dictionary <string, int>(); foreach (var p in preferences.IngredientPreferences) { preferenceLookup[p.Ingredient] = p.Score; } var getArgs = HttpUtility.ParseQueryString(request.Url.Query); double strictness = Math.Max(1e-4, double.Parse(getArgs["strictness"])); var rankedRecipes = new List <PreferenceScore>(); foreach (var recipe in recipes_) { rankedRecipes.Add(GetPreferenceScore(recipe, preferenceLookup, strictness)); } rankedRecipes.Sort(); StringBuilder buffer = new StringBuilder(); buffer.Append( @" <html> <title>") .Append(session.User.Username) .Append( @"'s personalized recipes</title> <body style='font-family:tahoma,arial,sans-serif;'> <fieldset> "); AddLegend(session.User.Username, buffer); foreach (var recipe in rankedRecipes) { var owp = recipe.Recipe.Recipe.OriginalWebPage; buffer.AppendFormat("<br><a href='{2}' target='_blank' title='score: {0:0%}'>{1}</a><br>\n", recipe.Score, RecipeDownloader.RemoveSpecialCharacters(owp.Name), owp.UrlRoot + owp.Url); if (recipe.Positives != null) { buffer.Append("<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor='lightcyan'>"); foreach (string entry in recipe.Positives) { buffer.Append("<li>").Append(entry).Append("</li>\n"); } buffer.Append("</table>"); } if (recipe.Neutrals != null) { buffer.Append("<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor='ivory'>"); foreach (string entry in recipe.Neutrals) { buffer.Append("<li>").Append(entry).Append("</li>\n"); } buffer.Append("</table>"); } if (recipe.Negatives != null) { buffer.Append("<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor='peachpuff'>"); foreach (string entry in recipe.Negatives) { buffer.Append("<li>").Append(entry).Append("</li>\n"); } buffer.Append("</table>"); } } buffer.AppendLine( @" </fieldset> </body> </html> "); return(buffer.ToString()); }
static string RecipeJsonPage(HttpListenerRequest request, HttpListenerResponse result, SessionInfo session) { var args = GetPostArgs(request); try { var preferences = args["preferences"]; if (null == preferences) { throw new ArgumentException("request does not contain post argument 'preferences'"); } var strictnessStr = args["strictness"]; if (null == strictnessStr) { throw new ArgumentException("request does not contain post argument 'strictness'"); } var limitStr = args["limit"]; if (null == limitStr) { throw new ArgumentException("request does not contain post argument 'limit'"); } int limit; if (!int.TryParse(limitStr, out limit) || !(0 < limit)) { throw new ArgumentException("limit " + limitStr + " is not a positive integer"); } double strictness; if (!double.TryParse(strictnessStr, out strictness)) { throw new ArgumentException("strictness " + strictnessStr + " is not a floating-point number"); } strictness = Math.Max(1e-4, strictness); var prefs = JsonConvert.DeserializeXNode(preferences); Dictionary <string, int> preferenceLookup = new Dictionary <string, int>(); foreach (var element in prefs.Root.Elements()) { if (element.HasAttributes) { throw new ArgumentException("preferences:" + element.Name + " has some kind of attributes inside"); } if (element.HasElements) { throw new ArgumentException("preferences:" + element.Name + " has some kind of elements inside"); } if (element.IsEmpty) { throw new ArgumentException("preferences:" + element.Name + " is empty"); } int score; if (!int.TryParse(element.Value, out score)) { throw new ArgumentException("preferences:" + element.Name + " has a score of " + element.Value + ", which is not an integer"); } preferenceLookup[element.Name.ToString()] = score; } var rankedRecipes = new List <PreferenceScore>(); foreach (var recipe in recipes_) { rankedRecipes.Add(GetPreferenceScore(recipe, preferenceLookup, strictness)); } rankedRecipes.Sort(); StringBuilder buffer = new StringBuilder(); int nResults = 0; foreach (var recipe in rankedRecipes) { ++nResults; if (nResults > limit) { break; } buffer.AppendLine("{"); buffer.AppendLine(" \"title\" : \"" + RecipeDownloader.RemoveSpecialCharacters(recipe.Recipe.Recipe.OriginalWebPage.Name) + "\","); buffer.AppendLine(" \"url\" : \"" + recipe.Recipe.Recipe.OriginalWebPage.UrlRoot + recipe.Recipe.Recipe.OriginalWebPage.Url + "\","); buffer.AppendLine(" \"positives\" : ["); if (recipe.Positives != null) { foreach (string entry in recipe.Positives) { buffer.Append(" \"").Append(entry).Append("\",").AppendLine(); } } buffer.AppendLine(" ],"); buffer.AppendLine(" \"neutrals\" : ["); if (recipe.Neutrals != null) { foreach (string entry in recipe.Neutrals) { buffer.Append(" \"").Append(entry).Append("\",").AppendLine(); } } buffer.AppendLine(" ],"); buffer.AppendLine(" \"negatives\" : ["); if (recipe.Negatives != null) { foreach (string entry in recipe.Negatives) { buffer.AppendLine(" \"").Append(entry).Append("\",").AppendLine(); } } buffer.AppendLine(" ],"); buffer.AppendLine("},"); } return("{ \"recipes\" : [\n" + buffer.ToString() + "]\n }\n"); } catch (Exception e) { StringBuilder preferenceBlob = new StringBuilder(); if (session.User != null) { PreferenceInfo preferences = preferences_[session.User.Username]; if (null != preferences) { preferenceBlob.AppendLine("{ \"preferences\" : {"); foreach (var p in preferences.IngredientPreferences) { preferenceBlob.AppendLine(" \"" + p.Ingredient + "\" : \"" + p.Score.ToString() + "\","); } preferenceBlob.AppendLine("} }"); } } return ("<html><body>" + e.ToString() + "<br>\n" + "<form method=post>\n" + "strictness:<input type='text' name='strictness' value='1'><br>\n" + "limit:<input type='text' name='limit' value='15'><br>\n" + "preferences:<br><textarea name=preferences rows=20 cols=80>" + preferenceBlob.ToString() + "</textarea><br>\n" + "<input type=submit>\n" + "</form>\n" + "</body></html>"); } }