csharp/9vult/MikuReader/MikuReader-Core/Core/Title/nHentai/Nhentai.cs

Nhentai.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MikuReader.Core
{
    /// 
    /// Representation of a Manga
    /// 
    public clast Nhentai : Hentai
    {
        private string id;
        private string name;
        private string usersatle;
        private string currentchapter;
        private string currentpage;

        private List chapters;
        private DirectoryInfo hentaiRoot;
        
        /// 
        /// Create a new Hentai when the files exist
        /// 
        /// Root directory for this Hentai
        public Nhentai(DirectoryInfo location)
        {
            this.hentaiRoot = location;
            chapters = new List();
            _Load();
        }

        /// 
        /// Create a new manga and its files
        /// 
        /// JSON Text for this Hentai
        public Nhentai(DirectoryInfo location, string jsonText)
        {
            JObject jobj = JObject.Parse(jsonText);

            this.hentaiRoot = location;
            this.name = (string)jobj["hentai"]["satle"];
            this.id = (string)jobj["hentai"]["num"];
            string url = (string)jobj["hentai"]["url"];

            chapters = new List();
            _Create(url);
        }

        /// 
        /// Loads Manga info from manga.txt
        /// 
        public override void _Load()
        {
            string input = File.ReadAllText(FileHelper.GetFilePath(hentaiRoot, "manga.json"));
            MangaInfo info = JsonConvert.DeserializeObject(input);

            id = info.Id;
            name = info.Name;
            usersatle = info.UserName;
            currentpage = info.Page;

            _PopulateChapters();
        }

        /// 
        /// Creates manga.txt, then calls Load()
        /// 
        /// 
        public override void _Create(string mangaUrl)
        {
            FileHelper.CreateFolder(FileHelper.APP_ROOT, "h" + id);

            MangaInfo info = new MangaInfo()
            {
                Type = "hentai",
                Source = "nhentai",
                Id = id,
                Name = name,
                UserName = usersatle,
                Chapter = "1",
                Page = "1"
            };

            string output = JsonConvert.SerializeObject(info);            
            File.WriteAllText(Path.Combine(hentaiRoot.FullName, "manga.json"), output);

            DirectoryInfo chapDir = FileHelper.CreateFolder(hentaiRoot, "1");
            chapters.Add(new Chapter(chapDir, id, "1", true));

            _Load();
        }

        /// 
        /// Create a Chapter for each chapter and add it to the chapter list
        /// 
        public override void _PopulateChapters()
        {
            foreach (DirectoryInfo di in FileHelper.GetDirs(hentaiRoot))
            {
                chapters.Add(new Chapter(di));
            }
        }

        public override void UpdateLocation(string chapter, string page)
        {
            this.currentchapter = chapter;
            this.currentpage = page;
        }

        public override void Save(string chapter, string page)
        {
            this.currentchapter = chapter;
            this.currentpage = page;

            MangaInfo info = new MangaInfo()
            {
                Type = "hentai",
                Source = "nhentai",
                Id = hentaiRoot.Name,
                Name = name,
                UserName = usersatle,
                Chapter = chapter,
                Page = page
            };

            string output = JsonConvert.SerializeObject(info);
            File.WriteAllText(Path.Combine(hentaiRoot.FullName, "manga.json"), output);
        }

        public override void UpdateProperties(string satle, string lang, string group)
        {
            this.usersatle = satle;
            Save(currentchapter, currentpage);
        }

        public override string Getsatle()
        {
            return this.name;
        }

        public override string GetUsersatle()
        {
            return this.usersatle;
        }

        public override List GetChapters()
        {
            return chapters;
        }

        public override string GetCurrentChapter()
        {
            return currentchapter;
        }

        public override string GetCurrentPage()
        {
            return currentpage;
        }

        public override string GetID()
        {
            return id;
        }

        public override bool IsDownloading()
        {
            FileInfo[] files = FileHelper.GetFiles(hentaiRoot);
            foreach (FileInfo f in files)
                if (f.Name.StartsWith("dl"))
                    return true;
            return false;
        }
    }
}