add the intiial bit.ly extension so we can start playing with the results
authorBryan Clark <clarkbw@gnome.org>
Wed, 06 Jan 2010 15:15:43 -0800
changeset 1216 4ec54fd894d1c906b920262a6b957be733e36eed
parent 1215 4d5d40e761d4e2581241df3d4fe6e57c5e12b80c
child 1217 2af2c34bbc4b43eb279985e48c6e78d8bb8ccde3
push id605
push userclarkbw@gnome.org
push dateWed, 06 Jan 2010 23:21:34 +0000
add the intiial bit.ly extension so we can start playing with the results
couch_docs/wq/rd.ext.core.msg-body-quoted-to-bitly.json
couch_docs/wq/rd.ext.core.msg-body-quoted-to-bitly.py
new file mode 100644
--- /dev/null
+++ b/couch_docs/wq/rd.ext.core.msg-body-quoted-to-bitly.json
@@ -0,0 +1,10 @@
+{
+  "schemas" : {
+    "rd.ext.workqueue" : {
+        "source_schemas" : ["rd.msg.body.quoted.hyperlinks"],
+        "code" : "RDFILE: *.py",
+        "content_type" : "application/x-python",
+        "info": "Uses the bit.ly REST API to expand and get additional information on bit.ly shortened URLs."
+    }
+  }
+}
new file mode 100644
--- /dev/null
+++ b/couch_docs/wq/rd.ext.core.msg-body-quoted-to-bitly.py
@@ -0,0 +1,66 @@
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is Raindrop.
+#
+# The Initial Developer of the Original Code is
+# Mozilla Messaging, Inc..
+# Portions created by the Initial Developer are Copyright (C) 2009
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#
+
+# This extension extracts information about a mailing list through which
+# a message has been sent.  It creates rd.mailing-list docs for the mailing
+# lists, one per list, and rd.msg.email.mailing-list docs for the emails,
+# one per email sent through a list.  All the information about a list
+# is stored in its rd.mailing-list doc; a rd.msg.email.mailing-list doc
+# just links an email to its corresponding list.
+
+import re
+import urllib2, json
+
+# Grab the hash code from links
+bitly_regex = re.compile('http://bit.ly/(\S+)')
+
+# Creates 'rd.msg.attachment' for bit.ly urls from 'rd.msg.body.quoted.hyperlinks'
+def handler(doc):
+
+    if not 'links' in doc:
+        return
+
+    bitlys = []
+    links = doc['links']
+    for link in links:
+        bitlys += bitly_regex.findall(link)
+
+    if len(bitlys) == 0:
+        return
+
+    for hash in bitlys:
+        options = {
+            'version' : '2.0.1',
+            'login'   : 'bitlyapidemo', # demo API user
+            'apiKey'  : 'R_0da49e0a9118ff35f52f629d2d71bf07', # demo API key
+            'hash'    : hash
+        }
+
+        info_api = "http://api.bit.ly/info?%s" % "&".join(['%s=%s' % v for v in options.items()])
+        opener = urllib2.build_opener()
+        obj = json.load(opener.open(info_api))
+
+        if obj.get('errorCode') == 0:
+            schema = obj.get('results').get(hash)
+            schema["is_attachment"] = True
+            emit_schema('rd.msg.body.bit.ly', schema)