Last active
May 3, 2019 22:53
-
-
Save JPBotelho/94bbede6c157df85156d2b50c5eec7f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import discord | |
from discord.ext import commands | |
import random | |
import json | |
headers = { | |
'Upgrade-Insecure-Requests': '1', | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', | |
} | |
key = "" | |
secret = "" | |
def discogsSearch(query): | |
try: | |
finalQuery = query.lower() | |
url = f"https://api.discogs.com/database/search?q=\"{finalQuery}\"&key={key}&secret={secret}" | |
downloadData = requests.get(url, headers=headers).text | |
parser = json.loads(downloadData) | |
items = parser["pagination"]["items"] | |
pages = parser["pagination"]["pages"] | |
firstResultType = parser["results"][0]["type"] | |
firstResultId = parser["results"][0]["id"] | |
firstResultTitle = parser["results"][0]["title"] | |
firstResultLink = "https://www.discogs.com"+parser["results"][0]["uri"] | |
finalOutput = f"""**{items} items found for** "{query}" **on Discogs.**\n**Title:** {firstResultTitle}\n**Type:** {firstResultType}\n{firstResultLink}""" | |
return finalOutput | |
except: | |
return "No results found." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
from discogs import discogsSearch | |
from youtube import youtubeSearch | |
from musicbrainz import musicBrainzSongSearch, musicBrainzArtistSearch | |
from discord.ext import commands | |
import random | |
import requests | |
description = "An example bot to showcase the discord.ext.commands extension module." | |
bot = commands.Bot(command_prefix='?', description=description) | |
@bot.event | |
async def on_ready(): | |
print('Logged in as') | |
print(bot.user.name) | |
print(bot.user.id) | |
print('------') | |
@bot.command() | |
async def discogs(ctx, *queries): | |
query = " ".join(queries) | |
message = discogsSearch(query) | |
await ctx.send(message) | |
@bot.command() | |
async def youtube(ctx, *queries): | |
query = " ".join(queries) | |
message = youtubeSearch(query) | |
await ctx.send(message) | |
@bot.command() | |
async def musicbrainz(ctx, *queries): | |
print(queries) | |
if(len(queries) >= 2): | |
if(queries[0] == "release"): | |
queryList = list(queries) | |
del queryList[0] | |
finalQuery = " ".join(queryList) | |
message = musicBrainzSongSearch(finalQuery) | |
await ctx.send(message) | |
elif(queries[0] == "artist"): | |
queryList = list(queries) | |
del queryList[0] | |
finalQuery = " ".join(queryList) | |
message = musicBrainzArtistSearch(finalQuery) | |
await ctx.send(message) | |
else: | |
await ctx.send("Invalid syntax.") | |
@bot.command() | |
async def shutdown(ctx): | |
await bot.close() | |
bot.run("NTcxNzUwMzY2ODYyMTE0ODM3.XMTSng.Q4s16MWwQne9mwKgsZwVQ4eFzmw") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import discord | |
from discord.ext import commands | |
import random | |
import json | |
import sys | |
headers = { | |
'Upgrade-Insecure-Requests': '1', | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', | |
'Music Hoarder\'s Discord bot': 'https://discord.gg/qYyxEM2' | |
} | |
def musicBrainzSongSearch(query): | |
try: | |
finalQuery = query.lower() | |
#searchPage = f"https://musicbrainz.org/search?query={finalQuery}&type=release&limit=25&method=indexed" | |
url = f"https://musicbrainz.org/ws/2/release?query={finalQuery}&limit=25&fmt=json" | |
downloadData = requests.get(url, headers=headers).text | |
parser = json.loads(downloadData) | |
totalResults = parser["count"] | |
title = parser["releases"][0]["title"] | |
status = "?" | |
artist = parser["releases"][0]["artist-credit"][0]["artist"]["name"] | |
date = "?" | |
try: | |
date = parser["releases"][0]["date"] | |
status = parser["releases"][0]["status"] | |
except: | |
pass | |
trackCount = parser["releases"][0]["track-count"] | |
discCount = parser["releases"][0]["media"][0]["disc-count"] | |
releaseID = parser["releases"][0]["id"] | |
releaseUrl = "https://musicbrainz.org/release/" + releaseID | |
if(discCount == 0): | |
discCount = 1 | |
print (releaseUrl) | |
finalOutput = f"""**{totalResults} releases found for** "{query}" **on MusicBrainz.**\n**Title:** {title}\n**Artist:** {artist}\n**Status:** {status}\n**Released:** {date}\n\n**Disc Count:** {discCount}\n**Track count:** {trackCount}\n**Release URL:** {releaseUrl}""" | |
return finalOutput | |
except: | |
raise sys.exc_info() | |
def musicBrainzArtistSearch(query): | |
try: | |
finalQuery = query.lower() | |
searchPage = f"https://musicbrainz.org/search?query={finalQuery}&type=artist&limit=25&method=indexed" | |
url = f"https://musicbrainz.org/ws/2/artist?query={finalQuery}&limit=25&fmt=json" | |
downloadData = requests.get(url, headers=headers).text | |
parser = json.loads(downloadData) | |
totalResults = parser["count"] | |
name = parser["artists"][0]["name"] | |
artistType = parser["artists"][0]["type"] | |
gender = "?" | |
born = "?" | |
died = "?" | |
try: | |
gender = parser["artists"][0]["gender"] | |
born = parser["artists"][0]["life-span"]["begin"].capitalize() | |
died = parser["artists"][0]["life-span"]["ended"] | |
except: | |
pass | |
if(died == None): died = "Alive" | |
#Release information needs another request | |
artistID = parser["artists"][0]["id"] | |
artistURL = f"https://musicbrainz.org/ws/2/release?artist={artistID}&fmt=json" | |
artistData = requests.get(artistURL, headers=headers).text | |
artistParser = json.loads(artistData) | |
releaseCount = artistParser["release-count"] | |
artistPage = "https://musicbrainz.org/artist/"+artistID | |
finalOutput = f"""{totalResults} results found for "**{query}**" on MusicBrainz.\n**Name:** {name}\n**Type:** {artistType}\n**Gender:** {gender}\n**Born:** {born}\n**Died:** {died}\n**Releases:** {releaseCount}\n\n**Artist page:** {artistPage}""" | |
return finalOutput | |
except: | |
raise sys.exc_info() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import discord | |
from discord.ext import commands | |
import random | |
import json | |
headers = { | |
'Upgrade-Insecure-Requests': '1', | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', | |
} | |
key = "" | |
def youtubeSearch(query): | |
try: | |
finalQuery = query.lower() | |
url = f"https://www.googleapis.com/youtube/v3/search?q={finalQuery}&part=snippet&type=video&maxResults=1&key={key}" | |
downloadData = requests.get(url, headers=headers).text | |
parser = json.loads(downloadData) | |
videoId = parser["items"][0]["id"]["videoId"] | |
totalResults = parser["pageInfo"]["totalResults"] | |
title = parser["items"][0]["snippet"]["title"] | |
url = "https://www.youtube.com/watch?v="+videoId | |
finalOutput = f"""**{totalResults} items found for** "{query}" **on Youtube.**\n**Title:** {title}\n{url}""" | |
return finalOutput | |
except: | |
return "No results found." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment