-
Notifications
You must be signed in to change notification settings - Fork 1
/
hackcmu20discordcommands.py
67 lines (57 loc) · 2.17 KB
/
hackcmu20discordcommands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from datetime import date
from datetime import time
from datetime import datetime
from dotenv import load_dotenv
import os
# google news api https://newsapi.org/docs
from newsapi import NewsApiClient
# Initialization for News API
load_dotenv()
apiKey = os.getenv('newsApiKey')
newsapi = NewsApiClient(api_key=apiKey)
# news API
def getArticles(query):
top_headlines = newsapi.get_top_headlines(q=query,
# sources='bbc-news,the-verge',
# category='general',
language='en',
country='us')
allArticles = ''
for i in range(len(top_headlines['articles'])):
if i > 4:
break
url = top_headlines['articles'][i]['url']
allArticles += f'{url} \n'
return allArticles
# spotify
import spotipy #https://spotipy.readthedocs.io/en/2.16.0/
from spotipy.oauth2 import SpotifyClientCredentials
import sys
from spotipy.oauth2 import SpotifyClientCredentials
spotify = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
def artistSearch(artistName):
name = artistName
results = spotify.search(q='artist:' + name, type='artist')
items = results['artists']['items']
url = results['artists']['items'][0]['external_urls']['spotify']
if len(items) > 0:
artist = items[0]
print(artist['name'], artist['images'][0]['url'], url)
return url
def songSearch(songName):
name = songName
results = spotify.search(q='track:' + name, type='track')
url = results['tracks']['items'][0]['external_urls']['spotify']
finalResults = ''
for i in range(5):
finalResults += f"{results['tracks']['items'][i]['external_urls']['spotify']} \n"
return finalResults
def albumSearch(albumName):
name = albumName
results = spotify.search(q='album:' + name, type='album')
url = results['albums']['items'][0]['external_urls']['spotify']
finalResults = ''
number = min(3, len(results['albums']['items']))
for i in range(number):
finalResults += f"{results['albums']['items'][i]['external_urls']['spotify']} \n"
return finalResults