How to Get National Holidays of Selected Country

How to get national holidays of selected country

Problem solved by using Google Calendar API V3. The idea I found from this post.
The holiday can get from default holiday calendar of google.

The ID list of default holiday calendar can be found here, support to 40 country.

A piece of code that handle permission and get holiday list:-

com.google.api.services.calendar.Calendar client = null;
credential = GoogleAccountCredential.usingOAuth2(mContext, CalendarScopes.CALENDAR);
credential.setSelectedAccountName(mList.get(0));
client = getCalendarService(credential);
do {
com.google.api.services.calendar.model.Events events;
events = client.events().list("en.usa#holiday@group.v.calendar.google.com").setPageToken(pageToken).execute();
onHolidayChecked(events.getItems()); //result return here (events.getItems())
pageToken = events.getNextPageToken();
} while (pageToken != null);

private com.google.api.services.calendar.Calendar getCalendarService(GoogleAccountCredential credential) {
return new com.google.api.services.calendar.Calendar.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
}

Get Holidays List of a Country from Google Calendar API

Yes, using Google API you can do that.

  • Create an API app in the google developer account

  • From the "Credentials" tab you can create an API key, you get
    something like this AIzaSyBcOT_DpEQysiwFmmmZXupKpnrOdJYAhhM

  • Then, you can access holidays calendar using this URL

Python: Return list of public holidays for a specific country

Maybe try installing via anaconda?
https://docs.conda.io/en/latest/miniconda.html

First create a conda environment (named py37 below, name it how you wish), then perform the installation within the environment and execute the script. There may be some weird thing in your base python installation causing the library to not work.

conda create --name py37 python=3.7 
conda activate py37
conda install -c conda-forge holidays

After this, run the code just as below. If you're using a text editor, you'll need to open that editor from the environment. Or just run from a python kernel through the command line.

Getting JSON with list of national holidays Google Calendar

Here's a simple example with a handful of the public holidays calendars:

$("#selectCountry").change(function(e) {  $("#output").html("Loading...");  var country = $("#selectCountry").val();  var calendarUrl = 'https://www.googleapis.com/calendar/v3/calendars/en.' + country                   + '%23holiday%40group.v.calendar.google.com/events?key=<yourAPIKey>';
$.getJSON(calendarUrl) .success(function(data) { console.log(data); $("#output").empty(); for (item in data.items) { $("#output").append( "<hr><h3>" + data.items[item].summary + "<h3>" + "<h4>" + data.items[item].start.date + "<h4>" ); } }) .error(function(error) { $("#output").html("An error occurred."); })});$("#selectCountry").trigger("change");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body> <select id="selectCountry"> <option value="usa">USA</option> <option value="uk">UK</option> <option value="bm">Bermuda</option> <option value="swedish">Sweden</option> </select> <pre id="output"></pre> <script type="text/javascript"> </script></body>

How can i list all holidays' date for each country using holidays module in python?

Firstly: pip show holidays
and then copy the location into the locationVariable

import holidays
import os
locationVariable = ""
countries = [name.split(".py")[0].replace("_"," ") for name in os.listdir(locationVariable+"holidays/countries/") if name.endswith(".py") and not name.startswith("__")]

print("List of countries are : " + str(len(countries)))

for i in countries:
for j in range(2010,2021):
try:
print(getattr(holidays,i.title().replace(" ",""))(years=j).keys())
except:
print("unable to iterate "+i.title().replace(" ",""))
break

seems like the formatting of the name is inconsistent(for Hongkong), please and use.

Make sure to mark is answered if helpfull.



Related Topics



Leave a reply



Submit