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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package Torello.HTML.Tools.NewsSite;

import Torello.JavaDoc.LinkJavaSource;

import Torello.HTML.TagNode;
import Torello.HTML.HTMLNode;
import Torello.HTML.DotPair;
import Torello.HTML.TC;
import Torello.HTML.URLFilter;

import Torello.HTML.NodeSearch.TagNodeFindL1Inclusive;
import Torello.HTML.NodeSearch.InnerTagFindInclusive;
import Torello.HTML.NodeSearch.TagNodeGet;
import Torello.HTML.NodeSearch.InnerTagGet;
import Torello.HTML.NodeSearch.InnerTagGetInclusive;
import Torello.HTML.NodeSearch.TextComparitor;

import Torello.Java.StrFilter;
import Torello.Java.FileRW;
import Torello.Java.LFEC;
import Torello.Java.Country;
import Torello.Java.C;

import Torello.Languages.LC;

import java.util.Vector;
import java.util.Hashtable;

import java.util.regex.Pattern;

import java.net.URL;

import java.io.IOException;

/**
 * This class is nothing more than an 'Example Class' that contains some foreign-language
 * based news web-pages, from both overseas and from Latin America.
 * 
 * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=NEWS_SITES>
 */
public class NewsSites
{
    private NewsSites() { }

