1#! /usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3
4import urllib, re
5
6class AppURLopener(urllib.FancyURLopener):
7    version = "Mozilla/5.0"
8urllib._urlopener = AppURLopener()
9
10def suche( source , country, code ):
11	for line in source:
12		result = re.match ( '(.*)(upload.wikimedia)(.*)'+ country +'(.*)', line )
13		if result > -1 :
14			svgurl = line[ line.find("http") : line.find(".svg") + 4 ].strip()
15			if svgurl.find('thumb') == -1 and len(svgurl) > 3 :
16				svgsource = urllib.urlopen(svgurl)
17				data = svgsource.read()
18				svgsource.close()
19
20				out_file = open('flag_' + code.strip().lower() + '.svg','w')
21				out_file.write(data)
22				out_file.close()
23				print svgurl
24				break
25
26
27
28isourlstring = 'http://www.iso.org/' + 'iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1-semic.txt'
29isosource = urllib.urlopen(isourlstring).readlines()
30
31for line in isosource:
32	if len(line) < 80 and len(line) > 5 :
33		print line
34		linelist = line.split(';')
35		linelist[0] = linelist[0].replace("KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF","NORTH KOREA")
36		linelist[0] = linelist[0].replace("KOREA, REPUBLIC OF","SOUTH KOREA")
37		linelist[0] = linelist[0].replace("CONGO","REPUBLIC OF THE CONGO")
38		linelist[0] = linelist[0].replace("REPUBLIC OF THE CONGO, THE DEMOCRATIC REPUBLIC OF THE","DEMOCRATIC REPUBLIC OF THE CONGO")
39		linelist[0] = linelist[0].replace("KOREA, REPUBLIC OF","SOUTH KOREA")
40		linelist[0] = linelist[0].replace('VIRGIN ISLANDS, BRITISH','BRITISH VIRGIN ISLANDS')
41		linelist[0] = linelist[0].replace('VIRGIN ISLANDS, U.S.','UNITED STATES VIRGIN ISLANDS')
42
43		linelist[0] = linelist[0].split(',')[0].rstrip()
44		linelist[0] = linelist[0].split('(')[0].rstrip()
45
46		namelist = linelist[0].split(' ')
47		fullname = ""
48		for word in namelist:
49			if fullname != "" :
50				fullname = fullname + "_"
51			if word == 'AND' or word == 'THE' or word == 'OF' or word.find('D\'') > -1:
52				word = word.lower()
53			else :
54				word = word.capitalize()
55			if word.find('\'') > -1 :
56				word = word.split('\'')[0] + '%27' + word.split('\'')[1].capitalize()
57			if word.find('-') > -1 :
58				word = word.split('-')[0] + '-' + word.split('-')[1].capitalize()
59			fullname = fullname + word
60
61		fullname.strip()
62		if fullname.find('Islands') > -1 or fullname.find('United') > -1 or fullname.find('Antilles') > -1  or fullname.find('Seychelles') > -1 or fullname.find('Philippines') > -1 or fullname.find('Republic') > -1 or fullname.find('Bahamas') > -1 or fullname.find('Territory') > -1  or fullname.find('Comoros') > -1 or fullname.find('Netherlands') > -1 or fullname.find('Isle') > -1:
63			fullname = 'the_' + fullname
64
65		if fullname.find("land_Islands") > -1 :
66			fullname ='Aaland'
67
68		fullname = fullname.replace('Timor-Leste','East_Timor')
69		fullname = fullname.replace('the_Syrian_Arab_Republic','Syria')
70		fullname = fullname.replace('Svalbard_and_Jan_Mayen','Norway')
71		fullname = fullname.replace('Saint_Pierre','Saint-Pierre')
72		fullname = fullname.replace('Russian_Federation','Russia')
73		fullname = fullname.replace('Libyan_Arab_Jamahiriya','Libya')
74		fullname = fullname.replace('the_Lao_People\'S_Democratic_Republic','Laos')
75		fullname = fullname.replace('Holy_See','')
76		fullname = fullname.replace('the_Heard_Island_and_Mcdonald_Islands','Australia')
77		fullname = fullname.replace('French_Southern_Territories','France')
78		fullname = fullname.replace('Mayotte','France')
79		fullname = fullname.replace('Guadeloupe','France')
80		fullname = fullname.replace('Reunion','France')
81		fullname = fullname.replace('Gambia','The_Gambia')
82		fullname = fullname.replace('Tokelau','New_Zealand')
83		fullname = fullname.replace('Taiwan','the_Republic_of_China')
84		fullname = fullname.replace('Viet_Nam','Vietnam')
85		fullname = fullname.replace('French_Guiana','France')
86		fullname = fullname.replace('Brunei_Darussalam','Brunei')
87		fullname = fullname.replace('Pitcairn','the_Pitcairn_Islands')
88		fullname = fullname.replace('Macao','Macau')
89		fullname = fullname.replace('Bouvet_Island','Norway')
90		fullname = fullname.replace('the_Palestinian_Territory','Palestine')
91		fullname = fullname.replace('the_United_States_Minor_Outlying_Islands','the_United_States')
92		fullname = fullname.replace('the_South_Georgia_and_the_South_Sandwich_Islands','South_Georgia_and_the_South_Sandwich_Islands')
93		fullname = fullname.replace('Cocos','the_Cocos_%28Keeling%29_Islands')
94
95		wpurlstring = 'http://de.wikipedia.org/wiki/Bild:Flag_of_'.strip() + fullname.strip() + '.svg'.strip()
96
97		wpsource = urllib.urlopen(wpurlstring).readlines()
98		if fullname !='' :
99			print wpurlstring
100			suche( wpsource, fullname, linelist[1] )
101