Source

csxj-crawler / csxj / datasources / sudinfo.py

Full commit
  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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# -*- coding: utf-8 -*-

import sys
from datetime import datetime
import locale
from itertools import chain
import urllib
import urllib2
from urlparse import urlparse
import codecs

from scrapy.selector import HtmlXPathSelector

from common.utils import fetch_content_from_url, fetch_html_content
from common.utils import extract_plaintext_urls_from_text
from common.utils import convert_utf8_url_to_ascii
from csxj.common.tagging import  classify_and_tag, make_tagged_url
from csxj.db.article import ArticleData



# for datetime conversions
if sys.platform in ['linux2', 'cygwin']:
    locale.setlocale(locale.LC_TIME, 'fr_FR.UTF8')
elif sys.platform in [ 'darwin']:
    locale.setlocale(locale.LC_TIME, 'fr_FR')
elif sys.platform in [ 'win32']:
    # locale string from: http://msdn.microsoft.com/en-us/library/cdax410z(v=VS.80).aspx
    locale.setlocale(locale.LC_ALL, 'fra')


SUDINFO_INTERNAL_SITES = {
    'portfolio.sudinfo.be':['internal site', 'images', 'gallery'],
    'portfolio.sudpresse.be':['internal site', 'images', 'gallery'],
    'bassenge.blogs.sudinfo.be':['internal blog', 'blog'],
    'pdf.lameuse.be':['internal site', 'pdf newspaper'],
    'football.sudpresse.be':['sports', 'internal site'],

}

SUDINFO_OWN_NETLOC = 'www.sudinfo.be'
SUDINFO_OWN_DOMAIN = 'sudinfo.be'

SOURCE_TITLE = u"Sud Info"
SOURCE_NAME = u"sudinfo"







def extract_date(hxs):
    today = datetime.today()

    date_string = u"".join(hxs.select("//p[@class='publiele']//text()").extract())
    date_bytestring = codecs.encode(date_string, 'utf-8')


    if date_string.startswith(u"Publié le "):
        date_fmt = codecs.encode(u"Publié le %A %d %B %Y à %Hh%M", 'utf-8')
    elif date_string.startswith(u"Mis à jour le "):
        date_fmt = codecs.encode(u"Mis à jour le %A %d %B %Y à %Hh%M", 'utf-8')
    else:
        raise ValueError("Unsupported date format. Update your parser.")

    datetime_published = datetime.strptime(date_bytestring, date_fmt)

    return datetime_published.date(), datetime_published.time()



def extract_title_and_url(link_hxs):
    url = link_hxs.select("./@href").extract()[0]
    # sometimes, there are links only I can see.
    if link_hxs.select(".//text()").extract():
        title = link_hxs.select(".//text()").extract()[0].strip()
        # maybe it was a space only string, which is not really interesting either
        if not title:
            title = "__GHOST_LINK__"
    else:
        title = "__GHOST_LINK__"
    return title, url


def extract_img_link_info(link_hxs):
    url = link_hxs.select("./@href").extract()[0]
    title = link_hxs.select("./img/@src").extract()[0].strip()
    return title, url


def extract_text_and_links_from_paragraph(paragraph_hxs):
    def separate_img_and_text_links(links):
        img_links = [l for l in links if l.select("./img")]
        text_links = [l for l in links if l not in img_links]

        return [extract_title_and_url(link) for link in text_links],  [extract_img_link_info(link) for link in img_links]


    links = paragraph_hxs.select(".//a")

    titles_and_urls, img_targets_and_urls = separate_img_and_text_links(links)

    tagged_urls = list()
    for title, url in titles_and_urls:
        tags = classify_and_tag(url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES)
        tags.update(['in text'])
        if title == "__GHOST_LINK__":
            tags.update(['ghost link'])
        tagged_urls.append(make_tagged_url(url, title, tags))

    for img_target, url in img_targets_and_urls:
        tags = classify_and_tag(url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES)
        tags.update(['in text', 'embedded image'])
        tagged_urls.append(make_tagged_url(url, img_target, tags))

    text_fragments  = paragraph_hxs.select(".//text()").extract()
    if text_fragments:
        text = ' '.join(text_fragments)
        plaintext_urls = extract_plaintext_urls_from_text(text)
        for url in plaintext_urls:
            tags = classify_and_tag(url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES)
            tags.update(['plaintext', 'in text'])

            tagged_urls.append(make_tagged_url(url, url, tags))
    else:
        text = u""


    return text, tagged_urls



def extract_intro_and_links(hxs):
    intro_container = hxs.select("//div [@id='article']/p[starts-with(@class, 'chapeau')]/following-sibling::*[1]")
    intro_text, tagged_urls = extract_text_and_links_from_paragraph(intro_container)
    return intro_text, tagged_urls



def extract_links_from_media_items(media_items):
    """ Extract and tag links from the list of embedded items.
    Returns a list of TaggedURL

    Parameters:

        @media_items: list of html selectors pointing to every list item (<li>)
    """


    def extract_and_tag_url_from_iframe(item):
        embedded_frame = item.select(".//iframe")
        if embedded_frame:
            target_url = embedded_frame.select("./@src").extract()[0]
            tags = classify_and_tag(target_url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES)
            tags.update(['embedded document', 'iframe'])
            return make_tagged_url(target_url, title, tags)
        else:
            return None


    tagged_urls = list()
    for item in media_items:
        media_type = item.select("./@class").extract()[0]

        title = item.select('./h3/text()').extract()
        if title:
            title = title[0]
        else:
            title = "__NO_TITLE__"

        if  media_type == 'video':
            if item.select(".//div [contains(@class, 'emvideo-kewego')]"):
                url = item.select(".//div [contains(@class, 'emvideo-kewego')]//video/@poster").extract()
                if url:
                    url = url[0]
                    tags = classify_and_tag(url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES)
                    tags.update(['bottom video', 'embedded video', 'embedded', 'kewego'])
                    tagged_urls.append(make_tagged_url(url, title, tags))
                else:
                    raise ValueError("There is a kewego video here somewhere, but we could not find the link.")
            elif item.select(".//div [contains(@class, 'emvideo-youtube')]"):
                url = item.select(".//div [contains(@class, 'emvideo-youtube')]//object/@data").extract()
                if url:
                    url = url[0]
                    tags = classify_and_tag(url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES)
                    tags.update(['bottom video', 'embedded video', 'embedded', 'youtube'])
                    tagged_urls.append(make_tagged_url(url, title, tags))
                else:
                    raise ValueError("There is a youtube video here somewhere, but we could not find the link.")
            else:
                raise ValueError("A unknown type of embedded video has been detected. Please update this parser.")
        elif media_type == 'document':
            embedded_widget_url = extract_and_tag_url_from_iframe(item)
            if embedded_widget_url:
                tagged_urls.append(embedded_widget_url)
            else:
                raise ValueError("This document does not embed an iframe. Please update this parser.")
        elif media_type == "links":
            links = item.select("./span/a")
            for l in links:
                title, url = extract_title_and_url(l)
                tags = classify_and_tag(url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES)
                tagged_urls.append(make_tagged_url(url, title, tags))
        elif media_type == "other":
            embedded_widget_url = extract_and_tag_url_from_iframe(item)
            if embedded_widget_url:
                tagged_urls.append(embedded_widget_url)
            else:
                raise ValueError("The expected iframe object was not found. Please update this parser.")
        else:
            raise ValueError("Unknown media type ('{0}') detected. Please update this parser.".format(media_type))



    return tagged_urls


def extract_content_and_links(hxs):
    content_paragraphs_hxs = hxs.select("//div [@id='article']/p[starts-with(@class, 'publiele')]/following-sibling::p")

    all_content_paragraphs, all_tagged_urls = list(), list()

    # process paragraphs
    for p in content_paragraphs_hxs:
        text, tagged_urls = extract_text_and_links_from_paragraph(p)
        all_content_paragraphs.append(text)
        all_tagged_urls.extend(tagged_urls)


    #extract embedded videos
    divs = hxs.select("//div [@id='article']/p[starts-with(@class, 'publiele')]/following-sibling::div/div [@class='bottomVideos']")

    for div in divs:
        urls = div.select("./div [contains(@class, 'emvideo-kewego')]//video/@poster").extract()
        for url in urls:
            tags = classify_and_tag(url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES)
            tags.update(['bottom video', 'embedded video', 'embedded'])

            all_tagged_urls.append(make_tagged_url(url, url, tags))


    new_media_items = hxs.select("//div [@class='digital-wally_digitalobject']//li")

    all_tagged_urls.extend(extract_links_from_media_items(new_media_items))

    return all_content_paragraphs, all_tagged_urls



LINK_TYPE_TO_TAG = {
    'media-video':['video'],
    'media-press':[],
}


def extract_associated_links(hxs):
    links = hxs.select("//div[@id='picture']/descendant::div[@class='bloc-01']//a")

    all_tagged_urls = []

    if links:
        def extract_url_and_title(link_hxs):
            url = link_hxs.select('@href').extract()[0]
            title = u"".join(link_hxs.select("text()").extract())

            tags = set()
            if not title:
                title = u'No Title'
                tags.add('ghost link')
            if not url:
                url = u''
                tags.add('no target')
            return url, title, tags

        all_tagged_urls = list()
        for item in links:
            url, title, tags = extract_url_and_title(item)
            tags.update(classify_and_tag(url, SUDINFO_OWN_NETLOC, SUDINFO_INTERNAL_SITES))
            link_type = item.select('@class')
            if link_type and link_type[0] in LINK_TYPE_TO_TAG:
                tags.update(LINK_TYPE_TO_TAG[link_type])

            all_tagged_urls.append(make_tagged_url(url, title, tags))

    media_links = hxs.select("//div[@id='picture']/descendant::div[@class='bloc-01 pf_article']//a")

    if media_links:
        for i, item in enumerate(media_links):
            item_id = item.select("./@href").extract()
            url = item_id
            title = u"EMBEDDED MEDIA {0}".format(i)
            tags = set(['media', 'embedded'])
            all_tagged_urls.append(make_tagged_url(url, title, tags))

    return all_tagged_urls



