Commits

Cédric Bonhomme committed 2f4a372

added add_to_index function, to add an article to the Whoosh index.

Comments (0)

Files changed (2)

 from contextlib import contextmanager
 
 import conf
+import search
 import utils
 import mongodb
 
 
             articles.append(article)
 
+            # add the article to the Whoosh index
+            #search.add_to_index([article], feed)
+
             if conf.MAIL_ENABLED and feed["mail"] and self.articles.get_articles(feed_id, article_id) == False:
                 # if subscribed to the feed AND if article not already in the database
                 threading.Thread(None, utils.send_mail, None, (conf.mail_from, conf.mail_to, \
                                 feed_id=feed["feed_id"])
     writer.commit()
 
+def add_to_index(articles, feed):
+    """
+    Add a list of articles to the index.
+    """
+    try:
+        ix = open_dir(indexdir)
+    except (EmptyIndexError, OSError) as e:
+        raise EmptyIndexError
+    writer = ix.writer()
+    for article in articles:
+        writer.add_document(title=article["article_title"], \
+                            content=utils.clear_string(article["article_content"]), \
+                            article_id=article["article_id"] , \
+                            feed_id=feed["feed_id"])
+    writer.commit()
+
 def search(term):
     """
     Search for `term` in the index.
     #create_index()
     results = search("Nothomb")
     for article in results:
-        print(article)
+        print(article)