ShortenUrl

By Euno, 5 Years ago, written in Python, viewed 112 times.
URL http://pb.stoleyour.com/view/138cdc0a 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. from discord.ext import commands
  6.  
  7. class ShortenUrl:
  8.  
  9.     def __init__(self, bot):
  10.         self.bot = bot
  11.  
  12.     @commands.command(pass_context=True)
  13.     """
  14.    Checks if the user exists in the database and returns a shortened link on success.
  15.    
  16.    Database format is {"USERID":"APIKEY","117450987848990720":"B3Nm9g8L?s="}
  17.    """
  18.     async def ouo(self, ctx):
  19.         with open('data.json') as data_file:
  20.             data_loaded = json.load(data_file)
  21.        
  22.         if data_loaded.get(ctx.message.author.id):        
  23.             url = ("http://ouo.io/api/" + data_loaded.get(ctx.message.author.id) + ctx.message.content)
  24.             handle = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
  25.             html = urllib.request.urlopen(handle).read().decode('utf-8')
  26.             await self.bot.say("Shortened: " + html)
  27.         else:
  28.             await self.bot.say("nope")
  29.  
  30. def setup(bot):
  31.     bot.add_cog(ShortenUrl(bot))

Reply to "ShortenUrl"

Here you can reply to the paste above