public static string DownloadFile(string url, string directory, string referer = "https://zhihu.com", string userAgent = "Mozilla/5.0 (iPad; CPU OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60", string cookie = "", string extension = "A") { try { Directory.CreateDirectory(directory); using (var web = new WebClient()) { web.Headers.Set(HttpRequestHeader.Referer, referer); web.Headers.Set(HttpRequestHeader.UserAgent, userAgent); web.Headers.Set(HttpRequestHeader.Cookie, cookie); if (url.StartsWith("//")) { url = "http:" + url; } if (url.StartsWith("/") && !url.StartsWith("//")) { url = "http://mp.qq.com" + url; } if (extension == "A") { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); switch (response.ContentType) { case "image/gif": return(""); //extension = ".gif"; break; case "image/jpeg": extension = ".jpg"; break; case "image/png": extension = ".png"; break; default: break; } } string fileName = HashTools.Hash_MD5_16(url) + extension; if (!File.Exists(fileName)) { web.DownloadFile(url, directory + fileName); } return(fileName); } } catch (Exception) { return(""); } }
private void makeOpfFile() { StringBuilder opf = new StringBuilder("<?xml version='1.0' encoding='utf-8'?>"); opf.AppendFormat(Environment.NewLine + @"<package xmlns=""http://www.idpf.org/2007/opf"" version=""2.0"" unique-identifier=""{0}"">", HashTools.Hash_MD5_16(title + date)); opf.AppendFormat(Environment.NewLine + @"<metadata>" + Environment.NewLine + @" <dc-metadata xmlns:dc=""http://purl.org/dc/elements/1.1/"" xmlns:opf=""http://www.idpf.org/2007/opf"">" + Environment.NewLine + @" <dc:title>{0}</dc:title>" + Environment.NewLine + @" <dc:language>{1}</dc:language>" + Environment.NewLine + @" <dc:Identifier id=""uid"">{2}</dc:Identifier>" + Environment.NewLine + @" <dc:creator>{3}</dc:creator>" + Environment.NewLine + @" <dc:publisher>{4}</dc:publisher>" + Environment.NewLine + @" <dc:subject>{5}</dc:subject>" + Environment.NewLine + @" <dc:date>{6}</dc:date>" + Environment.NewLine + @" <dc:description>{7}</dc:description>" + Environment.NewLine + @" </dc-metadata>", title, language, Guid.NewGuid(), creator, publisher, subject, date, description); opf.AppendFormat(Environment.NewLine + @"<x-metadata>" + Environment.NewLine + @" <output content-type=""application/x-mobipocket-subscription-magazine"" encoding =""utf-8""/>" + Environment.NewLine + @" <EmbeddedCover>./image/cover.jpg</EmbeddedCover>" + Environment.NewLine + @" </x-metadata>" + Environment.NewLine + @"</metadata>"); opf.Append(Environment.NewLine + "<manifest>"); int i = 0; foreach (Story.Manifestx m in manifestList) { opf.AppendFormat(Environment.NewLine + @" <item href=""{0}"" media-type=""application/xhtml+xml"" id=""{1}""/>", (m[0])[0].Replace("/files", ""), string.Format("{0:D3}-html", ++i));//添加内部文章 html } i = 0; foreach (Story.Manifestx m in manifestList) { foreach (string ima in (List <string>)m[1]) { string extension = Regex.Match(ima, @".[^.]*?$").Value; string type = ""; switch (extension) { case ".gif": type = "image/gif"; break; //extension = ".gif"; break; case ".jpg": type = "image/jpeg"; break; case ".png": type = "image/png"; break; default: continue; //不添加这个图片 } opf.AppendFormat(Environment.NewLine + @" <item href=""{0}"" media-type=""{1}"" id=""{2}""/>", ima.Replace(@"/files", ""), type, string.Format("{0:D3}-image", ++i)); //添加 html 对应的图片 } } opf.AppendFormat(Environment.NewLine + @" <item href=""./html/contents.html"" media-type=""application/xhtml+xml"" id=""contents""/>" + Environment.NewLine + @" <item href=""./misc/nav-contents.ncx"" media-type=""application/x-dtbncx+xml"" id=""nav-contents""/>" + Environment.NewLine + @" <item href= ""./image/cover.jpg"" media-type= ""image/jpeg"" id= ""cover_img""/>" + Environment.NewLine + @"</manifest>" + Environment.NewLine + @"<spine toc=""nav-contents"">" + Environment.NewLine + @" <itemref idref=""contents""/>"); for (i = 0; i < manifestList.Count; i++) { opf.AppendFormat(Environment.NewLine + @" <itemref idref=""{0}""/>", string.Format("{0:D3}-html", i + 1)); } opf.Append(Environment.NewLine + @"</spine>" + Environment.NewLine + @"</package>"); System.IO.File.WriteAllText(@"files\" + title + ".opf", opf.ToString(), Encoding.UTF8); }