import couchdb, os, logging, sys

# run in heroku with:
# heroku run python extras/couch_purge.py

logging.basicConfig(
    stream=sys.stdout,
    level=logging.DEBUG,
    format='%(levelname)8s %(name)s - %(message)s'
)

logger = logging.getLogger("couch_purge")

couch = couchdb.Server(url=os.getenv("CLOUDANT_URL"))
db = couch[os.getenv("CLOUDANT_DB")]
logger.info("connected to couch/" + os.getenv("CLOUDANT_DB"))

view_name = "admin/old-wikipedia-snaps"

for row in db.view(view_name, include_docs=True):

    doc = row.doc
    logger.info("got doc '{id}' back from {view_name}, with drilldown_url {url}".format(
        id=row.id,
        view_name=view_name,
        url=doc["drilldown_url"]

    ))
    logger.info("deleting doc '{id}'.".format(
        id=row.id
    ))
    db.delete(doc)

logger.info("finished the update.")





@@ -412,16 +412,96 @@
 _DB%22))%0A%0A
+#wikipedia_snap_cleanup()%0A#bad_doi_cleanup()%0A%0Adef wikipedia_snap_cleanup():%0A    
 view_nam
@@ -533,16 +533,20 @@
 snaps%22%0A%0A
+    
 for row 
@@ -588,16 +588,20 @@
 True):%0A%0A
+    
     doc 
@@ -610,16 +610,20 @@
 row.doc%0A
+    
     logg
@@ -703,32 +703,36 @@
 format(%0A        
+    
 id=row.id,%0A     
@@ -730,24 +730,28 @@
 id,%0A        
+  
+  
 view_name=vi
@@ -759,16 +759,20 @@
 w_name,%0A
+    
         
@@ -801,23 +801,31 @@
 %22%5D%0A%0A    
-))%0A
+    ))%0A   
     
+ 
 logger.i
@@ -859,32 +859,36 @@
 format(%0A        
+    
 id=row.id%0A    ))
@@ -881,27 +881,35 @@
 =row.id%0A    
+   
+ 
 ))%0A
+    
     db.delet
@@ -916,16 +916,20 @@
 e(doc)%0A%0A
+    
 logger.i
@@ -958,10 +958,766 @@
 te.%22)%0A%0A%0A
+def bad_doi_cleanup():%0A    view_name = %22admin/bad_dois%22%0A%0A    for row in db.view(view_name, include_docs=True):%0A%0A        doc = row.doc%0A        logger.info(%22got doc '%7Bid%7D' back from %7Bview_name%7D%22.format(%0A            id=row.id,%0A            view_name=view_name%0A        ))%0A%0A        logger.info(%22doc '%7Bid%7D' has these doi aliases: %7Baliases%7D%22.format(%0A            id=row.id,%0A            aliases=doc%5B%22aliases%22%5D%5B%22doi%22%5D%0A        ))%0A%0A        good_dois = %5Bx for x in doc%5B%22aliases%22%5D%5B%22doi%22%5D if %22 %5B%5D%22 not in x%5D%0A        doc%5B%22aliases%22%5D%5B%22doi%22%5D = good_dois%0A%0A        logger.info(%22saving doc '%7Bid%7D', which now has these aliases: '%7Baliases%7D'%22.format(%0A            id=row.id,%0A            aliases=doc%5B%22aliases%22%5D%0A        ))%0A        db.save(doc)%0A%0A    logger.info(%22finished the update.%22)%0A
 %0A%0A

add back older couch maint code into HEAD file