summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/types/Article.java
blob: dbfe8a7224c49fe93ad979f94b11091feb95600f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package org.fox.ttrss.types;

import android.os.Parcel;
import android.os.Parcelable;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// TODO: serialize Labels
public class Article implements Parcelable {
	public static final int TYPE_LOADMORE = -1;
	public static final int TYPE_AMR_FOOTER = -2;

	public static final int FLAVOR_KIND_ALBUM = 1;
	public static final int FLAVOR_KIND_VIDEO = 2;
	public static final int FLAVOR_KIND_YOUTUBE = 3;

	public static final int SCORE_LOW = -500;
	public static final int SCORE_HIGH = 500;

	public int id;
	public boolean unread; 
	public boolean marked; 
	public boolean published; 
	public int score;
	public int updated; 
	public boolean is_updated; 
	public String title; 
	public String link; 
	public int feed_id; 
	public List<String> tags;
	public List<Attachment> attachments;
	public String content;
    public String excerpt;
	public List<List<String>> labels;
	public String feed_title;	
	public int comments_count;
	public String comments_link;
	public boolean always_display_attachments;
	public String author;
	public String note;
    public boolean selected;
    public String flavor_image;
    public String flavor_stream;
    public int flavor_kind;
	public String site_url;

	/* not serialized */
	transient public Document articleDoc;
	transient public Element flavorImage;

	transient public String flavorImageUri;
	transient public String flavorStreamUri;
	transient public String youtubeVid;
	transient public List<Element> mediaList = new ArrayList<>();
	transient public int flavorViewHeight;

	public Article(Parcel in) {
		readFromParcel(in);
	}
	
	public Article() {
		
	}

	public void cleanupExcerpt() {
		if (excerpt != null) {
			excerpt = excerpt.replace("&hellip;", "…");
			excerpt = excerpt.replace("]]>", "");
			excerpt = Jsoup.parse(excerpt).text();
		}
	}

	public void collectMediaInfo() {
		if (flavor_image != null && flavor_image.length() > 0) {
			flavorImageUri = flavor_image;

			flavorImage = new Element("img")
					.attr("src", flavorImageUri);

			if (flavor_stream != null && flavor_stream.length() > 0) {
				flavorStreamUri = flavor_stream;
			}

			return;
		}

		articleDoc = Jsoup.parse(content);

		if (articleDoc != null) {
			mediaList = articleDoc.select("img,video,iframe[src*=youtube.com/embed/]");

			for (Element e : mediaList) {
				if ("iframe".equals(e.tagName().toLowerCase())) {
					flavorImage = e;
					break;
				} /*else if ("video".equals(e.tagName().toLowerCase())) {
					flavorImage = e;
					break;
				}*/
			}

			if (flavorImage == null) {
				for (Element e : mediaList) {
					flavorImage = e;
					break;
				}
			}

			if (flavorImage != null) {
				try {

					if ("video".equals(flavorImage.tagName().toLowerCase())) {
						Element source = flavorImage.select("source").first();
						flavorStreamUri = source.attr("src");

						flavorImageUri = flavorImage.attr("poster");
					} else if ("iframe".equals(flavorImage.tagName().toLowerCase())) {

						String srcEmbed = flavorImage.attr("src");

						if (srcEmbed.length() > 0) {
							Pattern pattern = Pattern.compile("/embed/([\\w-]+)");
							Matcher matcher = pattern.matcher(srcEmbed);

							if (matcher.find()) {
								youtubeVid = matcher.group(1);

								flavorImageUri = "https://img.youtube.com/vi/" + youtubeVid + "/hqdefault.jpg";
								flavorStreamUri = "https://youtu.be/" + youtubeVid;
							}
						}
					} else {
						flavorImageUri = flavorImage.attr("src");

						if (flavorImageUri != null && flavorImageUri.startsWith("//")) {
							flavorImageUri = "https:" + flavorImageUri;
						}

						flavorStreamUri = null;
					}
				} catch (Exception e) {
					e.printStackTrace();

					flavorImage = null;
					flavorImageUri = null;
					flavorStreamUri = null;
				}
			}
		}

		if (flavorImageUri == null) {
			// consider attachments
			if (attachments != null) {
				for (Attachment a : attachments) {
					if (a.content_type != null && a.content_type.contains("image/")) {
						flavorImageUri = a.content_url;

						if (flavorImageUri != null && flavorImageUri.startsWith("//")) {
							flavorImageUri = "https:" + flavorImageUri;
						}

						// this is needed for the gallery view
						flavorImage = new Element("img")
								.attr("src", flavorImageUri);

						break;
					}
				}
			}
		}

		//Log.d("Article", "collectMediaInfo: " + flavorImage);
	}

	public Article(int id) {
		this.id = id;
		this.title = "ID:" + id;
		this.link = "";
		this.tags = new ArrayList<String>();
	}

	@Override
	public int describeContents() {
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel out, int flags) {
		out.writeInt(id);
		out.writeInt(unread ? 1 : 0);
		out.writeInt(marked ? 1 : 0);
		out.writeInt(published ? 1 : 0);
		out.writeInt(score);
		out.writeInt(updated);
		out.writeInt(is_updated ? 1 : 0);
		out.writeString(title);
		out.writeString(link);
		out.writeInt(feed_id);
		out.writeStringList(tags);
		out.writeString(content);
        out.writeString(excerpt);
		out.writeList(attachments);
		out.writeString(feed_title);
		out.writeInt(comments_count);
		out.writeString(comments_link);
		out.writeInt(always_display_attachments ? 1 : 0);
		out.writeString(author);
		out.writeString(note);
        out.writeInt(selected ? 1 : 0);
		out.writeString(site_url);
	}
	
	public void readFromParcel(Parcel in) {
		id = in.readInt();
		unread = in.readInt() == 1;
		marked = in.readInt() == 1;
		published = in.readInt() == 1;
		score = in.readInt();
		updated = in.readInt();
		is_updated = in.readInt() == 1;
		title = in.readString();
		link = in.readString();
		feed_id = in.readInt();
		
		if (tags == null) tags = new ArrayList<String>();
		in.readStringList(tags);
		
		content = in.readString();
        excerpt = in.readString();
		
		attachments = new ArrayList<Attachment>();
		in.readList(attachments, Attachment.class.getClassLoader());
		
		feed_title = in.readString();
		
		comments_count = in.readInt();
		comments_link = in.readString();
		always_display_attachments = in.readInt() == 1;
		author = in.readString();
		note = in.readString();
        selected = in.readInt() == 1;
		site_url = in.readString();
	}
	
	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator CREATOR =
    	new Parcelable.Creator() {
            public Article createFromParcel(Parcel in) {
                return new Article(in);
            }
 
            public Article[] newArray(int size) {
                return new Article[size];
            }
        };
}