def is_page_error_404(hxs):
    return hxs.select("//head/title/text()").extract() == '404'



def extract_article_data(source):
    """
    """

    if hasattr(source, 'read'):
        html_content = source.read()
    else:
        source = convert_utf8_url_to_ascii(source)
        try:
            html_content = fetch_html_content(source)
        except urllib2.HTTPError as err:
            if err.code == 404:
                return None, "<html><head><title>404</title></head><body></body></html>"
            else:
                raise err

    hxs = HtmlXPathSelector(text=html_content)

    if is_page_error_404(hxs):
        return None, html_content
    else:
        category = hxs.select("//p[starts-with(@class, 'fil_ariane')]/a//text()").extract()
        title = hxs.select("//div[@id='article']/h1/text()").extract()[0]
        pub_date, pub_time = extract_date(hxs)
        author = hxs.select("//p[@class='auteur']/text()").extract()[0]
        fetched_datetime = datetime.today()

        intro, intro_links = extract_intro_and_links(hxs)

        content, content_links = extract_content_and_links(hxs)

        associated_links = extract_associated_links(hxs)

        all_links = intro_links + content_links + associated_links


        return (ArticleData(source, title, pub_date, pub_time, fetched_datetime,
                            all_links,
                            category, author,
                            intro, content),
                html_content)





### frontpage stuff

def extract_title_url_from_hxs_a(link):
    title = ''.join(link.select(".//text()").extract())
    return title.strip(), link.select("@href").extract()[0]


def get_regional_toc():
    url = 'http://www.sudinfo.be/8/regions'
    html_content = fetch_content_from_url(url)

    hxs = HtmlXPathSelector(text=html_content)
    links = hxs.select("//div[@class='first-content w630-300']//div[@class='bloc_section']//li/a")
    return [extract_title_url_from_hxs_a(link) for link in links]


def make_full_url(prefix, titles_and_urls):
    return [(title, urllib.basejoin(prefix, url)) for title, url in titles_and_urls]



def separate_blogposts_and_news(titles_and_urls):
    def is_internal_url(url):
        scehem, netloc, path, params, query, fragments = urlparse(url)
        return not netloc
    news_items = [(t, u) for t, u in titles_and_urls if is_internal_url(u)]
    blogpost_items = [(t, u) for t, u in titles_and_urls if (t, u) not in news_items]
    return news_items, blogpost_items



def get_frontpage_toc():
    BASE_URL = u'http://www.sudinfo.be/'
    html_content = fetch_content_from_url(BASE_URL)
    hxs = HtmlXPathSelector(text=html_content)

    column1_headlines = hxs.select("//div[starts-with(@class, 'first-content')]//div[starts-with(@class, 'article')]//h2/a")
    column3_headlines =  hxs.select("//div[@class='octetFun']/a/child::h3/..")
    buzz_headlines = hxs.select("//div[@class='buzz exergue clearfix']//h2/a")
    buzz_headlines.extend(hxs.select("//div[@class='buzz exergue clearfix']//li/a"))

    all_link_selectors = chain(column1_headlines, column3_headlines, buzz_headlines)
    headlines = [extract_title_url_from_hxs_a(link_selector) for link_selector in all_link_selectors]

    regional_headlines = get_regional_toc()
    headlines.extend(regional_headlines)

    news, blogposts = separate_blogposts_and_news(headlines)
    return make_full_url(BASE_URL, news), blogposts



def show_frontpage_articles():
    toc, blogs = get_frontpage_toc()

    print len(toc)
    for i, (title, url) in enumerate(toc[:]):
        print ""
        print u"----- [{0:02d}] -- {1}".format(i, url)
        article_data, html_content = extract_article_data(url)
        article_data.print_summary()
        #print article_data.to_json()



def test_sample_data():
    filepath = '../../sample_data/sudpresse_some_error.html'
    filepath = '../../sample_data/sudpresse_associated_link_error.html'
    with open(filepath) as f:
        article_data, raw = extract_article_data(f)
        article_data.print_summary()

        for link in article_data.links:
            print link.title

        print article_data.intro
        print article_data.content


