얼마 전, 네이버 블로그 첨부파일을 다운로드 받는 파이썬 코드를 작성했는데요...
자매품으로(?) 티스토리 첨부파일을 받는 코드도 만들어봤습니다. ^^;;;;
Win95/98 용 고전 게임들 찾다보면 첨부파일이 어마어마하게 많은 경우가 있는데...
하나하나 클릭하기 귀찮아서 코딩까지 하게 되었네요~ ^^;;;;
네이버 블로그는 첨부파일 정보가 있는 페이지까지 접근하기가 좀 귀찮을뿐(?)
그 페이지까지 접근만 하면 첨부파일 다운로드는 비교적 쉽게(?) 해결이 됩니다.
"aPostFiles" 라는 자바스크립트 배열이 있고 그 안에 모든 첨부파일의 정보(링크, 파일이름, 크기 등...)가
가지런히 담겨져 있기에 해당 배열의 내용만 잘 가져오면 되거든요 :)
티스토리는 처음에 접근하는 페이지에 첨부파일 정보가 바로 있어서~
첨부파일 정보를 확인하는 건 어렵지 않습니다.
그런데 네이버 블로그처럼 첨부파일 정보가 한 곳에 몰려있는게 아니라...
| <a href="첨부파일 링크"> .... 파일이름</a> |
( 전 그냥 페이지 전체 소스에서 '첨부파일 링크' 를 포함한 태그를 모조리 찾는 방법으로 해결을.. -_-;;;; )
[ GitHub - https://github.com/XeroNicHS/GMF ]
# GMF [File Downloader] for Tistory Blog
import re
import sys
from http import client
from urllib import request
def print_logo():
print("#------------------------------------------#")
print("# [GMF] Give Me a File!! [File Downloader] #")
print("#------------------------------------------#")
print("# for Tistory Blog\n")
def get_url_source(url):
try:
f = request.urlopen(url)
url_info = f.info()
url_charset = client.HTTPMessage.get_charsets(url_info)[0]
url_source = f.read().decode(url_charset)
return url_source
except Exception as e:
print("[-] Error : %s" % e)
sys.exit(-1)
def main():
print_logo()
if len(sys.argv) != 2:
print("[*] Usage : gmf_ti.py [Tistory Blog URL]")
else:
url = sys.argv[1]
print("[*] Target URL : %s\n" % url)
url_source = get_url_source(url)
# find 's1.daumcdn.net/cfs.tistory'
if url_source.find("t1.daumcdn.net/tistory") == -1:
print("[-] It is not a Tistory Blog")
sys.exit(0)
try:
# find all 'attach file link'
p_attach = re.compile(r"href=[\'\"](\S+?/attachment/.*?)[\'\"]\s*.*?/> (.*?)</", re.IGNORECASE | re.DOTALL)
result = p_attach.findall(url_source)
if result:
for each_file in result:
file_url = each_file[0]
if each_file[1] == "":
file_name = file_url[file_url.rfind('/') + 1:]
else:
file_name = each_file[1]
print("* File : %s" % file_name)
print(" Link : %s" % file_url)
request.urlretrieve(file_url, file_name)
print(" ==> Done")
else:
print("[-] Attached File not found !!")
except Exception as e:
print("[-] Error : %s" % e)
sys.exit(-1)
if __name__ == "__main__":
sys.exit(main())
![]() |
| gmf_ti.py 실행 |
티스토리 첨부파일 태그의 모든 형태를 다 확인한게 아니라...
경우에 따라서는 정보를 못가져올 수도 있습니다... @_@;;;;


