csharp/9vult/MikuReader/MikuReader-Core/Core/Download/MangaDexDownload.cs

MangaDexDownload.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MikuReader.Core
{
    /// 
    /// Implementation of a Download for Mangadex
    /// 
    public clast MangaDexDownload : IDownload
    {
        private Chapter chapter;
        private ArrayList clients;
        private float total;

        public event DownloadCompletedEventHandler DownloadCompleted;

        /// 
        /// Create a new Download
        /// 
        /// Chapter to download
        public MangaDexDownload(Chapter chapter)
        {
            this.chapter = chapter;
            clients = new ArrayList();
            total = 0;
        }

        public void StartDownloading()
        {
            File.Create(Path.Combine(chapter.GetChapterRoot().Parent.FullName, "dl" + chapter.GetID())).Close();
            string jsonUrl = MangaDexHelper.MANGADEX_URL + "/api/chapter/" + chapter.GetID();
            string jsonString;

            using (var wc = new WebClient())
            {
                jsonString = wc.DownloadString(jsonUrl);
            }

            JObject jobj = JObject.Parse(jsonString);

            string server = (string)jobj["server"];
            string hash = (string)jobj["hash"];

            // string[] page_array = /* ((string) */ jobj["page_array"]. /* ).Split(',') */;
            List page_array = new List();
            IJEnumerable jtokens = jobj["page_array"].Values();
            foreach(JToken t in jtokens)
            {
                page_array.Add((string)t);
            }

            foreach (string file in page_array)
            {
                if (server == "/data/")
                    server = MangaDexHelper.MANGADEX_URL + "/data/";

                string imgUrl = server + hash + "/" + file;

                FileInfo imgFile = new FileInfo(
                    Path.Combine(
                        chapter.GetChapterRoot().FullName, 
                        ConvertToNumericFileName(file)
                ));

                if (File.Exists(imgFile.FullName))
                    if (imgFile.Length  {x34 , png}
            string name = Regex.Replace(parts[0], "[^0-9.]", "");
            return name + "." + input[1];  // -> 34.png
        }
    }
}