In [76]:
#_*_ coding:utf-8 _*_
import urllib.request
import json
import datetime
import time
In [77]:
# file create with header and utf-8 encoding
def movieInfo_header_create(filename):
f = open(filename, mode='w', encoding='utf-8')
header = "movieCd\tprdtYear\tshowTm\tprdtStatNm\ttypeNm\tnations\tgenres\tdirectors\tactors\tshowTypes\tcompanys\taudits\n"
f.write(header)
f.close()
In [90]:
# need api key
def kofic_info_downloader(key, movieCd, filename):
url_ = ("http://www.kobis.or.kr/kobisopenapi/webservice/"
"rest/movie/searchMovieInfo.json?key=%s&movieCd=%s" %
(key, movieCd))
time.sleep(2)
request = urllib.request.Request(url_)
response = urllib.request.urlopen(request)
http_code = response.getcode()
if http_code == 200:
response_body = response.read()
# print(response_body.decode('utf-8'))
movieInfoResults = json.loads(response_body.decode('utf-8'))
# print(boxOfficeResults)
movieInfoResult = movieInfoResults['movieInfoResult']['movieInfo']
nations = []
for n_item in movieInfoResult['nations']:
nations.append(n_item['nationNm'])
genres = []
for g_item in movieInfoResult['genres']:
genres.append(g_item['genreNm'])
directors = []
for d_item in movieInfoResult['directors']:
directors.append(d_item['peopleNm']+"&"+d_item['peopleNmEn'])
actors = []
for a_item in movieInfoResult['actors']:
actors.append(a_item['peopleNm']+"&"+a_item['peopleNmEn']+"&"+
a_item['cast']+"&"+a_item['castEn'])
showTypes = []
for s_item in movieInfoResult['showTypes']:
showTypes.append(s_item['showTypeGroupNm']+"&"+s_item['showTypeNm'])
companys = []
for c_item in movieInfoResult['companys']:
companys.append(c_item['companyCd']+"&"+c_item['companyNm']
+"&"+c_item['companyNmEn']+"&"+c_item['companyPartNm'])
audits = []
for au_item in movieInfoResult['audits']:
audits.append(au_item['auditNo']+"&"+au_item['watchGradeNm'])
movieInfo_one_line = (
u"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (
movieCd, movieInfoResult['prdtYear'], movieInfoResult['showTm'],
movieInfoResult['prdtStatNm'], movieInfoResult['typeNm'],
nations, genres, directors, actors, showTypes, companys, audits))
f = open(filename, mode='a', encoding='utf-8')
f.write(movieInfo_one_line+"\n")
f.close()
else:
print("Error Code:" + http_code + movieCd)
#print("completed")
In [107]:
#Call functions
filename = "./movieInfo.txt"
codes = []
f = open("./movieCd.txt", mode='r', encoding='utf-8')
lines = f.readlines()
for l in lines:
codes.append(l.replace("\n",""))
apikey = ""
In [108]:
#movieInfo_header_create("./movieInfo.txt")
In [109]:
for code in codes:
kofic_info_downloader(apikey, code, filename)
In [73]:
kofic_info_downloader(apikey, '20124079', filename)
In [ ]: