Skip to content

Instantly share code, notes, and snippets.

@mledoze
Forked from herrjemand/flag_wiki.py
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mledoze/42dc342503ea533388d1 to your computer and use it in GitHub Desktop.
Save mledoze/42dc342503ea533388d1 to your computer and use it in GitHub Desktop.
from lxml import html #pip install lxml
import requests, time, os.path, json
save_to = 'flags_new' #folder where you want to save. NO / !
temp_data, data = {}, {}
def stripper(c_name):
print('Doing: ', c_name)
page = requests.get('https://en.wikipedia.org/wiki/File:Flag_of_' + c_name + '.svg') #loads the webpage
tree = html.fromstring(page.text) #html to xpath tree
link = ''.join(tree.xpath('//*[@id="file"]/a/img/../@href')) #getting svg links
return link
def save(link,cca3):
with open(save_to + '/' + cca3.lower() + '.svg','w') as w:
w.write(requests.get('https:' + link).text) #saving image
print("Done\n")
time.sleep(1) #one second delay, to ensure that host will not think that we dos attack him
with open('countries.json') as r: #loads the countries data
temp_data = json.loads(r.read())
for i in temp_data: #restructure it from the list to dictionary
data[i['name']['common']] = i
for key in data.keys():
link = ''
for i in ['common','official']:
link = stripper(data[key]['name'][i])
if link:
print(link)
save(link,data[key]['cca3'])
break
else:
print("Shit Fuck: ", '\s\s\s', data[key]['cca3'].lower(), '\s\s\s', data[key]['name']['common'],'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment