public static void SaveUserAnswers(string site, int userid) { string datadir = "..\\..\\..\\..\\data\\" + site + "\\"; string postsdir = Path.Combine(datadir, "posts\\"); string path; if (!Directory.Exists(postsdir)) { Directory.CreateDirectory(postsdir); } SeApiClient client = new SeApiClient(APIURL, site); Dictionary <int, object> answers = client.LoadUserAnswers(userid); Console.WriteLine("Saving {0} answers...", answers.Count); foreach (int key in answers.Keys) { path = Path.Combine(postsdir, "A" + key.ToString() + ".md"); TextWriter wr = new StreamWriter(path, false, Encoding.UTF8); using (wr) { AnswerMarkdown post = AnswerMarkdown.FromJsonData(site, answers[key]); post.ToMarkdown(wr); } } }
public static void SelectUserAnswers(string site, string subdir, string target, int userid) { string datadir = "..\\..\\..\\..\\data\\" + site + "\\"; string postsdir = Path.Combine(datadir, subdir + "\\"); string targetdir = Path.Combine(datadir, target + "\\"); string path; if (!Directory.Exists(targetdir)) { Directory.CreateDirectory(targetdir); } Console.WriteLine("Copying answers of user {0} to {1}...", userid, targetdir); PostSet posts = PostSet.LoadFromDir(postsdir, site); Console.WriteLine("Answers: {0}", posts.AllMarkdownAnswers.Count); int c = 0; foreach (int a in posts.AllMarkdownAnswers.Keys) { AnswerMarkdown answer = posts.AllMarkdownAnswers[a]; if (answer.UserId.Trim() != userid.ToString().Trim()) { continue; } try { QuestionMarkdown question = answer.Parent; if (question != null) { if (String.IsNullOrEmpty(question.Title)) { question.Title = "Question " + answer.QuestionId.ToString(); } answer.Title = "Ответ на \"" + question.Title.ToString() + "\""; } path = Path.Combine(targetdir, "A" + a.ToString() + ".md"); using (TextWriter wr = new StreamWriter(path, false, Encoding.UTF8)) { answer.ToMarkdown(wr); } c++; } catch (Exception ex) { Console.WriteLine("Error on anser " + a.ToString()); Console.WriteLine(ex.GetType() + ": " + ex.Message); } } Console.WriteLine("Copied: {0}", c); }
public static void SaveSingleAnswer(string site, int id) { string datadir = "..\\..\\..\\..\\data\\" + site + "\\"; string postsdir = Path.Combine(datadir, "posts\\"); string path; if (!Directory.Exists(postsdir)) { Directory.CreateDirectory(postsdir); } Console.WriteLine("Saving single answer {0} from {1}...", id, site); SeApiClient client = new SeApiClient(APIURL, site); string a = client.LoadSingleAnswer(id); if (a == null) { throw new Exception("Failed to load answer " + id.ToString() + " from " + site); } path = Path.Combine(postsdir, "A" + id.ToString() + ".md"); TextWriter wr = new StreamWriter(path, false, Encoding.UTF8); using (wr) { dynamic data = JSON.Parse(a); AnswerMarkdown post = AnswerMarkdown.FromJsonData(site, data); post.ToMarkdown(wr); } Console.WriteLine("Success"); }
public static void SaveQuestion(string site, int id) { string datadir = "..\\..\\..\\..\\data\\" + site + "\\"; string postsdir = Path.Combine(datadir, "posts\\"); string path; if (!Directory.Exists(postsdir)) { Directory.CreateDirectory(postsdir); } SeApiClient client = new SeApiClient(APIURL, site); string q = client.LoadQuestion(id); if (q == null) { throw new Exception("Failed to load question " + id.ToString() + " from " + site); } path = Path.Combine(postsdir, "Q" + id.ToString() + ".md"); TextWriter wr = new StreamWriter(path, false, Encoding.UTF8); using (wr) { dynamic data = JSON.Parse(q); QuestionMarkdown post = QuestionMarkdown.FromJsonData(site, data); post.ToMarkdown(wr); } Dictionary <int, string> answers = client.LoadQuestionAnswers(id); Console.WriteLine("Saving {0} answers...", answers.Count); foreach (int key in answers.Keys) { path = Path.Combine(postsdir, "A" + key.ToString() + ".md"); wr = new StreamWriter(path, false, Encoding.UTF8); using (wr) { dynamic data = JSON.Parse(answers[key]); AnswerMarkdown post = AnswerMarkdown.FromJsonData(site, data); post.ToMarkdown(wr); } } }