Exemple #1
0
def task_2():
    """task-1 で保存したスレッドからタスク待ちオブジェクトを作成する"""
    task = datastore.get_next_task()
    if task is None:
        logging.info('task not found.')
        return request.url
    dat = i2ch.download_html(task.dat_url)
    if dat is None:
        logging.error('download failed. URL:' + task.dat_url)
        return request.url

    # スレッド情報を取得し、チェック済み番号を取得
    thread_data = datastore.get_thread(task.id)
    checked_response = 0
    if thread_data is not None:
        checked_response = thread_data.response_num
    
    unicodedat = unicode(dat, 'shift-jis', 'ignore')
    counter = Counter(unicodedat)
    users = i2ch.get_user_list_from_dat(unicodedat)
    
    entries = []
    response_num = 0
    for user in users:
        response_num = int(user['response']['number'])
        # まだ未チェックのレス番までスキップする
        if response_num < checked_response:
            continue
        entries.append({
            'number': response_num,
            'author': user['response']['name'],
            'mail': user['response']['mail'],
            'body': user['response']['body'],
            'authorid': user['response']['id'],
            'thread': task.id,
            'at': user['response']['datetime'].strftime('%Y/%m/%d %H:%M:%S.%f'),
            'ids': user['ids']
        })
    thread_title = unicodedat.splitlines()[0].split('<>')[4] # 乱暴、データ形式が変わるとこける
    thread_title = thread_title.replace('&amp;', '&');
    datastore.add_entry_task(entries)
    datastore.update_thread(task.id, {
        'url': task.url,
        'response_num': response_num,
        'title': thread_title
    })
    return request.url
Exemple #2
0
def task_3():
    """task-2 で保存した情報を展開して書き込む"""
    # 一回で処理するタスク数
    PROC_TASK_NUM = 20
    
    entries = datastore.get_entry_task()
    thread_dic = {}
    ids_dic = {}
    task_count = 0
    while entries:
        entry = entries.pop(0)
        
        thread = None
        if entry['thread'] in thread_dic:
            thread = thread_dic[entry['thread']]
        else:
            thread = datastore.get_thread(entry['thread'])
            thread_dic[entry['thread']] = thread
        
        response_data = datastore.create_response({
            'number': entry['number'],
            'author': entry['author'],
            'mail': entry['mail'],
            'body': entry['body'],
            'authorid': entry['authorid'],
            'thread': thread,
            'at': datetime.strptime(entry['at'], '%Y/%m/%d %H:%M:%S.%f')
        })
        for id in entry['ids']:
            # このループで新規に追加された ID の場合、
            # データベースに保存される前に取得が発生するようで、
            # 結果的に同じ ID が複数データベースに書き込まれる現象が発生する
            # (根本的な解決ではないが)これを避けるため、いったん ID を保存し一括で保存するようにする
            #datastore.update_psn_user(id, response_data.key())
            if id not in ids_dic:
                ids_dic[id] = []
            ids_dic[id].append(response_data.key())
        task_count += 1
        if task_count > PROC_TASK_NUM:
            break
    for id, keys in ids_dic.iteritems():
        datastore.increment_user_count(id, keys)
    datastore.set_entry_task(entries)
    return request.url