Skip to content

Instantly share code, notes, and snippets.

@hisinsub
Last active August 29, 2015 14:26
Show Gist options
  • Save hisinsub/ceeddf6d48cad5e1543c to your computer and use it in GitHub Desktop.
Save hisinsub/ceeddf6d48cad5e1543c to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import hashlib
import urllib2
import xml.dom.minidom as xml
ALSONG_URL = "http://lyrics.alsong.net/alsongwebservice/service1.asmx"
ALSONG_TMPL = '''\
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="ALSongWebServer/Service1Soap" xmlns:ns1="ALSongWebServer" xmlns:ns3="ALSongWebServer/Service1Soap12">
<SOAP-ENV:Body>
<ns1:GetLyric5>
<ns1:stQuery>
<ns1:strChecksum>%s</ns1:strChecksum>
<ns1:strVersion>2.2</ns1:strVersion>
<ns1:strMACAddress />
<ns1:strIPAddress />
</ns1:stQuery>
</ns1:GetLyric5>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''
file = u"임재범 - 사랑이라서.mp3"
# 이 구간이 문제인데요...
f = open(file, 'rb')
data = f.read(160*1024)
# 첫 160kb 를 어떻게 읽어드리는 지를 잘 모르겠습니다.
m = hashlib.md5()
m.update(data)
strChecksum = m.hexdigest()
print strChecksum
# Make a request string
headers = { 'Content-Type': 'text/xml; charset=utf-8' }
request = urllib2.Request(ALSONG_URL, ALSONG_TMPL % key, headers)
# Print Alsong's response
response = urllib2.urlopen(request)
print response.read()
@sunghwan2789
Copy link

블로그 글의 참고 링크를 보면, "MP3 파일의 경우 strChecksum 값은 ID3태그등을 제외한 순수 MP3 음악 데이터를 앞에서부터 163840 바이트 읽어서 MD5로 돌린 값입니다"라고 합니다.
ID3 태그 등 MP3의 순수 음악 데이터 앞에 오는 메타 데이터 부분을 fseek 등으로 스킵하고, 그 뒤 160KB의 MD5 해시 문자열을 사용해야 합니다.

밑 리포는 이를 C#으로 작성하여 올린 곳 입니다.. 좋은 코드는 아니지만,, 한번 둘러보세요~@
https://github.com/sunghwan2789/osu-Lyrics/find/master/
소스 중에서 Audio로 시작하는 것들이 해당 기능을 합니다.
RawPosition이라는 멤버 변수로 순수 음악 데이터의 위치를 저장했습니다..

@hisinsub
Copy link
Author

hisinsub commented Aug 5, 2015

답변 너무 감사드립니다.

네, 사실 그걸 파이썬으로 어떻게 하는지 몰라서 검색해봤는데, 잘 안나오는거 같아요. 제일 가능성있는 글이 요건데요, http://stackoverflow.com/questions/16634128/how-to-extract-the-raw-data-from-a-mp3-file-using-python

여기의 답변과 같이 pydub 이라는 라이브러리를 써서 ._data 를 읽어드리면 될거 같은데 (맞을까요?), 요 방법도 예전에 한번 해봤는데 안됐거든요. 다시 한번 해봐야겠네요. 안되면, C#을 배워봐야겠네요.. 아직은 파이썬이랑 자바스크립트밖에 모르거든요, 흑..

@sunghwan2789
Copy link

일단 ID3가 뭔가, 구조가 어떻게 되나, 공부하면 이해가 빠를듯,,ㅋ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment