<commit_before>#!/usr/bin/python

import sys, os, codecs, glob
import os.path, time, calendar

infolder = os.path.realpath(sys.argv[1])
# inpath = os.path.realpath(sys.argv[1])

## Desired URLs are something like
## ^http[s]?://[^/]*agorahooawayyfoe.onion/p\.*

## our paths look like /dnmarchives/agora/2015-07-07/cat/XpMZAakr4w
## out paths look like /dnmarchives/agora/2015-07-07/p/XpMZAakr4w
## we will transform this to https://agorahooawayyfoe.onion/p/XpMZAakr4w

def inpathToUrl(path):
    root = "https://agorahooawayyfoe.onion/p/"
    return os.path.join(root, os.path.basename(path))

def inpathToEpoch(path, fromPathname=True):
    if fromPathname:
        # Use filename
        # /dnmarchives/agora/2015-07-07/p/XpMZAakr4w
        # => 2015-07-07
        tm = time.strptime(os.path.dirname(path).split(os.sep)[-2], "%Y-%m-%d")
        return int(1000 * time.mktime(tm))
    else:
        # Use file write date
        print os.path.getmtime(path)
        return 1000*calendar.timegm(time.gmtime(os.path.getmtime(path)))

def inpathToBody(path):
    url = inpathToUrl(path)
    epoch = inpathToEpoch(path)
    with codecs.open(path, 'r', encoding='utf-8') as fi:
        return {"_source": {"raw_content" : fi.read(),
                            "url": url,
                            "timestamp" : epoch}}

def infolderToOutpath(path):
    return os.path.join(path, "contents.cdr")

if __name__ == '__main__':
    outpath = infolderToOutpath(infolder)
    with codecs.open(outpath, 'w', encoding='utf-8') as fo:
        for inpath in glob.glob(os.path.join(infolder, "[0-9a-z]*")):
            url = inpathToUrl(inpath)
            body = inpathToBody(inpath)
            print >> fo, url
            print >> fo, "\t",
            print >> fo, body<commit_msg>for agora<commit_after>#!/usr/bin/python

import sys, os, codecs, glob
import os.path, time, calendar
import json

infolder = os.path.realpath(sys.argv[1])
# inpath = os.path.realpath(sys.argv[1])

## Desired URLs are something like
## ^http[s]?://[^/]*agorahooawayyfoe.onion/p\.*

## our paths look like /dnmarchives/agora/2015-07-07/cat/XpMZAakr4w
## out paths look like /dnmarchives/agora/2015-07-07/p/XpMZAakr4w
## we will transform this to https://agorahooawayyfoe.onion/p/XpMZAakr4w

def inpathToUrl(path):
    root = "https://agorahooawayyfoe.onion/p/"
    return os.path.join(root, os.path.basename(path))

def inpathToEpoch(path, fromPathname=True):
    if fromPathname:
        # Use filename
        # /dnmarchives/agora/2015-07-07/p/XpMZAakr4w
        # => 2015-07-07
        tm = time.strptime(os.path.dirname(path).split(os.sep)[-2], "%Y-%m-%d")
        return int(1000 * time.mktime(tm))
    else:
        # Use file write date
        print os.path.getmtime(path)
        return 1000*calendar.timegm(time.gmtime(os.path.getmtime(path)))

def inpathToBody(path):
    url = inpathToUrl(path)
    epoch = inpathToEpoch(path)
    with codecs.open(path, 'r', encoding='utf-8') as fi:
        return {"_source": {"raw_content" : fi.read(),
                            "url": url,
                            "timestamp" : epoch}}

def infolderToOutpath(path):
    return os.path.join(path, "contents.cdr")

if __name__ == '__main__':
    outpath = infolderToOutpath(infolder)
    with codecs.open(outpath, 'w', encoding='utf-8') as fo:
        limit = 25000
        for inpath in glob.glob(os.path.join(infolder, "[0-9a-zA-Z]*")):
            url = inpathToUrl(inpath)
            body = inpathToBody(inpath)
            print >> fo, url,
            print >> fo, "\t",
            print >> fo, json.dumps(body)
            limit += -1
            if limit <= 0:
                break

{'fraction_non_alphanumeric': 0.15204359673024523, 'fraction_numerical': 0.029700272479564034, 'mean_word_length': 2.6096361848574237, 'pattern_counts': {'":': 4, '<': 4, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1427247', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1347, 'n_words': 252}