#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import q20
import re
import urllib2

start_prefix = re.compile(u'{{基礎情報')
data_prefix = re.compile(r'^\|')
end_prefix = re.compile(r'}}')
markup_prefix = re.compile(r'(\'\'|\'\'\'|\'\'\'\'\')')
link_prefix = re.compile(r'(\[|\])')
stab_prefix = re.compile(r'({|})')

flag = 0
basic_dict = {}
mediawikiAPI = 'https://www.mediawiki.org/w/api.php'
topic = q20.british_find().replace('<br/>\n', '<br/>')

for t in q20.british_find().split('\n'):
    if start_prefix.match(t):
        flag = 1
        continue

    if flag == 1:
        if data_prefix.match(t):
            tlist = t.split(' = ')
            key = data_prefix.sub('', tlist[0])
            value = markup_prefix.sub('', tlist[1])
            value = link_prefix.sub('', value)
            value = stab_prefix.sub('', value)
            basic_dict.update({key:value})
        if end_prefix.match(t):
            flag = 0

image = basic_dict[u'国旗画像'].replace(' ', '%20')
URL = mediawikiAPI + '?action=query&titles=File:' + image + '&prop=imageinfo&format=json'
response = urllib2.urlopen(URL)
print json.dumps(response.read())
