<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
database deals with saving, updating and deleting rss items to/from database
"""
# @author chengdujin
# @contact <EMAIL>
# @created Jul., 2013


import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
sys.path.append('..')

from config import Collection
from config import db

# CONSTANTS
from config import LANGUAGES


def dedup(entries=None, language=None):
    """
    return entries not found in database
    """
    if not entries:
        return None
    if not language or language not in LANGUAGES:
        raise Exception("ERROR: language not found or not supported!")

    entries_new = []
    col = Collection(db, language)
    for entry in entries:
        # find duplicate in the form of the same link or title
        dup_link = col.find_one({'link': entry['link']})
        dup_title = col.find_one({'title': entry['title']})
        if dup_link or dup_title:
            print 'Find a duplicate for %s' % str(entry['title'])
            continue
        else:
            entries_new.append(entry)
    return entries_new if entries_new else None


# TODO: break update_database into several shorter mthods
def update(entries=None, language=None):
    """
    save entries
    """
    if not entries:
        return None
    if not language or language not in LANGUAGES:
        raise Exception("ERROR: language not found or not supported!")

    # collection was created by the feed
    col = Collection(db, language)
    for entry in entries:
        # then save to database
        entry_id = col.save(entry)
        entry['_id'] = str(entry_id)
    return entries<commit_msg>changed database.update into method that only acts on single entry<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
database deals with saving, updating and deleting rss items to/from database
"""
# @author chengdujin
# @contact <EMAIL>
# @created Jul., 2013


import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
sys.path.append('..')

from config import Collection
from config import db

# CONSTANTS
from config import LANGUAGES


def dedup(entries=None, language=None):
    """
    return entries not found in database
    """
    if not entries:
        return None
    if not language or language not in LANGUAGES:
        raise Exception("ERROR: language not found or not supported!")

    entries_new = []
    col = Collection(db, language)
    for entry in entries:
        # find duplicate in the form of the same link or title
        dup_link = col.find_one({'link': entry['link']})
        dup_title = col.find_one({'title': entry['title']})
        if dup_link or dup_title:
            print 'Find a duplicate for %s' % str(entry['title'])
            continue
        else:
            entries_new.append(entry)
    return entries_new if entries_new else None


def update(entry=None, language=None):
    """
    save an entry in database
    """
    if not entry:
        return None
    if not language or language not in LANGUAGES:
        raise Exception("ERROR: language not found or not supported!")

    # collection was created by the feed
    col = Collection(db, language)
    # then save to database
    entry_id = col.save(entry)
    entry['_id'] = str(entry_id)
    return entry
{'fraction_non_alphanumeric': 0.09817073170731708, 'fraction_numerical': 0.003658536585365854, 'mean_word_length': 2.8782505910165486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '3784400', 'n_tokens_mistral': 959, 'n_tokens_neox': 927, 'n_words': 342}