0

I'm trying to get content from downloaded file. If i save file like .mp3 it even playing. But when i get content there are a lot of "terrible" characters i.e. (û dInfo.ð"""".....::::EEEEEQQQQ]]]]]hhhhttttt¢¢¢¢®®®®®ººººÅÅÅÅÅÑÑÑÑÝÝÝÝÝèèèèôôôôôÿÿÿÿ:LAME3.96r´.l4 $N .ð]Ú5ÿû d Ô|c½i4BGçá"ʹU§ a0pTìÌ xà0 +£â?=yÃkðý§ýÛußAfØÿ®ÙBDgøQÊ0£a=¹ OTG@ )É ÄN¨hÎDMWQÛ0wmrÛA MdCeÞ9:!b>¢~Ú½´&ÞýÛ/¿h­·{þ>Åï²{·w±Ä,ÅõtewßS©?Ï'è! p@lHåÀ1üoù!c Aæø?Íæ0#äzôüÿsì§òp}o¾Ðn¨:Ð À@1®-0¦ ÐËÛþÐ褹À~! êõCÄâr+Ççú!ɱA3uå^O¦I÷'ív(µ~ÉNÎ~8æÙøÕ+Xy¬gt}êÑ3xk¿½ÞZ÷]ë^ÖÅ¢}åýíªn¾?µ)_{×ÇÎÿÝ>ÿzÞ>wÿß¾5﬿»ü}z}kDtwk)Ó=Ú[ÿzfXÞµ%q Gŧ~(°Ë%¬ÇºJùÝÇá3JBĸÑâ·Ê!W²qll°¡WÎÚRÕ¨âU0BD$F$ÅÕÀèûÏ*©l^Î¥¢3ëÿû¢d¬LY~s`AJ@Á%ù­ 4¨ËÍ;3sB½²ý»é¨murz{S0Ühà @Qö0Â(ÚFíê9(øi¸ò½¸~äÌ]ï¼a b°±±§Rióÿ÷·±O?Ã?:3£M20 /ÿÿÿçßîzöDIþï_ÿÿÿÿú±c>a¾¨2e ÁÙ£¯ÿÿÿÿÿýþÿÿÿÿ ![]h1EOFðKçYwA%ÜSԾó":9Ç5 RþèvC?7òEWÔ´üJdzcKÿÿTdp Lï¬DÆØ£Rm7£Ww·´ÅUeé¢hÇRî÷ @\uä«À#¿6òXµËÖÿ+U Oó}å-) This my code:

    fp = open('audio.txt', "wb")
    ch = curl.Curl()

    ch.setopt(curl.URL, url)
    ch.setopt(curl.TRANSFERTEXT, True)
    ch.setopt(curl.AUTOREFERER, True)
    ch.setopt(curl.FOLLOWLOCATION, True)
    ch.setopt(curl.POST, False)
    ch.setopt(curl.HTTPHEADER, ['REMOTE_ADDR:' + self.ip, 'HTTP_X_FORWARDED_FOR:' + self.ip])
    ch.setopt(curl.USERAGENT, self.useragent)
    ch.setopt(curl.CONNECTTIMEOUT, self.connect_timeout)
    ch.setopt(curl.TIMEOUT, self.curl_timeout)
    ch.setopt(curl.SSL_VERIFYPEER, False)
    ch.setopt(curl.COOKIE, "JSESSIONID=" + sessionid)
    ch.setopt(curl.WRITEDATA, fp)
    try:
        result = ch.perform()
    except curl.error as error:
        #errno, errstr = error
        ch.close()
        return 'Ошибка считывания mp3 файла с сервиса ФМС.'
    fp.close()
    with open('audio.txt', 'r', encoding = "ISO-8859-1") as content_file:
        content_file.seek(0)
        content = content_file.read()
    return content

How I can get normal characters (in UTF8) ? thanks.

  • 2
    MP3 is not textual format for reading before you go asleep, it is binary format which fits taste of your MP3 player. So even when converted to UTF-8 you will get the terrible characters there. – Jan Vlcinsky Jun 12 '14 at 10:34
  • 3
    I think you should learn some more about what a file and file format actually is. Questions like "How to read mp3 as UTF-8 text?" or "How to listen to a PDF in a mp3 player?" have as little sense as "What is the colour of music?". – Bartosz Marcinkowski Jun 12 '14 at 10:39

1 Answers1

0

try to remove ch.setopt(curl.TRANSFERTEXT, True), cause mp3 is binary.

After that change fp = open('audio.txt', "wb") to fp = open('audio.mp3', "wb")
now you'll have correct mp3 saved on disk. Then try use following

import mp3play
filename = r'audio.mp3'
mp3 = mp3play.load(filename)
mp3.play()

you should have installed mp3play package.

Grous
  • 77
  • 1
  • 5