ShortenUrl

By Euno, 5 Years ago, written in Python, viewed 113 times.
URL http://pb.stoleyour.com/view/07fb8474 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. import os.path
  7. from discord.ext import commands
  8.  
  9. class ShortenUrl:
  10.  
  11.     def __init__(self, bot):
  12.         self.bot = bot
  13.  
  14.     @commands.command(pass_context=True)
  15.     #
  16.     #Checks if the user exists in the database and returns a shortened link on success.
  17.     #
  18.     #Database format is {"USERID":"APIKEY","117450987848990720":"B3Nm9g8L?s="}
  19.     #
  20.     async def ouo(self, ctx):
  21.    
  22.         if os.path.isfile('users.json'):
  23.             with open('users.json') as user_file:
  24.                 users_loaded = json.load(user_file)
  25.            
  26.             if users_loaded.get(ctx.message.author.id):
  27.                 storedUrl = ctx.message.content[5:]
  28.                 storedUrl = storedUrl.replace("&", "%26")
  29.                 storedUrl = storedUrl.replace("#", "%23")
  30.                 storedUrl = ("http://ouo.io/api/" + users_loaded.get(ctx.message.author.id) + storedUrl)
  31.                 urlRequest = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})          
  32.                 shortenedUrl = urllib.request.urlopen(urlRequest).read().decode('utf-8')
  33.                 await self.bot.say("Shortened: " + shortenedUrl)
  34.             else:
  35.                 await self.bot.say("nope")
  36.         else:
  37.             await self.bot.say("Please create users.json")
  38.  
  39. def setup(bot):
  40.     bot.add_cog(ShortenUrl(bot))

Reply to "ShortenUrl"

Here you can reply to the paste above