def test_owly_bad_key(): b = Shortener(shorteners.OWLY_SHORTENER) with pytest.raises(TypeError): b.short('http://www.test.com') with pytest.raises(TypeError): b.expand('http://www.test.com')
def test_owly_bad_key(): b = Shortener('OwlyShortener') with pytest.raises(TypeError): b.short('http://www.test.com') with pytest.raises(TypeError): b.expand('http://www.test.com')
def test_owly_bad_key(): b = Shortener(Shorteners.OWLY) with pytest.raises(TypeError): b.short('http://www.test.com') with pytest.raises(TypeError): b.expand('http://www.test.com')
def test_bitly_bad_keys(): s = Shortener(Shorteners.BITLY) with pytest.raises(TypeError): s.short(expanded) with pytest.raises(TypeError): s.expand(shorten)
def test_google_bad_params(): s = Shortener(Shorteners.GOOGLE) with pytest.raises(TypeError): s.short(expanded) with pytest.raises(TypeError): s.expand(expanded)
def test_shortener_debug_enabled(): url = 'http://www.test.com' small = 'http://small.com' responses.add(responses.GET, url, body=small) responses.add(responses.GET, small, body=url) s = Shortener(debug=True) s.short('http://www.test.com') s.expand('http://small.com') with pytest.raises(NotImplementedError): s.total_clicks('http://small.com')
def test_is_valid_url(): bad = 'www.google.com' good = 'http://www.google.com' assert is_valid_url(good) assert not is_valid_url(bad) s = Shortener(Shorteners.TINYURL) with pytest.raises(ValueError): url = 'http://12' s.short(url) with pytest.raises(ValueError): url = 'www.11.xom' s.expand(url)
def test_is_valid_url(): bad = 'www.google.com' good = 'http://www.google.com' assert is_valid_url(good) assert not is_valid_url(bad) s = Shortener('TinyurlShortener') with pytest.raises(ValueError): url = 'http://12' s.short(url) with pytest.raises(ValueError): url = 'www.11.xom' s.expand(url)
def hello(): short = Shortener('TinyurlShortener') print(""" Hello World! Testing TinyurlShortener with www.google.com URL Shorten url: {} Expanded: {} """.format(short.short('http://www.google.com'), short.expand('http://goo.gl/fbsS')))
def hello(): googl = Shortener('GoogleShortener') return """ Hello World! Testing www.google.com Shorten url:{} - Expanded:{} """.format(googl.short('http://www.google.com'), googl.expand('http://goo.gl/fbsS')),
def hello(): short = Shortener('Tinyurl') print(""" Hello World! Testing TinyurlShortener with www.google.com URL Shorten url: {} Expanded: {} """.format(short.short('http://www.google.com'), short.expand('http://goo.gl/fbsS')))
def short_3(short_url_hash): fetch = mongo.db.fetch url_to = short_url_hash shortener = Shortener('Tinyurl') url_to = url_to.replace("hck.re", "http://tinyurl.com") long_url = shortener.expand(url_to) # return redirect(long_url, code=302) return jsonify({"q": url_to, "w": short_url_hash})
class UrlExpand: def __init__(self): self.shortener = Shortener('Tinyurl') def decodeURL(self, url): try: result = self.shortener.expand(url) return result except Exception as e: return url
class Shortening: def __init__(self): self.shortener = Shortener('Tinyurl') def shortenURL(self): self.url = input("Enter URL to shorten: ") shortURL = self.shortener.short(self.url) print("The shortened URL is: " + shortURL) def decodeURL(self): self.url = input("Enter URL to expand: ") expandURL = self.shortener.expand(self.url) print("The expanded URL is: " + expandURL)
class Shortlinks(Shortener): """ Classe que lida com a compressão ou encurtamento do link. """ def __init__(self): print(''' Instruções: Entre com uma URL válida e aperte enter. exemplo: https://dev.to/peteranglea/6-months-of-working-remotely-taught-me-a-thing-orten Opções: 1. Encurtar a URL 2. Decodificar uma URL encurtada 3. Transformar URL em QR code ''') self.option = int(input("Escolha uma opção: ")) self.url = str(input("Entre com a URL: ")) self.shortener = Shortener('Tinyurl') if self.option == 1: self.short_url() elif self.option == 2: self.decode_url() elif self.option == 3: self.qrcode_url() else: print('Opção inválida.') def short_url(self): """ Encurta uma URL """ self.shortened_url = self.shortener.short(self.url) print("Short URL: " + self.shortened_url + "\n") def decode_url(self): """ Descomprime uma URL """ self.decoded_url = self.shortener.expand(self.url) print("URL decodificada: " + self.decoded_url + "\n") def qrcode_url(self): """ Gera o QRCode da URL. """ self.shortener.short(self.url) print(self.shortener.qrcode())
def long_1(): fetch = mongo.db.fetch short_url = request.json['short_url'] # if not validators.url(long_url): # output = {"long_url":long_url, 'status' : "FAILED","status_codes":["INVALID_URLS"]} # else: # api_key = 'private' # shortener = Shortener('Tinyurl') # short_url=shortener.short(long_url) # short_url=short_url.replace("http://tinyurl.com","hck.re") # output = {"long_url":long_url,'short_url' : short_url, 'status' : "OK","status_codes":[]} shortener = Shortener('Tinyurl') short_url = short_url.replace("hck.re", "http://tinyurl.com") long_url = shortener.expand(short_url) output = {"long_url": long_url, 'status': "OK", "status_codes": []} return jsonify(output)
def long_2(): fetch = mongo.db.fetch long_urls = {} invalid_urls = [] short_urls = request.json['short_urls'] for i in short_urls: try: shortener = Shortener('Tinyurl') i = i.replace("hck.re", "http://tinyurl.com") long_url = shortener.expand(i) long_urls[i] = long_url except Exception: i = i.replace("http://tinyurl.com", "hck.re") output = { "invalid_urls": [i], 'status': "FAILED", "status_codes": ["SHORT_URLS_NOT_FOUND"] } return jsonify(output) if invalid_urls != []: output = { "invalid_urls": [], 'status': "FAILED", "status_codes": ["SHORT_URLS_NOT_FOUND"] } return jsonify(output) else: output = { "long_urls": long_urls, "invalid_urls": [], 'status': "OK", "status_codes": [] } return jsonify(output) return jsonify(output)
def test_googl_expand_function(self): engine = 'GoogleShortener' short = Shortener(engine) self.assertEqual(short.expand('http://goo.gl/fbsS'), 'http://www.google.com/')
def test_expand_method_bad_response(): responses.add(responses.GET, short, body='', status=400) s = Shortener() with pytest.raises(ExpandingErrorException): s.expand(short)
def test_expand_method_bad_url(): s = Shortener() with pytest.raises(ValueError): s.expand('test.com')
import pandas as pd import API import json from pyshorteners import Shortener import os fileList={} shortener = Shortener('Tinyurl', timeout=9000) for account in json.loads(API.getAccounts()): for sendFiles in json.loads(API.getSendMessageResult(account)): if os.path.isfile('messageExl/' + sendFiles['FileName']): df = pd.read_csv('messageExl/' + sendFiles['FileName'] , sep=',', encoding='utf_8_sig',low_memory=False) if u'專屬網址' in df: try: url = df[u'專屬網址'].values[0] print url longer = shortener.expand(url) fileList[sendFiles['FileName']] = longer.split('AICRM-')[1].split('&')[0] + '.csv' except: print sendFiles['FileName'] print(fileList) for F, m in fileList.items(): API.Cangeee(F,m) for key, value in largeSet.items()[1:]: df = pd.read_csv('0978136278' , sep=',', encoding='utf_8_sig',low_memory=False) dfList = df[u'收件人手機'].values
def test_tinyurl_expand_function(self): engine = 'TinyurlShortener' short = Shortener(engine) self.assertEqual(short.expand('http://tinyurl.com/ycus76'), u'https://www.facebook.com')
def handle(msg): chat_id = msg['chat']['id'] # Stores chat id for referencing message try: command = msg['text'] # Filters text from the message sent except KeyError as k: try: file_id = (msg['photo'][2])['file_id'] file_path = bot.getFile(file_id) f_path = "https://api.telegram.org/file/bot221225786:AAElg0gODaJi7-xy0AM68eKH5moyuXZOzh0/" + file_path[ 'file_path'] #file=bot.download_file(f_path) bot.sendPhoto(chat_id, file_id) return except e: print(e) command = "Not Applicable" bot.sendMessage( chat_id, "Please try again with a command or an image /help") return print('Got command: %s' % command) # Here Starts the command interpretation if command == '/roll': bot.sendMessage(chat_id, random.randint(1, 6)) elif command == '/time': bot.sendMessage(chat_id, str(datetime.datetime.now())) elif command == '/hi': bot.sendMessage(chat_id, "Hello") elif command == '/weather': temperature = "Temperature > " + str( w.get_temperature(unit='celsius')['temp'] ) + " degree celcius\nMaximum Temperature > " + str( w.get_temperature(unit='celsius')['temp_max'] ) + " degree celcius\nMinimum Temperature > " + str( w.get_temperature(unit='celsius')['temp_min']) + " degree celcius" wind = "Speed > " + str(w.get_wind()['speed']) + "\nDegrees > " + str( w.get_wind()['deg']) + " degrees clockwise from North direction" pressure = "Sea Level > " + str( w.get_pressure()['sea_level']) + "\nPressure > " + str( w.get_pressure()['press']) bot.sendMessage( chat_id, "New Delhi,India\n( " + w.get_detailed_status() + " )\n\nTemperature Details :\n" + temperature + "\n\nWind Speed Details :\n" + wind + "\n\nCloud Coverage : \n" + str(w.get_clouds()) + "%" + "\n\nHumidity : \n" + str(w.get_humidity()) + "%" + "\n\nPressure Details :\n" + pressure + "\n\nData fetched by openweathermap API.All copyrights reserved") elif '/wiki' in command: try: ny = wikipedia.summary(command[5:len(str(command))], sentences=7) bot.sendMessage(chat_id, ny) except wikipedia.exceptions.DisambiguationError as e: stri = "This may refer to :\n\n" for i, topic in enumerate(e.options): stri = stri + str(i) + " " + topic + "\n" stri = stri + "\nPlease choose anyone from above options" bot.sendMessage(chat_id, stri) except wikipedia.exceptions.PageError as e: bot.sendMessage(chat_id, "No partial/full match found for this") elif command == '/help': bot.sendMessage( chat_id, """List of supported commands is\n /hi - Greet Your Device\n /roll - Rolls a dice\n /weather - Tells detailed current weather report of Raspberry Pi's location\n /time - Tells current date and time\n /wiki <Topic Name> - Does a topic search on wikipedia and gives a summary of the topic.Try long tapping /wiki in autofill\n /torrent <magnet link/torrent url/infohash> - Adds and downloads torrent to your raspberry pi remotely\n /torrent_status - Give the detailed status of your torrent(s) you have added/downloaded\n /url <URL> - Shorten the given URL using Google API(goo.gl).\n /url_exp <Shortened URL> - Expands the given shortened url made using Google API\n /speedtest - Does a detailed network speed test using ookla's speedtest API\n /yt <Youtube video link> - Creates the shortened download link for given youtube video\n /news <Topic> - Displays top 10 latest headlines fetched by Google News API about given toipc using Beautiful soup py Library. \n\nSee your autofill for quick selection of command or tap '/' icon on right side of your chat textbox.\n For Commands with parameters,you can long tap the autosuggestion for quick typing and type your parameter followed by a space.""" ) elif '/torrent ' in command: os.system("deluge-console add Desktop " + command[8:len(str(command))]) bot.sendMessage(chat_id, "Torrent Successfully added") elif command == '/torrent_status': p = os.popen("deluge-console info") q = p.read() try: bot.sendMessage(chat_id, str(q)) except telepot.exception.TelegramError as e: bot.sendMessage(chat_id, "No added torrents found for remote download") p.close() elif '/url ' in command: url = str(command[5:len(command)]) if validators.url(url): shortener = Shortener('Google', api_key=api_key) bot.sendMessage(chat_id, "Shortened URL is\n" + str(shortener.short(url))) else: bot.sendMessage(chat_id, "Please enter a valid url") elif '/url_exp ' in command: url = str(command[9:len(command)]) shortener = Shortener('Google', api_key=api_key) bot.sendMessage(chat_id, "Expanded URL is\n" + shortener.expand(url)) elif command == '/speedtest': bot.sendMessage( chat_id, """Wait for a while until we check and measure speed of system's network. If result does'nt come in 30 seconds,Try again.Little patience is appreciated...""" ) try: p = str(subprocess.check_output(["speedtest-cli"])) q = p[2:len(p) - 1] r = q.replace("\\r", "") s = r.split("\\n") bot.sendMessage(chat_id, '\n'.join(s)) except: bot.sendMessage( chat_id, "Something went wrong,Please try again\n Or\nTry some other commands /help" ) elif '/news ' in command: headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' } tempurl = "https://www.google.com/search?q=%s&num=10&start=10&tbm=nws#q=%s&tbas=0&tbs=sbd:1&tbm=nws&gl=d" news_topic = command[6:] url = tempurl % (news_topic, news_topic) print(url) ahrefs = [] titles = [] req = requests.get(url, headers=headers) soup = bs4.BeautifulSoup(req.text, "html.parser") #you don't even have to process the div container #just go strait to <a> and using indexing get "href" #headlines ahref = [a["href"] for a in soup.find_all("a", class_="_HId")] #"buckets" ahref += [a["href"] for a in soup.find_all("a", class_="_sQb")] ahrefs.append(ahref) #or get_text() will return the array inside the hyperlink #the title you want title = [a.get_text() for a in soup.find_all("a", class_="_HId")] title += [a.get_text() for a in soup.find_all("a", class_="_sQb")] titles.append(title) #print(ahrefs) titles = str(titles) titles = titles.strip("[[]]") titles = titles.replace('"', '\'') titles = " " + titles tit = titles.split(',') ans = "" k = 0 for i in tit: if str(i)[0] == " " and str(i)[1] == "'": ans = ans + "\n" + str(k + 1) + ". " + str(i) k = k + 1 else: ans = ans + "\n" + str(i) bot.sendMessage( chat_id, "Top " + str(k) + " latest news headlines for the given topic are :\n\n" + ans) elif '/yt ' in command: bot.sendMessage( chat_id, "Wait until we create the download link,Sitback and relax..") url = command[4:len(command)] ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'}) with ydl: result = ydl.extract_info(url, download=False) # We just need the info if 'entries' in result: # Can be a playlist or a list of videos video = result['entries'][0] else: # Just a video video = result video_url = video['url'] p = shortener.short(video_url) bot.sendMessage(chat_id, "Download link for given youtube video is:\n" + p) elif '/cal ' in command: ans = eval(str(command[5:len(command)])) bot.sendMessage(chat_id, "Answer is:\n" + ans) else: bot.sendMessage( chat_id, "Type /help for list of supported commands till now,There are many more to come!!" )