def url_shortner(self, long_url): if url.startswith("http"): b = bitly_api.Connection(login=self.bitly_user, api_key=self.bitly_key) bitly_url = b.shorten(long_url)["url"] return bitly_url else: return "Invalid URI. Please ensure the url begins with {HTTP}."
def tos_tweeter(): randomint = random.randint(1, 7689) html = requests.get('http://tos.sky.is/tos/to/word/isl/%s/' % randomint).text root = lxml.html.fromstring(html) for node in root.cssselect("sup"): #drop footnotes node.drop_tree() try: word = root.xpath('//div[@class="word_title"]') word_isl = word[0][0].text[:1].upper() + word[0][0].text[1:] kyn = word[0][0].tail.strip() word_definition = root.xpath( '//div[@class="word_definition"]')[0].text_content() enska = root.xpath('//div[@class="word_detail"]/*[text()="Enska:"]/..' )[0].text_content().replace('Enska:', '(e.') if kyn != '': strengur = word_isl + ' (' + kyn + ')' else: strengur = word_isl strengur = strengur + ' ' + enska + '). ' + word_definition strengur = smart_truncate(strengur) c = bitly_api.Connection(access_token=BITLY_ACCESS_TOKEN) shorturl = c.shorten('http://tos.sky.is/tos/to/word/isl/%s/' % randomint)['url'] strengur = strengur + ' ' + shorturl except Exception, e: tos_tweeter()
def get_news(): """ Fetches latest news from News API. Collects the top 5 news from google news. Shortens news links using Bitly APIs. Returns Aggregated news. """ API_USER = settings.BITLY_API_USER API_KEY = settings.BITLY_API_KEY bitly_conn = bitly_api.Connection(API_USER, API_KEY) url = "https://newsapi.org/v2/top-headlines?sources=google-news-in&apiKey=" + settings.NEWS_API_KEY news = requests.get(url) data = news.json() get_news = '' for i in range(5): title = data["articles"][i]["title"] description = data["articles"][i]["description"] link = data["articles"][i]["url"] flink = bitly_conn.shorten(uri=link) flink = flink["url"] get_news += "*" + title + " :* _" + description + "_ \n" + flink + "\n\n" return get_news # print(news())
def urlShort(): if request.method == 'POST': urlText = request.form['textURL'] else: urlText = request.args.get('textURL') session['show_url'] = 'show_url_on' bitly = bitly_api.Connection(API_USER, API_KEY) response = bitly.shorten(urlText) shorturl = response["url"] longurl = response["long_url"] user = mongo.db.urlData data = mongo.db.urlData.find_one({ 'Username': session['username'], 'Long URL': longurl }) if not data: user.insert({ 'Username': session['username'], 'Short URL': shorturl, 'Long URL': longurl }) return render_template('URL_Shortener.html', heading='URLShortener', shorturl=shorturl, longurl=longurl)
def save(self, *args, **kwargs): original = None if self.pk is not None: # being updated self.modified = make_aware(datetime.datetime.now()) original = Expert.objects.get(pk=self.pk) # shorten url if original is None or self.long_url != original.long_url: import bitly_api # todo: wrap below code in try .. except for possible connection issues conn = bitly_api.Connection( access_token=settings.BITLY_ACCESS_TOKEN) self.short_url = conn.shorten(uri=self.long_url)['url'] # get heading text if original is None or self.long_url != original.long_url: from urllib import request from urllib.error import URLError try: response = request.urlopen(self.long_url) html = response.read() charset = response.info().get_charset() if charset is None: charset = 'latin-1' # see: https://stackoverflow.com/a/25759213/2863603 html = html.decode(charset) self.heading_text = self.strip_non_header_text(html) except URLError: raise URLError('Expert object CANNOT be saved due to bad url!') # caught at the view level super().save(*args, **kwargs)
def get_lien_court(url): """Obtenir le lien court""" c = bitly_api.Connection(access_token="") sh = c.shorten(url) return sh['url']
def getTweets(): consumer_key, consumer_secret, access_token, access_token_secret, bitly_access_token = getKeys( ) auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) bitly_connection = bitly_api.Connection(access_token=bitly_access_token) client = MongoClient('127.0.0.1', 27017) db = client.tweets myStreamListener = MyStreamListener(bitly_connection, db, 0) myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener) track = ['bit ly', 'bitly'] connected = False while not connected: try: myStream.filter(track=track) connected = True except Exception as err: print(traceback.format_exc()) print("Trying again...") time.sleep(2) pass
def shortenLink(longURL): API_USER = "******" ACCESS_TOKEN = "3c78ba7edaba170ec27e18905ac22e0af9e36419" c = bitly_api.Connection(access_token=ACCESS_TOKEN) response = c.shorten(longURL) return response['url']
def random_bitly(old_url): c = bitly_api.Connection(api_key=settings.BITLY_CLIENT_ID, secret=settings.BITLY_SECRET_KEY, access_token=settings.BITLY_ACCESS_TOKEN, login=settings.BITLY_LOGIN) bitly_data_dict = c.shorten(old_url) return bitly_data_dict["url"]