ShortenUrl

By Euno, 5 Years ago, written in Python, viewed 99 times.
URL http://pb.stoleyour.com/view/d8349c30 Embed
Download Paste or View RawExpand paste to full width of browser
  1. import discord
  2. import asyncio
  3. import urllib.request
  4. import requests
  5. import json
  6. from discord.ext import commands
  7.  
  8. class ShortenUrl:
  9.  
  10.     def __init__(self, bot):
  11.         self.bot = bot
  12.  
  13.     @commands.command(pass_context=True)
  14.     #
  15.     #Checks if the user exists in the database and returns a shortened link on success.
  16.     #
  17.     #Database format is {"USERID":"APIKEY","117450987848990720":"B3Nm9g8L?s="}
  18.     #
  19.     async def ouo(self, ctx):
  20.         with open('data.json') as data_file:
  21.             data_loaded = json.load(data_file)
  22.        
  23.         if data_loaded.get(ctx.message.author.id):
  24.             url = ("http://ouo.io/api/" + data_loaded.get(ctx.message.author.id) + ctx.message.content[5:])
  25.             await self.bot.say(url)
  26.             handle = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})          
  27.             html = urllib.request.urlopen(handle).read().decode('utf-8')
  28.             await self.bot.say("Shortened: " + html)
  29.         else:
  30.             await self.bot.say("nope")
  31.  
  32. def setup(bot):
  33.     bot.add_cog(ShortenUrl(bot))

Reply to "ShortenUrl"

Here you can reply to the paste above