def printenvironment():
print(f’The API key is: {key}‘)
print(f’The mp3 dir is: {mp3DIR}’)
if name == “main”:
printenvironment()
data = {"title": "Lorem Ipsum",
“text”: “Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.”,
“status”: status,
“tags”: [“Test”],
“collection”: collectionID,
“audio”: mp3DIR }
response = requests.post(‘https://www.lingq.com/api/v2/fr/lessons/’,
data=data, headers={‘Authorization’: 'Token ’ + key})
print(response.status_code)
print(response.text)
THIS IS THE OUTPUT:
The API key is: d57b6431d575db71fc754b5e5527dd2e422a2f94
The mp3 dir is: / Users/paulgamble/Desktop/mp3splitter-js/test.mp3
400
#{“audio”: [“The submitted data was not a file. Check the encoding type on the form.”]}
def main():
for args in sys.argv[1:]:
assimil = eyed3.load(args)
lyrics = assimil.tag.lyrics[0].text
title = args + ’ - ’
transcript = ‘’
course = assimil.tag.artist
# split lyrics string to an array of lines
lyricsArr = lyrics.splitlines()
# iterate through the lyrics to generate a transcript and title
for x in lyricsArr:
y = x.split(’ : ')
if y[0][0] == ‘N’:
title = title + y[1] + ’ - ’
elif y[0] == ‘S00-TITLE’:
title = title + y[1]
else:
transcript = transcript + y[1] + “\r\n”
# we have the necessary data now use the lingq api
m = MultipartEncoder([
('title',title),
('text',transcript),
('status','private'),
('audio',(args, open(args, 'rb'), 'audio/mpeg'))]
)
h = {'Authorization' : APIKEY,
'Content-Type' : m.content_type}
r = requests.post('https://www.lingq.com/api/v2/es/lessons/', data=m, headers=h)
print r.text
Yeah, the code at the bottom is all you need. Specify the params in the MultipartEncoder tuples, set your Authorization and Content-Type headers, and send the post. You’ll need requests-toolbelt, though. Otherwise you can do it manually by piecing together the multipart form data like you did in your partial example…
In a future version (and ideal world) it would be nice to scrape my ebooks with this script as well rather than copy and paste. If I have time in the future perhaps I will find a way.
In the mean time this resolved my original problem I was trying to solve with audio upload.