-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmyinstants.py
197 lines (155 loc) · 4.84 KB
/
myinstants.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/env python
"""
Module that search sounds in www.myinstants.com
Author: Luiz Francisco Rodrigues da Silva <[email protected]>
"""
import os
import re
import sys
from urllib.parse import urljoin
import parsel
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
from user_agent import generate_user_agent
SEARCH_URL = "https://www.myinstants.com/en/search/?name={}"
MEDIA_URL = "https://www.myinstants.com{}"
UPLOAD_URL = "https://www.myinstants.com/en/new/"
LOGIN_URL = "https://www.myinstants.com/accounts/login/"
class MyInstantsApiException(Exception):
"""General exception for myinstants api"""
pass
class HTTPErrorException(MyInstantsApiException):
"""HTTP error exception for myinstants api"""
pass
class NameAlreadyExistsException(MyInstantsApiException):
""" "Exception throw when an instants name already exists"""
pass
class FileSizeException(MyInstantsApiException):
"""Exception throw when the instants file size is bigger than supported"""
pass
class LoginErrorException(MyInstantsApiException):
"""Exception thrown when the login failed"""
pass
class InvalidPageErrorException(MyInstantsApiException):
"""Exception thrown when an invalid page is downloaded"""
pass
def search_instants(query):
"""Search instant
Params:
query: String to search
"""
query_string = (
"+".join(query) if isinstance(query, list) else query.replace(" ", "+")
)
response = requests.get(
SEARCH_URL.format(query_string),
headers={
"User-Agent": generate_user_agent(),
},
)
if response.status_code != 200:
return {}
sel = parsel.Selector(response.text)
names = sel.css(".instant .instant-link::text").getall()
links = sel.css(
".instant .small-button::attr(onclick),.instant .small-button::attr(onmousedown)"
).re(r"play\('(.*?)',")
return [
{
"text": text,
"url": MEDIA_URL.format(url),
}
for text, url in zip(names, links)
]
def upload_instant(name, filepath):
"""Upload sound for Myinstants
Params:
name: Name of the instants to be uploaded
filepath: Path of the sound file to be uploaded
"""
# Create session and get cookies
session = requests.session()
response = session.get(
LOGIN_URL,
headers={
"User-Agent": generate_user_agent(),
},
)
sel = parsel.Selector(response.text)
token = sel.css("input[name=csrfmiddlewaretoken]::attr(value)").get()
if not token:
raise InvalidPageErrorException
data = {
"csrfmiddlewaretoken": token,
"login": os.environ["MYINSTANTS_USERNAME"],
"password": os.environ["MYINSTANTS_PASSWORD"],
"next": "/en/new/",
}
# Login
response = session.post(
LOGIN_URL,
data=data,
headers={
"User-Agent": generate_user_agent(),
},
)
if response.status_code != 200:
raise LoginErrorException
# Get upload token
sel = parsel.Selector(response.text)
token = sel.css("input[name=csrfmiddlewaretoken]::attr(value)").get()
if not token:
raise InvalidPageErrorException
filename = os.path.basename(filepath)
multipart_data = MultipartEncoder(
fields={
"csrfmiddlewaretoken": token,
"name": name,
"sound": (
filename,
open(filepath, "rb"),
"audio/mpeg",
),
"image": ("", None, ""),
"color": "00FF00",
"category": "",
"description": "",
"tags": "",
"accept_terms": "on",
}
)
# Upload sound
response = session.post(
UPLOAD_URL,
data=multipart_data,
headers={
"Content-Type": multipart_data.content_type,
"Referer": UPLOAD_URL,
"User-Agent": generate_user_agent(),
},
)
sel = parsel.Selector(response.text)
errors = "\n".join(sel.css("ul.errorlist").getall())
if "instant with this name already exists." in errors:
raise NameAlreadyExistsException
if "please keep filesize under 300.0 kb" in errors:
raise FileSizeException
if response.status_code != 200:
raise HTTPErrorException
last_uploaded_element = sel.xpath(
"//a[contains(@class, 'instant-link') and text()=$name]/@href", name=name
).get()
if not last_uploaded_element:
return response.url
# Return sound url
return urljoin(response.url, last_uploaded_element)
def main():
"""Main function"""
try:
response = search_instants(sys.argv[1:])
except requests.exceptions.Timeout:
print("Timeout")
response = []
print(response)
if __name__ == "__main__":
main()