aboutsummaryrefslogtreecommitdiff
path: root/mongodb.py
blob: 851db9453ab5c937cec201cc34a7a525f96c86ea (plain)
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2012/03/03 $"
__date__ = "$Date: 2012/03/03 $"
__copyright__ = "Copyright (c) Cedric Bonhomme"
__license__ = "GPLv3"

import time

from pymongo.connection import Connection

class Articles(object):
    """
    """
    def __init__(self, url='localhost', port=27017):
        """
        Instantiates the connection.
        """
        self.connection = Connection(url, port)
        self.db = self.connection.pyaggr3g470r

    def add_collection(self, collection):
        """
        Creates a new collection for a new feed.
        """
        collection =  self.db.collection.feed_id
        collection.insert(collection)

    def add_article(self, article, collection_id):
        """
        Add an article in a collection.
        """
        collection = self.db.collection_id
        cursor = collection.find({"article_id":article.article_id})
        if cursor.count() == 0:
            collection.insert(user_dic)

    def get_all_articles():
        """
        Return all articles from all collections.
        """
        articles = []
        collections = self.db.collection_names
        for colliection in collections:
            collection = self.db.collection_id
            articles.append(collection)
        return articles

    def get_articles_from_collection():
        """
        Return all the articles of a collection.
        """
        collection = self.db.collection_id
        return collection
        
        
        
    #
    # Collection: users
    #
    def register_user(self, sender_uuid, user):
        """
        Insert a new user in the collection of users.
        """
        user_dic = {"uuid":sender_uuid, "name":user, \
                    "time-registration":time.time()}

        collection = self.db.users
        cursor = collection.find({"uuid":sender_uuid})
        if cursor.count() == 0:
            collection.insert(user_dic)


    def print_users(self):
        """
        List and print the users.
        """
        collection = self.db.users
        cursor = collection.find()
        for d in cursor:
            print d

    def nb_users(self):
        """
        Return the number of users.
        """
        collection = self.db.users
        collection.count()


    # Functions on database
    def drop_wsc_database(self):
        self.connection.drop_database('pyaggr3g470r')


if __name__ == "__main__":
    # Point of entry in execution mode.
    articles_database = Articles()    
bgstack15