    @SuppressWarnings("unchecked")
    private static final Hashtable<String, Vector<URL>> newsPaperSections = 
        (Hashtable<String, Vector<URL>>) LFEC.readObjectFromFile_JAR
        (NewsSite.class, "data-files/SectionURLs.htdat", true, Hashtable.class);


    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=NEWS_SITES_RUN_EX>
     * @throws IOException This throws for IO errors that may occur when reading the web-server,
     * or when saving the web-pages or images to the file-system.
     * 
     * @see FileRW#delTree(String, boolean, Appendable)
     * @see NewsSite
     * @see FileRW#writeFile(CharSequence, String)
     * @see C#toHTML(String, boolean, boolean, boolean)
     */
    @LinkJavaSource(handle="RunExample")
    public static void runExample() throws IOException
    {
        // Click on the @LinkJavaSource Curved-Arrow to view the example code in full screen
        RunExample.run();
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=NEWS_SITES_MAIN>
     * @param argv These are the command line arguments passed by the JRE to this method.
     * 
     * @throws IOException If there are any problems while attempting to save the output to the
     * the output file (if one was named / requested).
     */
    public static void main(String[] argv) throws IOException
    {
        // Uncomment this line to run the example code (instead of section-data print)
        // runExample(); System.exit(0);

        // The data-file is loaded into private field "newsPaperSections"
        // This private field is a Hashtable<String, Vector<URL>>.  Convert each of
        // these sections so that they may be printed to terminal and maybe to a text
        // file.

        StringBuilder sb = new StringBuilder();

        for (String newspaper : newsPaperSections.keySet())
        {
            sb.append(newspaper + '\n');
            for (URL section : newsPaperSections.get(newspaper))
                sb.append(section.toString() + '\n');
            sb.append("\n\n***************************************************\n\n");
        }
        
        String s = sb.toString();
        System.out.println(s);
        
        // If there is a command-line parameter, it shall be interpreted a file-name.
        // The contents of the "sections data-file" (as text) will be written a file on the
        // file-system using the String-value of "argv[0]" as the name of the output-filename.

        if (argv.length == 1) FileRW.writeFile(s, argv[0]);
    }


    // URLFilter.regexKEEP(Pattern.compile("^http.+baidu\\.com\\/s\\?id=\\d+$")));
    // ArticleGet.usual(TextComparitor.CN_CI, "article-content"));

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=ABC_ES_LG>
     * @see TagNodeFindL1Inclusive#all(Vector, String)
     * @see TagNodeGet#first(Vector, int, int, TC, String[])
     * @see TagNode#AV(String)
     */
    public static Vector<String> ABC_LINKS_GETTER(URL url, Vector<HTMLNode> page)
    {
        final Vector<String> ret = new Vector<>();

        TagNode tn;
        String urlStr;

        // Links are kept inside <ARTICLE> ... </ARTICLE> on the main / section page.
        for (DotPair article : TagNodeFindL1Inclusive.all(page, "article"))

            // Now find the <A HREF=...> ... </A>
            if ((tn = TagNodeGet.first(page, article.start, article.end, TC.OpeningTags, "a"))
                != null)

                if ((urlStr = tn.AV("href")) != null)
                    ret.add(urlStr);

        return ret;
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=ABC_ES>
     * <EMBED CLASS='external-html' DATA-FILE-ID=NEWS_STE_CHANGE>
     */
    public static final NewsSite ABCES = new NewsSite
    (
        "ABC España",
        Country.Spain,
        "https://www.abc.es/",
        LC.ES,
        "ABC is a Spanish national daily newspaper.  It is the third largest general-interest " +
        "newspaper in Spain, and the oldest newspaper still operating in Madrid.",
        newsPaperSections.get("ABCES"),
        StrFilter.comparitor(TextComparitor.EW_CI, ".html"),
        NewsSites::ABC_LINKS_GETTER,
        ArticleGet.usual("main"),
        null // bannerAndAdFinder
    );

    /** <EMBED CLASS='external-html' DATA-FILE-ID=PULSO> */
    public static final NewsSite Pulso = new NewsSite
    (
        "El Pulso, México",
        Country.Mexico,
        "https://elpulso.mx",
        LC.ES,
        "El Pulso newspaper is Spanish language newspaper in Mexico. It is showing breaking " +
            "news, " +
        "headlines, kids news, tourism news, entertainment news, study news, industrial news, " +
        "economical news, health & beauty news, crime news, career news, Travel news, " +
        "diet & fitness news, Top stories, special news, celebrity news.",
        newsPaperSections.get("PULSO"),
        StrFilter.regExKEEP(Pattern.compile(
            "^https?:\\/{2}.*?\\/\\d{4}\\/\\d{2}\\/\\d{2}\\/[\\w-]{10,}\\/$"
        ), false),
        null, // LinksGet
        ArticleGet.usual(TextComparitor.C, "entry-content"),
        null // bannerAndAddFinder
    );

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=EL_NACIONAL_LG>
     * @see InnerTagFindInclusive#all(Vector, String, String, TextComparitor, String[])
     * @see TagNodeGet#first(Vector, int, int, TC, String[])
     * @see TagNode#AV(String)
     */
    public static Vector<String> EL_NACIONAL_LINKS_GETTER(URL url, Vector<HTMLNode> page)
    {
        Vector<String> ret = new Vector<>();       TagNode tn;     String urlStr;

        // Links are kept inside <DIV CLASS=td-module-thumb> ... </DIV> on the main / section page.
        for (DotPair article : InnerTagFindInclusive.all
            (page, "div", "class", TextComparitor.C, "td-module-thumb"))

            // Now find the <A HREF=...> ... </A>
            if ((tn = TagNodeGet.first
                (page, article.start, article.end, TC.OpeningTags, "a")) != null)

                if ((urlStr = tn.AV("href")) != null)
                    ret.add(urlStr);

        return ret;
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=EL_NACIONAL>
     * <EMBED CLASS='external-html' DATA-FILE-ID=NEWS_STE_CHANGE>
     */
    public static final NewsSite ElNacional = new NewsSite
    (
        "El Nacional",
        Country.Venezuela,
        "https://elnacional.com",
        LC.ES,
        "El Nacional is a Venezuelan publishing company under the name C.A. Editorial " +
            "El Nacional, " +
        "most widely known for its El Nacional newspaper and website. It, along with Últimas " +
        "Noticias and El Universal, are the most widely read and circulated daily national " +
        "newspapers in the country, and it has an average of more than 80,000 papers distributed " +
        "daily and 170,000 copies on weekends.",
        newsPaperSections.get("ElNacional"),
        (URLFilter) null, /* The LinksGetter will only return valid Anchor's */
        NewsSites::EL_NACIONAL_LINKS_GETTER,
        ArticleGet.usual("article"),
        null /* bannerAndAdFinder */
    );

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=EL_ESPECTADOR_LG>
     * @see InnerTagFindInclusive#all(Vector, String, String, TextComparitor, String[])
     * @see InnerTagGet#first(Vector, int, int, String, String, TextComparitor, String[])
     * @see TagNode#AV(String)
     */
    public static Vector<String> EL_ESPECTADOR_LINKS_GETTER(URL url, Vector<HTMLNode> page)
    {
        Vector<String> ret = new Vector<>();

        TagNode tn;
        String  urlStr;

        // Links are kept inside <DIV CLASS="Card ..."> ... </DIV> on the main / section page.
        for (DotPair article : InnerTagFindInclusive.all
            (page, "div", "class", TextComparitor.C, "Card"))

            // Now find the <A CLASS="card-link" HREF=...> ... </A>
            if ((tn = InnerTagGet.first
                (page, article.start, article.end, "a", "class", TextComparitor.C, "card-link"))
                    != null)

                if ((urlStr = tn.AV("href")) != null)
                    ret.add(urlStr);

        return ret;
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=EL_ESPECTADOR>
     * <EMBED CLASS='external-html' DATA-FILE-ID=NEWS_STE_CHANGE>
     */
    public static final NewsSite ElEspectador = new NewsSite
    (
        "El Espectador, Columbia",
        Country.Colombia, 
        "https://elespectador.com",
        LC.ES,
        "El Espectador (meaning \"The Spectator\") is a newspaper with national circulation within "+
        "Colombia, founded by Fidel Cano Gutiérrez on 22 March 1887 in Medellín and published "     +
        "since 1915 in Bogotá. It changed from a daily to a weekly edition in 2001, following a "   +
        "financial crisis, and became a daily again on 11 May 2008, a comeback which had been "     +
        "long rumoured, in tabloid format (28 x 39.5 cm). From 1997 to 2011 its main shareholder "  +
        "was Julio Mario Santo Domingo.",
        newsPaperSections.get("ElEspectador"),
        StrFilter.comparitor(TextComparitor.ENDS_WITH, "/"),
        NewsSites::EL_ESPECTADOR_LINKS_GETTER,
        ArticleGet.usual("article"),
        null /* bannerAndAdFinder */
    );

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=GOV_CN_CAROUSEL_LG>
     * @see InnerTagGetInclusive#first(Vector, String, String, TextComparitor, String[])
     * @see TagNodeGet#all(Vector, TC, String[])
     * @see TagNode#AV(String)
     */
    public static Vector<String> GOVCN_CAROUSEL_LINKS_GETTER(URL url, Vector<HTMLNode> page)
    {
        Vector<String>  ret     = new Vector<>();
        String          urlStr;

        // Find the first <DIV CLASS="slider-carousel"> ... </DIV> section
        Vector<HTMLNode> carouselDIV = InnerTagGetInclusive.first
            (page, "div", "class", TextComparitor.CN_CI, "slider-carousel");


        // Retrieve any HTML Anchor <A HREF=...> ... </A> found within the contents of the
        // Divider.

        for (TagNode tn: TagNodeGet.all(carouselDIV, TC.OpeningTags, "a"))
            if ((urlStr = tn.AV("href")) != null)
                ret.add(urlStr);

        return ret;
    };

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=GOV_CN_CAROUSEL>
     * <EMBED CLASS='external-html' DATA-FILE-ID=NEWS_STE_CHANGE>
     */
    public static final NewsSite GovCNCarousel = new NewsSite
    (
        "Chinese Government Web Portal",
        Country.China,
        "https://gov.cn/",
        LC.ZH_CN,
        "The Chinese Government Sponsored Web-Site",
        newsPaperSections.get("GovCNCarousel"),
        StrFilter.regExKEEP(Pattern.compile(
            "^http://www.gov.cn/(?:.+?/)?\\d{4}-\\d{2}/\\d{2}/(?:.+?/)?" +
                "content_\\d+.htm(?:l)?(#\\d+)?"
        ), false),
        NewsSites::GOVCN_CAROUSEL_LINKS_GETTER,
        ArticleGet.usual(TextComparitor.C, "article"),
        null /* bannerAndAddFinder */
    );

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=GOV_CN>
     * <EMBED CLASS='external-html' DATA-FILE-ID=NEWS_STE_CHANGE>
     */
    public static final NewsSite GovCN = new NewsSite
    (
        "Chinese Government Web Portal",
        Country.China,
        "https://gov.cn/",
        LC.ZH_CN,
        "The Chinese Government Sponsored Web-Site",
        newsPaperSections.get("GovCN"),
        StrFilter.regExKEEP(Pattern.compile(
            "^http://www.gov.cn/(?:.+?/)?\\d{4}-\\d{2}/\\d{2}/(?:.+?/)?" +
                "content_\\d+.htm(?:l)?(#\\d+)?"
        ), false),
        null,
        ArticleGet.usual(TextComparitor.C, "article"),
        null /* bannerAndAddFinder */
    );
}