Last active
August 29, 2015 14:11
-
-
Save Amrt1n3zm/b4e74390e83046a95d20 to your computer and use it in GitHub Desktop.
url unshort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ | |
| The MIT License (MIT) | |
| Copyright (c) 2014 sinfonier-project | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in | |
| all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| THE SOFTWARE. | |
| """ | |
| import json | |
| import urllib2 | |
| import basesinfonierbolt | |
| class UrlUnshort(basesinfonierbolt.BaseSinfonierBolt): | |
| def __init__(self): | |
| basesinfonierbolt.BaseSinfonierBolt().__init__() | |
| def userprepare(self): | |
| # Get Param (get value of "param_name" from input box) | |
| self.apikey = self.getParam("api_key") | |
| def convertkey(key): | |
| if(key=="fullurl"): | |
| return "url" | |
| return key | |
| def userprocess(self): | |
| shurl = self.getField("url") | |
| self.removeField("url") | |
| apicall = "http://api.unshorten.it?shortURL="+shurl+"&responseFormat=json&return=both&apiKey="+self.apikey | |
| response = urllib2.urlopen(apicall) | |
| html = response.read() | |
| js= json.loads(html) | |
| for(key,value) in js.items(): | |
| if(value==3): | |
| self.addField("error",value) | |
| break; | |
| self.addField(convertkey(key),value) | |
| # Mandatory: Emit the tuple to the next bolt | |
| self.emit() | |
| UrlUnshort().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment