forked from OrendCross/TwitchSongRequest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsongRequestInput.py
More file actions
170 lines (142 loc) · 5.17 KB
/
Copy pathsongRequestInput.py
File metadata and controls
170 lines (142 loc) · 5.17 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# songRequestInput.py
# Takes inputted YouTube, SoundCloud, or phrases and inserts relevant audio URLs into the queue database
import os.path, sqlite3, urllib.request, re, sys, codecs
from Google import Create_Service
from urllib.parse import urlparse
from bs4 import BeautifulSoup
from iso8601_duration import parse_duration
#YouTube API Info
CLIENT_SECRET_FILE = 'H:\My Drive\MixItUp\Files\client_secret_913406518902-2v45e1g516ukedgf94cph01e0uf65ju3.apps.googleusercontent.com.json'
API_NAME = 'youtube'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/youtube']
con = sqlite3.connect('H:\My Drive\MixItUp\Files\songRequest.db')
# Global Variables
youTubeURL = 'https://www.youtube.com/watch?v='
maxVideoSeconds = 300
# Return YouTube Video ID from search term(s)
def searchYoutube(searchQuery):
search = searchQuery.replace(' ','+')
html = urllib.request.urlopen('https://www.youtube.com/results?search_query=' + search)
video_ids = re.findall(r'watch\?v=(\S{11})', html.read().decode())
return video_ids[0]
# Return YouTube Video ID from URL
def getYouTubeID(Search):
parseURL = urlparse(Search)
if len(parseURL.query) > 0:
videoID = parseURL.query.replace('v=', '')
else:
videoID = parseURL.path.replace('/', '')
return videoID
# Get YouTube Information
def getYouTubeInfo(videoID):
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
part_string = 'contentDetails,snippet'
video_ids = videoID
response = service.videos().list(
part = part_string,
id = video_ids
).execute()
return response
# Get SoundCloud title
def getSoundCloudInfo(url):
html = urllib.request.urlopen(url)
title = re.findall(r'<title>(.*?)<\/title>', html.read().decode())[0]
title = title.replace(' | Listen online for free on SoundCloud', '')
title = title.replace('<title>', '')
title = title.replace('"', '')
title = title.replace('Stream ', '', 1)
title = title.replace('Listen to ', '', 1)
title = title.replace(' playlist online for free on SoundCloud', '')
return title
# Return false if video is longer than 300 seconds
def checkVideoLength(durration):
if parse_duration(durration).total_seconds() > maxVideoSeconds:
return False
return True
# Inserts songs and users into the SongDatabase
def insertIntoDB(Priority, TwitchUserID, Username, Title, URL):
cur = con.cursor()
cur.execute("INSERT OR REPLACE INTO Users VALUES (?, ?)", (TwitchUserID, Username))
cur.execute("INSERT INTO Songs VALUES (?, ?, ?, ?, ?)", (Title, URL, 'Queued', Priority, TwitchUserID))
con.commit()
con.close()
# Checks SongDatabase for duplicate URLS in the active queue
def checkForDuplicates(URL):
cur = con.cursor()
cur.execute("SELECT COUNT(*) FROM Songs WHERE URL = ? AND Status = 'Queued'", (URL,))
dupes = cur.fetchone()[0]
con.close()
if dupes > 0:
return True
else:
return False
# Main
# Command:
# songRequestBot.py [Priority=0|1] [TwitchUserID] [TwitchUsername] [RequestURL/SearchTerm(s)]
#
# Result:
# Return (via print) message to be sent to chat
# Add song to SQLite DB
def main():
# Priority = 0 | 1
Priority = sys.argv[1]
# TwitchUserID = Twitch UUID
TwitchUserID = sys.argv[2]
# TwitchUsername = Username
TwitchUsername = sys.argv[3]
# Search = SoundCloud or YouTube or phrase
Search = sys.argv[4]
for x in sys.argv[5:]:
Search = Search + ' ' + x
parseURL = urlparse(Search)
URL = ''
Title = ''
if len(parseURL.netloc) > 0:
# If input is a YouTube Link
if 'youtube' in parseURL.netloc or 'youtu.be' in parseURL.netloc:
ID = getYouTubeID(Search)
videoInfo = getYouTubeInfo(ID)
# If YouTube Link is invalid
if videoInfo['pageInfo']['totalResults'] == 0:
print('@' + TwitchUsername + 'Unable to add song to queue: link invalid!')
return
if not checkVideoLength(videoInfo['items'][0]['contentDetails']['duration']):
print('@' + TwitchUsername + 'Request is not within the required length, 1 to 300 seconds')
return
URL = youTubeURL + ID
Title = videoInfo['items'][0]['snippet']['title']
# Else if input is a SoundCloud Link
elif 'soundcloud' in parseURL.netloc:
URL = Search
Title = getSoundCloudInfo(Search)
else:
# If input is a URL that does not get caught by the initial checks
print('@' + TwitchUsername + 'Only valid YouTube or SoundCloud links are accepted!')
return
# Else input is something else (phrase, youtube VideoID, unsupported website, or gibberish)
else:
# Checking if YouTube Video ID
ID = getYouTubeID(Search)
videoInfo = getYouTubeInfo(ID)
if videoInfo['pageInfo']['totalResults'] != 0:
URL = youTubeURL + ID
Title = videoInfo['items'][0]['snippet']['title']
else:
ID = searchYoutube(Search)
videoInfo = getYouTubeInfo(ID)
URL = youTubeURL + ID
Title = videoInfo['items'][0]['snippet']['title']
if checkForDuplicates(URL):
print('@' + TwitchUsername + 'This song is already in the Queue!')
return
insertIntoDB(Priority, TwitchUserID, TwitchUsername, Title, URL)
print('@' + TwitchUsername + 'Added ' + Title + ' to the playlist')
#print('URL: ' + URL )
#print('Title: ' + Title )
#print('User: ' + TwitchUserID )
#encoded_unicode = Title.encode("utf8")
#f = open('H:\\My Drive\\MixItUp\\Files\\test.txt', "wb")
#f.write(encoded_unicode)
#f.close()
main()