コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        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");
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public static void ConvertToMarkdown(string site, string src, string target)
        {
            string datadir   = "..\\..\\..\\..\\data\\" + site + "\\";
            string postsdir  = Path.Combine(datadir, src + "\\");
            string targetdir = Path.Combine(datadir, target + "\\");

            if (!Directory.Exists(targetdir))
            {
                Directory.CreateDirectory(targetdir);
            }

            Console.WriteLine("Converting data files to markdown ({0}/{1})...", site, src);

            Dictionary <int, PostMarkdown> posts = PostMarkdown.LoadFromJsonDir(postsdir, site);

            Console.WriteLine("Posts: {0}", posts.Count);
            PostMarkdown.SaveToDir(targetdir, posts.Values);

            Dictionary <int, QuestionMarkdown> questions = QuestionMarkdown.LoadFromJsonDir(postsdir, site);

            Console.WriteLine("Questions: {0}", questions.Count);
            QuestionMarkdown.SaveToDir(targetdir, questions.Values);

            Dictionary <int, AnswerMarkdown> answers = AnswerMarkdown.LoadFromJsonDir(postsdir, site);

            Console.WriteLine("Answers: {0}", answers.Count);
            AnswerMarkdown.SaveToDir(targetdir, answers.Values);
        }
コード例 #5
0
        public static new AnswerMarkdown FromJsonData(string siteval, object data)
        {
            AnswerMarkdown res = new AnswerMarkdown();

            res.site = siteval;

            dynamic owner = JSON.GetPropertyValue(data, "owner");

            if (owner != null)
            {
                dynamic owner_link = JSON.GetPropertyValue(owner, "link");
                dynamic owner_id   = JSON.GetPropertyValue(owner, "user_id");

                if (owner_link != null)
                {
                    res.UserLink = owner_link;
                }
                else
                {
                    res.UserLink = "";
                }

                if (owner_id != null)
                {
                    res.UserId = owner_id.ToString();
                }
                else
                {
                    res.UserId = "";
                }

                res.UserName = owner.display_name;
            }
            else
            {
                res.UserName = "******";
                res.UserLink = "";
                res.UserId   = "";
            }

            res.IsAccepted = (data as dynamic).is_accepted;
            res.PostType   = "answer";
            res.id         = (data as dynamic).answer_id;
            res.QuestionId = (data as dynamic).question_id;
            res.Title      = "Answer " + res.id.ToString();
            res.Body       = (data as dynamic).body;
            res.data       = res;
            return(res);
        }
コード例 #6
0
        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);
                }
            }
        }
コード例 #7
0
        public static new Dictionary <int, AnswerMarkdown> LoadFromJsonDir(string path, string site)
        {
            Dictionary <int, AnswerMarkdown> posts;

            string[] files = Directory.GetFiles(path, "A*.json");
            posts = new Dictionary <int, AnswerMarkdown>(files.Length);
            JSON parser = new JSON();

            using (parser)
            {
                for (int i = 0; i < files.Length; i++)
                {
                    string file  = Path.GetFileNameWithoutExtension(files[i]);
                    string idstr = file.Substring(1);
                    int    id;

                    if (!Int32.TryParse(idstr, out id))
                    {
                        Console.WriteLine("Bad answer id = {0} in file {1}", idstr, files[i]);
                        continue;
                    }

                    try
                    {
                        string json = File.ReadAllText(files[i], Encoding.UTF8);
                        posts[id] = AnswerMarkdown.FromJsonData(site, parser.JsonParse(json));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error reading file " + files[i]);
                        Console.WriteLine(ex.ToString());
                    }
                }
            }//end using

            return(posts);
        }
コード例 #8
0
        public static new AnswerMarkdown FromMarkdown(string siteval, TextReader src)
        {
            AnswerMarkdown res = new AnswerMarkdown();

            res.site = siteval;

            bool          reading_yml  = false;
            bool          reading_body = false;
            StringBuilder sbBody       = new StringBuilder(5000);

            string param_name;
            string param_val;

            while (true)
            {
                string line = src.ReadLine();
                if (line == null)
                {
                    break;
                }

                if (line == "---" && reading_yml == false && reading_body == false)
                {
                    //start YML block
                    reading_yml = true;
                    continue;
                }

                if (line == "---" && reading_yml == true && reading_body == false)
                {
                    //end YML block
                    reading_yml  = false;
                    reading_body = true;
                    continue;
                }

                if (reading_yml)
                {
                    if (line.Length <= 2)
                    {
                        continue;
                    }
                    int index = line.IndexOf(':');
                    if (index < 0)
                    {
                        index = line.Length - 2;
                    }

                    param_name = line.Substring(0, index);
                    param_val  = line.Substring(index + 1);

                    param_val = param_val.Trim();

                    if (param_val == "\"\"")
                    {
                        param_val = "";
                    }

                    if (param_val.Length > 2)
                    {
                        if (param_val[0] == '"')
                        {
                            param_val = param_val.Substring(1);
                        }
                        if (param_val[param_val.Length - 1] == '"')
                        {
                            param_val = param_val.Substring(0, param_val.Length - 1);
                        }
                    }

                    param_val = param_val.Replace("\\\"", "\"");
                    param_val = param_val.Replace("\\\\", "\\");

                    switch (param_name)
                    {
                    case "title": res.Title = param_val; continue;

                    case "se.owner.user_id": res.UserId = param_val; continue;

                    case "se.owner.display_name": res.UserName = param_val; continue;

                    case "se.owner.link": res.UserLink = param_val; continue;

                    case "se.link": res.Link = param_val; continue;

                    case "se.is_accepted": res.IsAccepted = Boolean.Parse(param_val); continue;

                    case "se.answer_id": res.id = Int32.Parse(param_val); continue;

                    case "se.question_id": res.QuestionId = Int32.Parse(param_val); continue;

                    case "se.score": res.Score = Int32.Parse(param_val); continue;
                    }
                }
                else if (reading_body)
                {
                    sbBody.AppendLine(line);
                }
            }

            res.Body     = sbBody.ToString();
            res.PostType = "answer";
            res.data     = res;

            if (String.IsNullOrEmpty(res.Title))
            {
                res.Title = "Answer " + res.id.ToString();
            }

            return(res);
        }
コード例 #9
0
 public void AddAnswer(AnswerMarkdown a)
 {
     this.answers.Add(a);
     a.Parent = this;
 }
コード例 #10
0
        public static PostSet LoadFromDir(string path, string site)
        {
            Dictionary <int, Question>         questions;
            Dictionary <int, Answer>           answers;
            Dictionary <int, Answer>           single_answers;
            Dictionary <int, QuestionMarkdown> mdquestions;
            Dictionary <int, AnswerMarkdown>   mdanswers;
            Dictionary <int, AnswerMarkdown>   md_single_answers;

            string[] files = Directory.GetFiles(path, "Q*.json");
            questions = new Dictionary <int, Question>(files.Length);
            JSON parser = new JSON();

            using (parser)
            {
                for (int i = 0; i < files.Length; i++)
                {
                    string file  = Path.GetFileNameWithoutExtension(files[i]);
                    string idstr = file.Substring(1);
                    int    id;

                    if (!Int32.TryParse(idstr, out id))
                    {
                        Console.WriteLine("Bad question id = {0} in file {1}", idstr, files[i]);
                        continue;
                    }

                    try
                    {
                        string   json = File.ReadAllText(files[i], Encoding.UTF8);
                        Question q    = new Question(site, id, parser.JsonParse(json));
                        questions[id] = q;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error reading file " + files[i]);
                        Console.WriteLine(ex.ToString());
                    }
                }

                files          = Directory.GetFiles(path, "A*.json");
                answers        = new Dictionary <int, Answer>(files.Length);
                single_answers = new Dictionary <int, Answer>(files.Length);

                for (int i = 0; i < files.Length; i++)
                {
                    string file  = Path.GetFileNameWithoutExtension(files[i]);
                    string idstr = file.Substring(1);
                    int    id;

                    if (!Int32.TryParse(idstr, out id))
                    {
                        Console.WriteLine("Bad answer id = {0} in file {1}", idstr, files[i]);
                        continue;
                    }

                    try
                    {
                        string json = File.ReadAllText(files[i], Encoding.UTF8);
                        Answer a    = new Answer(site, id, parser.JsonParse(json));
                        answers[id] = a;

                        int qid = a.DataDynamic.question_id;

                        if (questions.ContainsKey(qid))
                        {
                            questions[qid].AddAnswer(a);
                        }
                        else
                        {
                            single_answers[id] = a;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error reading file " + files[i]);
                        Console.WriteLine(ex.ToString());
                    }
                }
            }//end using

            files       = Directory.GetFiles(path, "Q*.md");
            mdquestions = new Dictionary <int, QuestionMarkdown>(files.Length);

            for (int i = 0; i < files.Length; i++)
            {
                string file  = Path.GetFileNameWithoutExtension(files[i]);
                string idstr = file.Substring(1);
                int    id;

                if (!Int32.TryParse(idstr, out id))
                {
                    Console.WriteLine("Bad question id = {0} in file {1}", idstr, files[i]);
                    continue;
                }

                try
                {
                    TextReader read = new StreamReader(files[i], Encoding.UTF8);

                    using (read)
                    {
                        mdquestions[id] = QuestionMarkdown.FromMarkdown(site, read);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error reading file " + files[i]);
                    Console.WriteLine(ex.ToString());
                }
            }

            files             = Directory.GetFiles(path, "A*.md");
            mdanswers         = new Dictionary <int, AnswerMarkdown>(files.Length);
            md_single_answers = new Dictionary <int, AnswerMarkdown>(files.Length);

            for (int i = 0; i < files.Length; i++)
            {
                string file  = Path.GetFileNameWithoutExtension(files[i]);
                string idstr = file.Substring(1);
                int    id;

                if (!Int32.TryParse(idstr, out id))
                {
                    Console.WriteLine("Bad answer id = {0} in file {1}", idstr, files[i]);
                    continue;
                }

                try
                {
                    TextReader read = new StreamReader(files[i], Encoding.UTF8);

                    using (read)
                    {
                        mdanswers[id] = AnswerMarkdown.FromMarkdown(site, read);
                    }

                    int qid = mdanswers[id].QuestionId;

                    if (mdquestions.ContainsKey(qid))
                    {
                        mdquestions[qid].AddAnswer(mdanswers[id]);
                    }
                    else
                    {
                        md_single_answers[id] = mdanswers[id];
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error reading file " + files[i]);
                    Console.WriteLine(ex.ToString());
                }
            }

            return(new PostSet
            {
                Questions = questions,
                SingleAnswers = single_answers,
                MarkdownQuestions = mdquestions,
                MarkdownAnswers = md_single_answers,
                AllMarkdownAnswers = mdanswers
            });
        }