baqarhussain commited on
Commit
bdfb99d
·
verified ·
1 Parent(s): 864b54f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import socket
3
  import requests
 
4
  from urllib.parse import urlparse
5
 
6
  # Function to extract the domain name from the URL
@@ -32,13 +33,17 @@ def get_country_info(ip):
32
 
33
  # Function to get the flag URL from a country code
34
  def get_country_flag(country_name):
35
- country_flags = {
36
- "US": "https://countryflagsapi.com/png/us",
37
- "IN": "https://countryflagsapi.com/png/in",
38
- "GB": "https://countryflagsapi.com/png/gb",
39
- # Add more country codes and flags as needed
40
- }
41
- return country_flags.get(country_name.upper(), "https://countryflagsapi.com/png/unknown")
 
 
 
 
42
 
43
  # Streamlit app
44
  def main():
 
1
  import streamlit as st
2
  import socket
3
  import requests
4
+ import pycountry
5
  from urllib.parse import urlparse
6
 
7
  # Function to extract the domain name from the URL
 
33
 
34
  # Function to get the flag URL from a country code
35
  def get_country_flag(country_name):
36
+ try:
37
+ # Using pycountry to get the alpha_2 country code (e.g., "US", "IN")
38
+ country = pycountry.countries.get(name=country_name)
39
+ if country:
40
+ country_code = country.alpha_2
41
+ flag_url = f"https://countryflagsapi.com/png/{country_code.lower()}"
42
+ return flag_url
43
+ else:
44
+ return "https://countryflagsapi.com/png/unknown" # Default flag if country not found
45
+ except AttributeError:
46
+ return "https://countryflagsapi.com/png/unknown" # Return unknown flag if error
47
 
48
  # Streamlit app
49
  def main():