Skip to content

Instantly share code, notes, and snippets.

@yackermann
Last active August 29, 2015 14:13
Show Gist options
  • Save yackermann/74ed5d21903c6b9c87e4 to your computer and use it in GitHub Desktop.
Save yackermann/74ed5d21903c6b9c87e4 to your computer and use it in GitHub Desktop.
Script to download flags using mledoze/countries data set.
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 = {}, {}
errors = 'Errors: \n'
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:
errors += data[key]['cca3'].lower() + ' ' + data[key]['name']['common'] + '\n' #Saves list of errors in loading flags
print("Shit Fuck: ", '\s\s\s', data[key]['cca3'].lower(), '\s\s\s', data[key]['name']['common'],'\n')
print('\n\n',errors) #print errors
with open('errors_flags.txt','w') as w:
w.write(errors) #writes errors to file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment