<commit_before>import datetime
from clfparser import CLFParser
import operator

# def tail(file, n=1, bs=1024):
#     f = open(file)
#     f.seek(0,2)
#     l = 1-f.read(1).count('\n')
#     B = f.tell()
#     while n >= l and B > 0:
#             block = min(bs, B)
#             B -= block
#             f.seek(B, 0)
#             l += f.read(block).count('\n')
#     f.seek(B, 0)
#     l = min(l,n)
#     lines = f.readlines()[-l:]
#     f.close()
#     return lines


def most_popular_pages(filename, cutoff_time):
    with open(filename) as f:
        content = f.readlines()
    records = [CLFParser.logDict(l) for l in content]
    kept = [r['r'] for r in records
            if r['h'] != '-' and
            r['r'][0:4] == '"GET'and
            isinstance(r['time'], datetime.date) and
            r['time'] > cutoff_time]
    urls = {}
    for record in kept:
        s = record.split(" ")[1]
        if s[-1] == "/" and s != "/sitenews/rss/":
            if s in urls:
                urls[s] = urls[s] + 1
            else:
                urls[s] = 1
    most_pop = sorted(urls.items(), key=operator.itemgetter(1), reverse=True)
    for url in most_pop[0:5]:
        print(url[0], url[1])


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Get most popular pages')
    parser.add_argument('--logfile', type=str, default="access.log")
    parser.add_argument('--date', type=str, default="")
    options = parser.parse_args()

    if options.date == "":
        cutoff_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
    else:
        cutoff_time = datetime.datetime.strptime(options.date,
                                                 '%Y-%m-%d %H:%M:%S')
    most_popular_pages(options.logfile, cutoff_time)<commit_msg>Implemented variable number of top urls.<commit_after>import datetime
from clfparser import CLFParser
import operator

# def tail(file, n=1, bs=1024):
#     f = open(file)
#     f.seek(0,2)
#     l = 1-f.read(1).count('\n')
#     B = f.tell()
#     while n >= l and B > 0:
#             block = min(bs, B)
#             B -= block
#             f.seek(B, 0)
#             l += f.read(block).count('\n')
#     f.seek(B, 0)
#     l = min(l,n)
#     lines = f.readlines()[-l:]
#     f.close()
#     return lines


def most_popular_pages(filename, cutoff_time, num):
    with open(filename) as f:
        content = f.readlines()
    records = [CLFParser.logDict(l) for l in content]
    kept = [r['r'] for r in records
            if r['h'] != '-' and
            r['r'][0:4] == '"GET'and
            isinstance(r['time'], datetime.date) and
            r['time'] > cutoff_time]
    urls = {}
    for record in kept:
        s = record.split(" ")[1]
        if s[-1] == "/" and s != "/sitenews/rss/":
            if s in urls:
                urls[s] = urls[s] + 1
            else:
                urls[s] = 1
    most_pop = sorted(urls.items(), key=operator.itemgetter(1), reverse=True)
    for url in most_pop[0:num]:
        print(url[0], url[1])


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Get most popular pages')
    parser.add_argument('--logfile', type=str, default="access.log")
    parser.add_argument('--date', type=str, default="")
    parser.add_argument('--diff', type=int, default=10)
    parser.add_argument('--num', type=int, default=5)
    options = parser.parse_args()

    if options.date == "":
        cutoff_time = datetime.datetime.now() - \
                      datetime.timedelta(minutes=options.diff)
    else:
        cutoff_time = datetime.datetime.strptime(options.date,
                                                 '%Y-%m-%d %H:%M:%S')
    most_popular_pages(options.logfile, cutoff_time, options.num)
{'fraction_non_alphanumeric': 0.16318986004753103, 'fraction_numerical': 0.013203063110641669, 'mean_word_length': 2.0771730300568643, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3352015', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1281, 'n_words': 286}