|
| 1 | +{ |
| 2 | + File: uComicInfo.pas |
| 3 | + License: GPLv2 |
| 4 | + This unit is a part of Free Manga Downloader |
| 5 | +} |
| 6 | + |
| 7 | +unit uComicInfo; |
| 8 | + |
| 9 | +{$mode objfpc}{$H+} |
| 10 | + |
| 11 | +interface |
| 12 | + |
| 13 | +uses |
| 14 | + Classes, SysUtils, uBaseUnit; |
| 15 | + |
| 16 | +type |
| 17 | + |
| 18 | + { TComicInfo } |
| 19 | + |
| 20 | + TComicInfo = class |
| 21 | + private |
| 22 | + class function EscapeXML(const AText: String): String; static; |
| 23 | + class function FormatDate(ADate: TDateTime): String; static; |
| 24 | + public |
| 25 | + class function GenerateComicInfoXML( |
| 26 | + const AFilePath: String; |
| 27 | + const ATitle: String; |
| 28 | + const ANumber: Integer; |
| 29 | + const AMangaInfo: TMangaInfo; |
| 30 | + const APageCount: Integer |
| 31 | + ): Boolean; static; |
| 32 | + |
| 33 | + class function GenerateExtraEPUBMetadata( |
| 34 | + const AFilePath: String; |
| 35 | + const ATitle: String; |
| 36 | + const ANumber: Integer; |
| 37 | + const AMangaInfo: TMangaInfo |
| 38 | + ): String; static; |
| 39 | + end; |
| 40 | + |
| 41 | +implementation |
| 42 | + |
| 43 | +{ TComicInfo } |
| 44 | + |
| 45 | +class function TComicInfo.EscapeXML(const AText: String): String; |
| 46 | +begin |
| 47 | + Result := AText; |
| 48 | + Result := StringReplace(Result, '&', '&', [rfReplaceAll]); |
| 49 | + Result := StringReplace(Result, '<', '<', [rfReplaceAll]); |
| 50 | + Result := StringReplace(Result, '>', '>', [rfReplaceAll]); |
| 51 | + Result := StringReplace(Result, '"', '"', [rfReplaceAll]); |
| 52 | + Result := StringReplace(Result, '''', ''', [rfReplaceAll]); |
| 53 | + Result := Trim(Result) |
| 54 | +end; |
| 55 | + |
| 56 | +class function TComicInfo.FormatDate(ADate: TDateTime): String; |
| 57 | +begin |
| 58 | + if ADate = 0 then |
| 59 | + Result := '' |
| 60 | + else |
| 61 | + Result := FormatDateTime('yyyy-MM-dd', ADate); |
| 62 | +end; |
| 63 | + |
| 64 | +class function TComicInfo.GenerateComicInfoXML( |
| 65 | + const AFilePath: String; |
| 66 | + const ATitle: String; |
| 67 | + const ANumber: Integer; |
| 68 | + const AMangaInfo: TMangaInfo; |
| 69 | + const APageCount: Integer |
| 70 | + ): Boolean; |
| 71 | +var |
| 72 | + XML: TStringList; |
| 73 | + L: TStringList; |
| 74 | +begin |
| 75 | + Result := False; |
| 76 | + if not Assigned(AMangaInfo) then Exit; |
| 77 | + |
| 78 | + L := nil; |
| 79 | + XML := TStringList.Create; |
| 80 | + try |
| 81 | + XML.Add('<?xml version="1.0" encoding="UTF-8"?>'); |
| 82 | + XML.Add('<ComicInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'); |
| 83 | + |
| 84 | + if AMangaInfo.Title <> '' then |
| 85 | + XML.Add(' <Series>' + EscapeXML(AMangaInfo.Title) + '</Series>'); |
| 86 | + |
| 87 | + if ATitle <> '' then |
| 88 | + XML.Add(' <Title>' + EscapeXML(ATitle) + '</Title>') |
| 89 | + else if AMangaInfo.Title <> '' then |
| 90 | + XML.Add(' <Title>' + EscapeXML(AMangaInfo.Title) + '</Title>'); |
| 91 | + |
| 92 | + XML.Add(' <Number>' + IntToStr(ANumber + 1) + '</Number>'); |
| 93 | + |
| 94 | + // If the Count matches the Volume count or Chapter count, then Kavita will assume the Series is Completed (you own all items of the series) |
| 95 | + if (AMangaInfo.Status = '0') then // completed |
| 96 | + XML.Add(' <Count>' + EscapeXML(IntToStr(AMangaInfo.ChapterLinks.Count)) + '</Count>'); |
| 97 | + |
| 98 | + if AMangaInfo.Summary <> '' then |
| 99 | + XML.Add(' <Summary>' + EscapeXML(AMangaInfo.Summary) + '</Summary>'); |
| 100 | + |
| 101 | + if AMangaInfo.Authors <> '' then |
| 102 | + XML.Add(' <Writer>' + EscapeXML(AMangaInfo.Authors) + '</Writer>'); |
| 103 | + |
| 104 | + if AMangaInfo.Artists <> '' then |
| 105 | + XML.Add(' <Penciller>' + EscapeXML(AMangaInfo.Artists) + '</Penciller>'); |
| 106 | + |
| 107 | + if AMangaInfo.Genres <> '' then |
| 108 | + XML.Add(' <Genre>' + EscapeXML(AMangaInfo.Genres) + '</Genre>'); |
| 109 | + |
| 110 | + case AMangaInfo.Status of |
| 111 | + '0': XML.Add(' <Notes>Series completed</Notes>'); |
| 112 | + '1': XML.Add(' <Notes>Series ongoing</Notes>'); |
| 113 | + '2': XML.Add(' <Notes>Series on hold</Notes>'); |
| 114 | + '3': XML.Add(' <Notes>Series cancelled</Notes>'); |
| 115 | + end; |
| 116 | + |
| 117 | + XML.Add(' <Web>' + EscapeXML(AMangaInfo.FullLink) + '</Web>'); |
| 118 | + |
| 119 | + if APageCount > 0 then |
| 120 | + XML.Add(' <PageCount>' + IntToStr(APageCount) + '</PageCount>'); |
| 121 | + |
| 122 | + XML.Add('</ComicInfo>'); |
| 123 | + |
| 124 | + if Trim(XML.Text) <> '' then |
| 125 | + begin |
| 126 | + L := TStringList.Create; |
| 127 | + L.Text := XML.Text; |
| 128 | + L.SaveToFile(AFilePath); |
| 129 | + Result := FileExists(AFilePath); |
| 130 | + end; |
| 131 | + finally |
| 132 | + L.Free; |
| 133 | + XML.Free; |
| 134 | + end; |
| 135 | +end; |
| 136 | + |
| 137 | +class function TComicInfo.GenerateExtraEPUBMetadata( |
| 138 | + const AFilePath: String; |
| 139 | + const ATitle: String; |
| 140 | + const ANumber: Integer; |
| 141 | + const AMangaInfo: TMangaInfo |
| 142 | +): String; |
| 143 | +var |
| 144 | + XML: TStringList; |
| 145 | + StatusText: String; |
| 146 | +begin |
| 147 | + Result := ''; |
| 148 | + if not Assigned(AMangaInfo) then Exit; |
| 149 | + |
| 150 | + XML := TStringList.Create; |
| 151 | + try |
| 152 | + if AMangaInfo.Authors <> '' then |
| 153 | + XML.Add(' <dc:creator opf:role="aut">' + EscapeXML(AMangaInfo.Authors) + '</dc:creator>'); |
| 154 | + |
| 155 | + if AMangaInfo.Artists <> '' then |
| 156 | + XML.Add(' <dc:creator opf:role="art">' + EscapeXML(AMangaInfo.Artists) + '</dc:creator>'); |
| 157 | + |
| 158 | + if AMangaInfo.Summary <> '' then |
| 159 | + XML.Add(' <dc:description>' + EscapeXML(AMangaInfo.Summary) + '</dc:description>'); |
| 160 | + |
| 161 | + if AMangaInfo.Title <> '' then |
| 162 | + XML.Add(' <meta name="series" content="' + EscapeXML(AMangaInfo.Title) + '"/>'); |
| 163 | + |
| 164 | + if ANumber >= 0 then |
| 165 | + XML.Add(' <meta name="chapter" content="' + IntToStr(ANumber + 1) + '"/>'); |
| 166 | + |
| 167 | + if AMangaInfo.Genres <> '' then |
| 168 | + XML.Add(' <meta name="genre" content="' + EscapeXML(AMangaInfo.Genres) + '"/>'); |
| 169 | + |
| 170 | + case AMangaInfo.Status of |
| 171 | + '0': StatusText := 'completed'; |
| 172 | + '1': StatusText := 'ongoing'; |
| 173 | + '2': StatusText := 'on_hold'; |
| 174 | + '3': StatusText := 'cancelled'; |
| 175 | + else |
| 176 | + StatusText := ''; |
| 177 | + end; |
| 178 | + |
| 179 | + if StatusText <> '' then |
| 180 | + XML.Add(' <meta name="status" content="' + StatusText + '"/>'); |
| 181 | + |
| 182 | + if AMangaInfo.Website <> '' then |
| 183 | + XML.Add(' <meta name="publisher" content="' + EscapeXML(AMangaInfo.Website) + '"/>'); |
| 184 | + |
| 185 | + XML.Add(' <meta property="dcterms:modified">' + |
| 186 | + FormatDateTime('yyyy-mm-dd"T"hh:nn:ss"Z"', Now) + |
| 187 | + '</meta>'); |
| 188 | + |
| 189 | + if Trim(XML.Text) <> '' then |
| 190 | + Result := XML.Text; |
| 191 | + finally |
| 192 | + XML.Free; |
| 193 | + end; |
| 194 | +end; |
| 195 | + |
| 196 | +end. |
0 commit comments