#! /usr/local/bin/python
# -*- coding:utf-8 -*-

print "Content-Type: text/html;charset=UTF-8"
print
print """

<head>
<meta charset="utf-8"/>

	<title>動画</title>
</head>
<body>
<form action="add2.py" method="post">
	<p>部門:
	<li>おもしろ(1)<input type="radio" name="bu" value="1" />
	<li>スーパープレイ(2)<input type="radio" name="bu" value="2" />
	<li>ロビアク(3)<input type="radio" name="bu" value="3" />
	<li>サウンド(4)<input type="radio" name="bu" value="4" />
	<li>解説(5)<input type="radio" name="bu" value="5" /></p>
	<p>ID:<input type="text" name="id" />
	<p>追加:<input type="radio" name="ad" value="add" checked/>
	削除:<input type="radio" name="ad" value="delete"/>
	<p><input type="submit"/>
</form>
</body>
</html>
"""

import sqlite3,requests,cgi
p=cgi.FieldStorage()

if p.getvalue("id")!=None:
	url="http://ext.nicovideo.jp/api/getthumbinfo/sm"
	response = requests.get(url+p.getfirst("id"))

	import xml.etree.ElementTree as ET
	root = ET.fromstring(response.content)
	sss=0
	q=root[0][1].text
	con=sqlite3.connect("data.db")
	cur=con.cursor()
	if p.getvalue("ad")=="add":
		sql="insert into movie (id,bu,title) values (?,?,?)"
		sql_v=(p.getvalue("id"),p.getvalue("bu"),q)
		cur.execute("select * from movie where id=?",[p.getvalue("id")])
		if len(cur.fetchall())!=0:
			sss=1
	
	elif p.getvalue("ad")=="delete":
		sql=u"delete from movie where id=?"
		sql_v=[p.getvalue("id")]
	
	
	
	if p.getvalue("ad")=="delete" or (p.getvalue("ad")=="add" and p.getvalue("bu")!=None and sss==0):
		cur.execute(sql,sql_v)
		con.commit()
		print "success on sm"+p.getvalue("id")
	else:
		print "error"
	con.close()
