def bis_search(search, tags=None): if cache_search(search): # skip repeats return terms = " ".join(map(lambda q: "+" + q, search.split())) queryBingFor = "'%s'" % terms # the apostrophe's required as that is the format the API Url expects. quoted_query = urllib.quote(queryBingFor) rootURL = "https://api.datamarket.azure.com/Bing/Search/" searchURL = rootURL + "Image?Adult=%27Off%27&$format=json&Query=" + quoted_query print searchURL pwds = Passwords() username = pwds.getPassword('microsoft.accountKey') accountKey = pwds.getPassword('microsoft.accountKey') password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, searchURL,username,accountKey) handler = urllib2.HTTPBasicAuthHandler(password_mgr) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) try: resp = urllib2.urlopen(searchURL) content = resp.read() code = 200 except urllib2.URLError, e: code = str(e)
def bis_search(search, tags=None): if cache_search(search): # skip repeats return terms = " ".join(map(lambda q: "+" + q, search.split())) queryBingFor = "'%s'" % terms # the apostrophe's required as that is the format the API Url expects. quoted_query = urllib.quote(queryBingFor) rootURL = "https://api.datamarket.azure.com/Bing/Search/" searchURL = rootURL + "Image?Adult=%27Off%27&$format=json&Query=" + quoted_query print searchURL pwds = Passwords() username = pwds.getPassword('microsoft.accountKey') accountKey = pwds.getPassword('microsoft.accountKey') password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, searchURL, username, accountKey) handler = urllib2.HTTPBasicAuthHandler(password_mgr) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) try: resp = urllib2.urlopen(searchURL) content = resp.read() code = 200 except urllib2.URLError, e: code = str(e)
def post_to_imgur(url, title=None): imgur_url = None pwds = Passwords() client_id = pwds.getPassword('imgur.clientId') if client_id is None: return imgur_url headers = {} headers['Authorization'] = "Client-ID %s" % client_id params = {} params['image'] = url if title is not None: params['title'] = title params['description'] = title code, content, resp = get_page('https://api.imgur.com/3/image', params, headers) if code == 200: results = json.loads(content) try: imgur_url = results["data"]["link"] except: print "Invalid imgur response", content pass elif code is not None: print "imgur said: %s" % code else: print "get_page returned None?" return imgur_url
def extract_article_text(url): """Uses a web service to extract the text of an article.""" pwds = Passwords() token = pwds.getPassword('apibot.token') params = {} params['token'] = token params['url'] = url params['timeout'] = 20000 # timeout in ms summary_url = 'http://www.diffbot.com/api/article?%s' % urllib.urlencode( params) code, content, resp = util.get_page(summary_url, max_size=512 * 1024) if code != 200: print "Got %s requesting %s" % (str(code), summary_url) return None try: results = json.loads(content) except: print code, content, resp return "The summarizer doesn't like this." if "title" in results: if "text" in results: return "%s\f%s" % (results["title"], results["text"]) else: return results["title"] elif "text" in results: return results["text"] else: print results return None
def shorten_url(url): """Uses a web service to shorten a long url.""" short_url = None pwds = Passwords() token = pwds.getPassword('bitly.token') if random.random() < 0.01: url = random.choice(random_urls) params = { "access_token": token, "longUrl": url, "domain": "j.mp", # bit.ly and bitly.com are also options. } shortener = 'https://api-ssl.bitly.com/v3/shorten?%s' % urllib.urlencode( params) (code, content, resp) = util.get_page(shortener) url = None if code == 200: try: results = json.loads(content) except: print "error loading json from", shortener, content try: url = results["data"]["url"] except: print "unexpected json response from", shortener, results else: print shortener, "returned", code, content return url
def extract_article_text(url): """Uses a web service to extract the text of an article.""" pwds = Passwords() token = pwds.getPassword('apibot.token') params = {} params['token'] = token params['url'] = url params['timeout'] = 20000 # timeout in ms summary_url = 'http://www.diffbot.com/api/article?%s' % urllib.urlencode( params) code, content, resp = util.get_page(summary_url, max_size=512*1024) if code != 200: print "Got %s requesting %s" % (str(code), summary_url) return None try: results = json.loads(content) except: print code, content, resp return "The summarizer doesn't like this." if "title" in results: if "text" in results: return "%s\f%s" % (results["title"], results["text"]) else: return results["title"] elif "text" in results: return results["text"] else: print results return None
def make_tumblr_request(url, params): """This is the root of the tumblr api. Can perform any tumblr operation by defining request_type and params correctly. returns (success, response, result) """ # oauth stuff -- I should probably cache this. pwds = Passwords() consumer_key = pwds.getPassword('tumblr.consumerKey') consumer_secret = pwds.getPassword('tumblr.consumerSecret') access_key = pwds.getPassword('tumblr.accessKey') access_secret = pwds.getPassword('tumblr.accessSecret') if (consumer_key is None or consumer_secret is None or access_key is None or access_secret is None): print "I tried to make a tumblr but I'm not configured." return (None, None, None) consumer = oauth.Consumer(consumer_key, consumer_secret) token = oauth.Token(access_key, access_secret) oauth_client = oauth.Client(consumer, token) # set up request if url is not None: request_url = url else: request_url = 'http://api.tumblr.com/v2/blog/goat-blog.tumblr.com/%s' % ( request_type) method = 'POST' body = urllib.urlencode(params) response, content = oauth_client.request(request_url, method, body) if response.status >= 200 and response.status < 300: success = True else: success = False try: results = json.loads(content)["response"] except: results = None return success, response, results