示例#1
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);
                }
            }
        }
示例#2
0
        public static new Dictionary <int, QuestionMarkdown> LoadFromJsonDir(string path, string site)
        {
            Dictionary <int, QuestionMarkdown> posts;

            string[] files = Directory.GetFiles(path, "Q*.json");
            posts = new Dictionary <int, QuestionMarkdown>(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);
                        posts[id] = QuestionMarkdown.FromJsonData(site, parser.JsonParse(json));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error reading file " + files[i]);
                        Console.WriteLine(ex.ToString());
                    }
                }
            }//end using

            return(posts);
        }