Fix date/language meta data on e-hentai
[fanfix.git] / src / be / nikiroo / fanfix / supported / InfoReader.java
CommitLineData
68686a37
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStream;
333f0e7b 8import java.net.URL;
68686a37
NR
9import java.util.ArrayList;
10import java.util.List;
11
12import be.nikiroo.fanfix.data.MetaData;
13import be.nikiroo.utils.MarkableFileInputStream;
14
15// not complete: no "description" tag
16public class InfoReader {
57f02339
NR
17 public static MetaData readMeta(File infoFile, boolean withCover)
18 throws IOException {
68686a37
NR
19 if (infoFile == null) {
20 throw new IOException("File is null");
21 }
22
23 if (infoFile.exists()) {
24 InputStream in = new MarkableFileInputStream(new FileInputStream(
25 infoFile));
26 try {
57f02339 27 return createMeta(infoFile.toURI().toURL(), in, withCover);
68686a37
NR
28 } finally {
29 in.close();
30 in = null;
31 }
32 } else {
33 throw new FileNotFoundException(
34 "File given as argument does not exists: "
35 + infoFile.getAbsolutePath());
36 }
37 }
38
57f02339
NR
39 private static MetaData createMeta(URL sourceInfoFile, InputStream in,
40 boolean withCover) throws IOException {
68686a37
NR
41 MetaData meta = new MetaData();
42
43 meta.setTitle(getInfoTag(in, "TITLE"));
44 meta.setAuthor(getInfoTag(in, "AUTHOR"));
45 meta.setDate(getInfoTag(in, "DATE"));
46 meta.setTags(getInfoTagList(in, "TAGS", ","));
47 meta.setSource(getInfoTag(in, "SOURCE"));
2206ef66 48 meta.setUrl(getInfoTag(in, "URL"));
68686a37
NR
49 meta.setPublisher(getInfoTag(in, "PUBLISHER"));
50 meta.setUuid(getInfoTag(in, "UUID"));
51 meta.setLuid(getInfoTag(in, "LUID"));
52 meta.setLang(getInfoTag(in, "LANG"));
53 meta.setSubject(getInfoTag(in, "SUBJECT"));
54 meta.setType(getInfoTag(in, "TYPE"));
55 meta.setImageDocument(getInfoTagBoolean(in, "IMAGES_DOCUMENT", false));
57f02339
NR
56 if (withCover) {
57 meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
58 getInfoTag(in, "COVER")));
59 }
793f1071
NR
60 try {
61 meta.setWords(Long.parseLong(getInfoTag(in, "WORDCOUNT")));
62 } catch (NumberFormatException e) {
63 meta.setWords(0);
64 }
65 meta.setCreationDate(getInfoTag(in, "CREATION_DATE"));
a9eb3f46 66 meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
68686a37 67
57f02339 68 if (withCover && meta.getCover() == null) {
68686a37
NR
69 meta.setCover(BasicSupport.getDefaultCover(meta.getSubject()));
70 }
71
72 return meta;
73 }
74
75 private static boolean getInfoTagBoolean(InputStream in, String key,
76 boolean def) throws IOException {
77 Boolean value = getInfoTagBoolean(in, key);
78 return value == null ? def : value;
79 }
80
81 private static Boolean getInfoTagBoolean(InputStream in, String key)
82 throws IOException {
83 String value = getInfoTag(in, key);
84 if (value != null && !value.trim().isEmpty()) {
85 value = value.toLowerCase().trim();
86 return value.equals("1") || value.equals("on")
87 || value.equals("true") || value.equals("yes");
88 }
89
90 return null;
91 }
92
93 private static List<String> getInfoTagList(InputStream in, String key,
94 String separator) throws IOException {
95 List<String> list = new ArrayList<String>();
96 String tt = getInfoTag(in, key);
97 if (tt != null) {
98 for (String tag : tt.split(separator)) {
99 list.add(tag.trim());
100 }
101 }
102
103 return list;
104 }
105
106 /**
107 * Return the value of the given tag in the <tt>.info</tt> file if present.
108 *
109 * @param key
110 * the tag key
111 *
112 * @return the value or NULL
113 *
114 * @throws IOException
115 * in case of I/O error
116 */
117 private static String getInfoTag(InputStream in, String key)
118 throws IOException {
119 key = "^" + key + "=";
120
121 if (in != null) {
122 in.reset();
123 String value = BasicSupport.getLine(in, key, 0);
124 if (value != null && !value.isEmpty()) {
125 value = value.trim().substring(key.length() - 1).trim();
126 if (value.startsWith("'") && value.endsWith("'")
127 || value.startsWith("\"") && value.endsWith("\"")) {
128 value = value.substring(1, value.length() - 1).trim();
129 }
130
131 return value;
132 }
133 }
134
135 return null;
136 }
137}