Source

csxj-crawler / csxj / db / provider.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
"""
Interface over the json database.

This is how a typical database looks like:

- provider1
  - stats.json
  - last_frontpage_list.json
  - YYYY-MM-DD/
    - cached_metainfo.json
    - HH.MM.SS/
        - articles.json
        - raw_data/
            - references.json
            - 0.html
            - 1.html
            - ...
        - reprocessed_YYYY-MM-DD_HH.MM.SS/
            - articles.json
            - raw_data/
                - references.json
                - 0.html
                - 1.html
                - ...
    - HH.MM.SS
    - HH.MM.SS
    - ...
  - YYYY-MM-DD
  - YYYY-MM-DD
  - ...
  - queue
    -

This helper module enables programmatic access to this hierarchy. 
"""

import os
from datetime import datetime, timedelta
from collections import namedtuple, defaultdict
import json
import shutil

import utils
from article import ArticleData
from csxj.common.decorators import deprecated
from constants import *




ErrorLogEntry = namedtuple('ErrorLogEntry', 'url filename stacktrace')
ErrorLogEntry2 = namedtuple('ErrorLogEntry2', 'url title stacktrace')

def make_error_log_entry(url, stacktrace, outdir):
    """
    """
    return ErrorLogEntry(url, outdir, stacktrace)


def make_error_log_entry2(url, title, stacktrace):
    """
    """
    return ErrorLogEntry2(url, title, stacktrace)


class NonExistentDayError(Exception):
    def __init__(self, provider_name, date_string):
        super(NonExistentDayError, self).__init__()
        self.provider_name = provider_name
        self.date_string = date_string


class NonExistentBatchError(Exception):
    def __init__(self, provider_name, date_string, batch_time_string):
        super(NonExistentBatchError, self).__init__()
        self.provider_name = provider_name
        self.date_string = date_string
        self.batch_time_string = batch_time_string



class Provider(object):
    """
    Programmatic interface to access the data stored for one content provider.
    """
    def __init__(self, db_root, provider_name):
        self.db_root = db_root
        self.name = provider_name


    @property
    def directory(self):
        return os.path.join(self.db_root, self.name)


    def get_last_frontpage_items(self):
        """
        Returns a list of (title, url) for the items fetched on the last query.
        """
        provider_dir = self.directory
        filename = os.path.join(provider_dir, LAST_STORIES_FILENAME)
        if os.path.exists(filename):
            with open(filename, 'r') as f:
                last_stories_fetched = [tuple(i) for i in  json.load(f)]
                return last_stories_fetched
        else:
            return list()


    def get_source_summary_for_all_days(self):
        """
        Returns a list of (date, article_count, error_count). The date is a string (formatted as: YYYY-MM-DD),
        and counters are integers.

        The list is sorted on the date (earlier date at the front)
        """

        all_days = [d for d in utils.get_subdirectories(self.directory) if d != QUEUE_DIR]
        all_days.sort()
        result = list()
        for date_string in all_days:
            metainfos = self.get_cached_metainfos_for_day(date_string)
            result.append((date_string, metainfos))
        return result


    def get_all_days(self):
        """
        Returns a sorted list of all the dates (formatted as: YYYY-MM-DD) for which there is data available
        """
        all_days = [d for d in utils.get_subdirectories(self.directory) if d != QUEUE_DIR]
        all_days.sort()
        return all_days


    def get_all_batch_hours(self, date_string):
        """
        For a certain date (YYYY-MM-DD string), returns a list of hours (as HH.MM.SS strings) for which we have data available.
        """
        path = os.path.join(self.directory, date_string)
        if os.path.exists(path):
            all_batches = utils.get_subdirectories(path)
            all_batches.sort()
            return all_batches
        else:
            raise NonExistentDayError(self.name, date_string)



    def get_batch_articles(self, date_string, batch_time_string):
        """
        Returns the articles saved for a specific first batch.
        This function does not return the articles which might have been reprocessed
        after a (manual) error handling session. You should use the
        get_reprocessed_articles() function for that.

        The function return a sorted list of ArticleData instances.
        The list is sorted using the the article url as key.
        """
        batch_dir = os.path.join(self.directory, date_string, batch_time_string)
        if os.path.exists(batch_dir):
            json_filepath = os.path.join(batch_dir, ARTICLES_FILENAME)
            with open(json_filepath, 'r') as f:
                json_content = json.load(f)
                articles = [ArticleData.from_json(json_string) for json_string in json_content['articles']]
                articles.sort(key=lambda art: art.url)
                return articles
        else:
            raise NonExistentBatchError(self.name, date_string, batch_time_string)


    def get_pending_batch_errors(self, date_string, batch_time_string):
        """
        Returns the pending errors for a specific batch.

        Returns a list of ErrorLogEntry2 instances.
        This list is not sorted.
        """
        batch_dir = os.path.join(self.directory, date_string, batch_time_string)
        if os.path.exists(batch_dir):
            all_errors = list()

            def extract_errors_from_file(ErrorKls, filename):
                with open(filename, 'r') as f:
                    json_content = json.load(f)
                    return [ErrorKls(*error_data) for error_data in json_content['errors']]

            json_filepath = os.path.join(batch_dir, ERRORS_FILENAME)
            if os.path.exists(json_filepath):
                all_errors.append(extract_errors_from_file(ErrorLogEntry, json_filepath))
            else:
                all_errors.append(list())


            json_filepath = os.path.join(batch_dir, ERRORS2_FILENAME)
            if os.path.exists(json_filepath):
                all_errors.append(extract_errors_from_file(ErrorLogEntry2, json_filepath))
            else:
                all_errors.append(list())

            return all_errors
        else:
            raise NonExistentBatchError(self.name, date_string, batch_time_string)



    def has_reprocessed_content(self, date_string, batch_time_string):
        batch_dir = os.path.join(self.directory, date_string, batch_time_string)
        if os.path.exists(batch_dir):
            return len([i for i in utils.get_subdirectories(batch_dir) if i.startswith(REPROCESSED_DIR_PREFIX)]) > 0
        else:
            raise NonExistentBatchError(self.name, date_string, batch_time_string)


    def get_reprocessed_batch_articles(self, date_string, batch_time_string):
        """
        Returns articles fetched during an error handling session.

        ((date_string, hour_string), articles)
        """
        batch_dir = os.path.join(self.directory, date_string, batch_time_string)
        if os.path.exists(batch_dir):
            reprocessed_articles = list()
            for reprocessed_data_dir in [i for i in utils.get_subdirectories(batch_dir) if i.startswith(REPROCESSED_DIR_PREFIX)]:
                reprocessed_date, reprocessed_time = reprocessed_data_dir.split("_")[1:]

                json_filepath = os.path.join(batch_dir, reprocessed_data_dir, ARTICLES_FILENAME)
                with open(json_filepath, 'r') as f:
                    json_content = json.load(f)
                    articles = [ArticleData.from_json(json_string) for json_string in json_content['articles']]
                    articles.sort(key=lambda art: art.url)
                    reprocessed_articles.append(((reprocessed_date, reprocessed_time), articles))
            return reprocessed_articles
        else:
            raise NonExistentBatchError(self.name, date_string, batch_time_string)


    def get_batch_metainfos(self, date_string, batch_time_string):
        """
        Returns a dictionnary with the metainfos associated to the
        given batch.

        Current metainfos are:
            - article_count
            - link_count
            - error_count (not reprocessed yet)
            - total_error_count (after reprocessing)
            - reprocessed_article_count
            _ reprocessed_link_count

        These infos are calculated on the fly after loading the database json files
        """

        def compute_article_and_link_count(articles):
            return len(articles), sum(len(art.links) for art in articles)

        articles = self.get_batch_articles(date_string, batch_time_string)
        article_count, link_count = compute_article_and_link_count(articles)
        errors = self.get_pending_batch_errors(date_string, batch_time_string)
        pending_error_count = len(errors[0]) + len(errors[1])

        reprocessed_articles = self.get_reprocessed_batch_articles(date_string, batch_time_string)
        reprocessed_article_count, reprocessed_link_count = 0, 0
        for (batch_time, reprocessed_articles) in reprocessed_articles:
            article_count, link_count = compute_article_and_link_count(reprocessed_articles)
            reprocessed_article_count += article_count
            reprocessed_link_count += link_count

        total_error_count = pending_error_count + reprocessed_article_count

        metainfos = {
            'article_count':article_count,
            'link_count':link_count,
            'pending_error_count':pending_error_count,
            'total_error_count':total_error_count,
            'reprocessed_article_count':reprocessed_article_count,
            'reprocessed_link_count':reprocessed_link_count
        }

        return metainfos



    def get_cached_metainfos_for_day(self, date_string):
        """
        Returns a dictionary with some cached statistics
        about the content for a particular day.
        """
        day_directory = os.path.join(self.directory, date_string)
        if os.path.exists(day_directory):
            cached_metainfo_file = os.path.join(day_directory, METAINFO_FILENAME)
            if os.path.exists(cached_metainfo_file):
                with open(cached_metainfo_file) as f:
                    cached_metainfo = json.load(f)
                    return cached_metainfo
            else:
                metainfos = defaultdict(int)
                for batch_time_string in self.get_all_batch_hours(date_string):
                    batch_metainfos = self.get_batch_metainfos(date_string, batch_time_string)
                    for k, v in batch_metainfos.items():
                        metainfos[k] += v

                cached_metainfo_file = os.path.join(day_directory, METAINFO_FILENAME)
                with open(cached_metainfo_file, 'w') as f:
                    json.dump(metainfos, f)
                return metainfos
        else:
            raise NonExistentDayError(self.name, date_string)



    def get_cached_metainfos(self):
        """
        Summizes the overall values of the cached metainfo files.

        Returns a dictionnary with metainfo names and their value.
        """
        source_metainfos = defaultdict(int)

        for day in self.get_all_days():
            day_metainfos = self.get_cached_metainfos_for_day(day)
            for k, v in day_metainfos.items():
                source_metainfos[k] += v

        return source_metainfos


    def remove_all_cached_metainfo(self):
        for date_string in self.get_all_days():
            day_directory = os.path.join(self.directory, date_string)
            cached_metainfo_file = os.path.join(day_directory, METAINFO_FILENAME)
            if os.path.exists(cached_metainfo_file):
                os.remove(cached_metainfo_file)


    ### convenience functions: group things per batch
    def get_articles_per_batch(self, date_string):
        """
        Returns a list of (time, [Articles]).
        """
        day_directory = os.path.join(self.directory, date_string)
        if os.path.exists(day_directory):
            all_batch_times = utils.get_subdirectories(day_directory)
            all_batches = []
            for batch_time in all_batch_times:
                articles = self.get_batch_articles(date_string, batch_time)
                all_batches.append((batch_time, articles))
                
            all_batches.sort(key=lambda x: x[0])
            return all_batches
        else:
            raise NonExistentDayError(self.name, date_string)


    def get_errors2_per_batch(self, date_string):
        """
        Returns a list of (time, [errors]).
        """
        day_directory = os.path.join(self.directory, date_string)
        if os.path.exists(day_directory):
            all_batch_times = utils.get_subdirectories(day_directory)
            all_errors = []
            for batch_time in all_batch_times:
                errors = self.get_pending_batch_errors(date_string, batch_time)
                all_errors.append((batch_time, errors))
            all_errors.sort(key=lambda x: x[0])
            return all_errors

        else:
            raise NonExistentDayError(self.name, date_string)


    def get_data_per_batch(self, date_string, data_extractor_func):
        day_directory = os.path.join(self.directory, date_string)
        if os.path.exists(day_directory):
            all_batch_times = utils.get_subdirectories(day_directory)
            all_data = []
            for batch_time in all_batch_times:
                extracted_data = data_extractor_func(self, date_string, batch_time)
                all_data.append(extracted_data)
            all_data.sort(key=lambda x: x[0])
            return all_data

        else:
            raise NonExistentDayError(self.name, date_string)




    def get_deleted_articles_from_batch_directory(self, batch_directory):
        deleted_articles_filename = os.path.join(batch_directory, DELETED_ARTICLES_FILENAME)
        if os.path.exists(deleted_articles_filename):
            with open(deleted_articles_filename, 'r') as f:
                return json.load(f)
        else:
            return {"deleted_articles":[]}


    def get_blogpost_titles_from_batch(self,  day_string, hour_string):
        blogpost_filename = os.path.join(self.directory, day_string, hour_string, BLOGPOSTS_FILENAME)
        if os.path.exists(blogpost_filename):
            with open(blogpost_filename) as f:
                return json.load(f)
        else:
            return {"blogposts":[]}


    ### Queue management
    def cleanup_queue(self, day_string):
        """
        Removes the queued items for a certain date. Should be called only after
        a successfull crawling session.
        """
        day_queue_directory = os.path.join(self.directory, QUEUE_DIR, day_string)
        if os.path.exists(day_queue_directory):
            shutil.rmtree(day_queue_directory)

            

    def get_queued_batches_by_day(self):
        """
        Each datasource directory contains a 'queue' directory in which items' urls
        are stored for delayed download.

        Under the 'queue' directory,
        """
        queue_directory = os.path.join(self.directory, QUEUE_DIR)
        batched_days = utils.get_subdirectories(queue_directory)
        batches_by_day = list()
        for day_string in batched_days:
            day_directory = os.path.join(queue_directory, day_string)
            batches_by_day.append((day_string, self.get_queued_items_by_batch(day_directory)))

        batches_by_day.sort(key=lambda day_batches: day_batches[0])
        return batches_by_day



    def get_queued_items_by_batch(self, day_directory):
        """
        Queued items for a day are stored in json files, one for every batch.
        The hierarchy looks like:

         - 2011-26-11/
            - ...
            - 21.00.00.json
            - 22.00.00.json
            - ...

        Every file contains two lists of (title, url) pairs: one for the actual
        news stories, and one for the occasionally promoted blogposts.
        """
        items_by_batch = list()
        for batch_file in utils.get_json_files(day_directory):
            batch_hour = batch_file[:-5]
            with open(os.path.join(day_directory, batch_file)) as f:
                items = json.load(f)
                items_by_batch.append((batch_hour, items))
        items_by_batch.sort(key=lambda batch: batch[0])
        return items_by_batch



    def get_queued_items_count(self):
        queue_directory = os.path.join(self.directory, QUEUE_DIR)
        batched_days = utils.get_subdirectories(queue_directory)
        item_count = 0

        for day_string in batched_days:
            day_directory = os.path.join(queue_directory, day_string)
            item_count += self.get_item_count_for_day(day_directory)

        return item_count


    def get_queue_error_count(self):
        """
        """
        queue_error_log = self.get_queue_errors()
        return sum([len(logged_stacktraces.items()) for logged_stacktraces in queue_error_log.values()])


    def get_queue_error_count_for_last_days(self, day_count):
        """
        """
        today = datetime.now()
        def is_recent_enough(error_date_string):
            error_date = datetime.strptime(error_date_string, "%Y-%m-%d")
            return error_date > (today - timedelta(days=day_count))

        queue_error_log = self.get_queue_errors()
        recent_queue_errors = [(d, errors) for (d, errors) in queue_error_log.iteritems() if is_recent_enough(d)]
        return sum([len(logged_stacktraces.items()) for (d, logged_stacktraces) in recent_queue_errors])


    def get_queue_errors(self):
        error_log_file = os.path.join(self.directory, QUEUE_ERROR_LOG_FILENAME)

        queue_error_log = dict()
        if(os.path.exists(error_log_file)):
            f = open(error_log_file, 'r')
            queue_error_log = json.load(f)
            f.close()

        return queue_error_log



    #OLD STUFF TO BE REMOVED SOME DAY:
    @deprecated
    def get_article_and_error_count(self, date_string):
        """
        Returns the number of articles fetched and errors encountered for all the batches in a day.
        """
        article_count, error_count = 0, 0
        for hour_string in self.get_all_batch_hours(date_string):
            articles, batch_error_count = self.get_batch_content(date_string, hour_string)
            article_count += len(articles)
            error_count += batch_error_count
        return article_count, error_count


    @deprecated
    def get_item_count_for_day(self, day_directory):
        item_count = 0
        for batch_file in utils.get_json_files(day_directory):
            batch_hour = batch_file[:-5]
            with open(os.path.join(day_directory, batch_file)) as f:
                items = json.load(f)
                item_count += len(items['articles'])
        return item_count


    @deprecated
    def get_errors_per_batch(self, date_string):
        """
        Returns a list of (time, [errors]).
        """
        day_directory = os.path.join(self.directory, date_string)
        if os.path.exists(day_directory):
            all_batch_times = utils.get_subdirectories(day_directory)
            all_errors = []
            for batch_time in all_batch_times:
                errors = self.get_errors_from_batch(date_string, batch_time)
                all_errors.append((batch_time, errors))
            all_errors.sort(key=lambda x: x[0])
            return all_errors

        else:
            raise NonExistentDayError(self.name, date_string)


    @deprecated
    def get_articles_and_errorcounts_per_batch(self, date_string):
        """
        Returns a list of (hour_string, [Articles], error_count) for a certain date
        """
        day_directory = os.path.join(self.directory, date_string)
        if os.path.exists(day_directory):
            all_batch_times = utils.get_subdirectories(day_directory)
            all_batches = []
            for batch_time in all_batch_times:
                batch_content = self.get_batch_content(date_string, batch_time)
                articles, batch_error_count = batch_content
                all_batches.append((batch_time, articles, batch_error_count))

            all_batches.sort(key=lambda x: x[0])
            return all_batches
        else:
            raise NonExistentDayError(self.name, date_string)



    @deprecated
    def get_batch_content(self, date_string, batch_time_string):
        """
        Returns the data saved for a specific batch
        """
        batch_dir = os.path.join(self.directory, date_string, batch_time_string)
        if os.path.exists(batch_dir):
            json_filepath = os.path.join(batch_dir, ARTICLES_FILENAME)
            with open(json_filepath, 'r') as f:
                json_content = json.load(f)
                articles = [ArticleData.from_json(json_string) for json_string in json_content['articles']]
                articles.sort(key=lambda art: art.url)
                n_errors = len(json_content['errors'])
                return articles, n_errors
        else:
            raise NonExistentBatchError(self.name, date_string, batch_time_string)


    @deprecated
    def get_errors_from_batch(self, date_string, batch_time_string):
        batch_dir = os.path.join(self.directory, date_string, batch_time_string)
        if os.path.exists(batch_dir):
            json_filepath = os.path.join(batch_dir, ERRORS_FILENAME)
            if os.path.exists(json_filepath):
                with open(json_filepath, 'r') as f:
                    json_content = json.load(f)
                    return [ErrorLogEntry(*error_data) for error_data in json_content['errors']]
            else:
                return None
        else:
            raise NonExistentBatchError(self.name, date_string, batch_time_string)



if __name__=="__main__":
    db_root = "/Users/sevas/TEST_DB"
    p = Provider(db_root, "rtbfinfo")

    print p.get_queue_error_count_for_last_days(3)