Python - uploading audio via API

That’s similar to the approach I took when I wanted to import assimil content en-masse. Here’s what I ended up doing.

#!/usr/bin/env python

import eyed3
import sys
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

get your apikey from Login - LingQ and paste it in

APIKEY=‘Token xxxxxxxxxxxxxxxxxxxx’

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

if name == “main”:
main()

3 Likes