def show_article():
    urls = [
        u"http://www.sudinfo.be/336280/article/sports/foot-belge/anderlecht/2012-02-26/lierse-anderlecht-les-mauves-vont-ils-profiter-de-la-defaite-des-brugeois",
        u"http://www.sudinfo.be/335985/article/sports/foot-belge/charleroi/2012-02-26/la-d2-en-direct-charleroi-gagne-en-l-absence-d-abbas-bayat-eupen-est-accro",
        u"http://www.sudinfo.be/361378/article/sports/foot-belge/standard/2012-03-31/jose-riga-apres-la-defaite-du-standard-a-gand-3-0-nous-n%E2%80%99avons-pas-a-rougir",
        u"http://www.sudinfo.be/361028/article/fun/people/2012-03-31/jade-foret-et-arnaud-lagardere-bientot-parents",
        u"http://www.sudinfo.be/361506/article/fun/insolite/2012-04-01/suppression-du-jour-ferie-le-1er-mai-sarkozy-qui-s’installe-en-belgique-attentio",
        u"http://www.sudinfo.be/359805/article/culture/medias/2012-03-29/gopress-le-premier-kiosque-digital-belge-de-la-presse-ecrite-avec-sudpresse",
        u"http://www.sudinfo.be/346549/article/regions/liege/actualite/2012-03-12/debordements-au-carnaval-de-glons-un-bus-tec-arrete-et-40-arrestationss",
        u"http://www.sudinfo.be/522122/article/actualite/faits-divers/2012-09-15/victor-dutroux-michelle-martin-est-aussi-une-victime-de-marc-dont-je-ne-sais-pa",
        u"http://www.sudinfo.be/518865/article/actualite/belgique/2012-09-11/le-prince-laurent-n%E2%80%99est-pas-sur-qu%E2%80%99albert-est-reellement-son-pere-%E2%80%9D",
        u"http://www.sudinfo.be/522313/article/regions/mons/2012-09-15/mons-accuse-de-viols-en-serie-le-malgache-n’a-avoue-qu’un-seul-fait",
        u"http://www.sudinfo.be/522322/article/regions/mouscron/2012-09-15/comines-john-verfaillie-champion-de-belgique-de-rummikub",
        u"http://www.sudinfo.be/522139/article/regions/bruxelles/2012-09-15/victor-3-ans-s’echappe-de-son-ecole-et-se-retrouve-au-milieu-d’un-carrefour",
        u"http://www.sudinfo.be/335985/article/sports/foot-belge/charleroi/2012-02-26/la-d2-en-direct-charleroi-gagne-en-l-absence-d-abbas-bayat-eupen-est-accro",
        u"http://www.sudinfo.be/529977/article/sports/foot-belge/anderlecht/2012-09-18/ligue-des-champions-anderlecht-va-t-il-pouvoir-realiser-un-«truc»-dans-l’",
        u"http://www.sudinfo.be/534336/article/culture/medias/2012-09-25/eva-longoria-nue-tiffany-de-virgin-radio-va-le-faire-ainsi-que-toute-l’equipe-de",
        u"http://www.sudinfo.be/534573/article/sports/foot-belge/standard/2012-09-25/jelle-van-damme-standard-menace-benjamin-deceuninck-en-direct-fais-gaffe-av",
        u"http://www.sudinfo.be/534931/article/actualite/sante/2012-09-26/la-prescription-des-medicaments-bon-marche-continue-de-progresser",
        u"http://www.sudinfo.be/551998/article/fun/buzz/2012-10-04/des-nus-partout-dans-bruxelles-qui-miment-l-acte-sexuel-l’incroyable-performance",
        u"http://www.sudinfo.be/551998/article/fun/buzz/2012-10-04/schocking-in-brussles-des-hommes-nus-miment-l-acte-sexuel-au-palais-de-justice-a"

        ]

    for url in urls[:]:
        article_data, raw_html = extract_article_data(url)

        if article_data:
            article_data.print_summary()
            print article_data.intro
            print article_data.content

            for tagged_url in article_data.links:
                print u"{0} [{1}] {2!r}".format(*tagged_url)
        else:
            print 'no article found'





def show_frontpage_toc():
    headlines, blogposts = get_frontpage_toc()

    for title, url in headlines:
        print u"{0} \t\t\t\t\t [{1}]".format(title, url)



    for title, url in blogposts:
        print u"{0} \t\t\t\t\t [{1}]".format(title, url)

    print len(headlines), len(blogposts)

if __name__=='__main__':
    #show_frontpage_toc()
    #download_one_article()
    #show_frontpage_articles()

    url = "/Volumes/Curst/json_db_0_5/sudinfo/2012-06-05/14.05.07/raw_data/18.html"
    f = open(url,"r")

    article_data, content_html = extract_article_data(f)
    article_data.print_summary()