There exists a public Google calendar whose calendar ID I have. I would like to set a start and end date and then get all the events between these dates in a CSV file. What is the most efficient way to do this in Python?
The closest solution I have found is https://stackoverflow.com/a/27213635/1936752 which yields a json file and does not have the filtering by date. I could do this with the json file and writing some code to filter only the dates I want and then export to csv, but I guess there is a smarter way?
The manual way of doing what I want is to download the ics file using the "Export Calendar" function and then using an ics to csv converted like https://openicsfile.com/csv-convert.html. I can then filter easily the dates I want. I wish to do exactly this but using a Python script.
I believe your goal is as follows.
You want to retrieve the events from a publicly shared Google Calendar.
You want to retrieve the event title and the date as a CSV file.
You want to achieve this using python.
In this case, how about the following sample script?
Sample script:
In this sample script, the event list is retrieved using "Events: list" of Calendar API with API key. So, please retrieve your API key. Ref And, please enable Calendar API at the API console.
And, please set the variables of the following sample script.
import csv
from googleapiclient.discovery import build
api_key = "###" # Please set your API key.
calendar_id = "###" # Please set the calendar ID.
start = "2022-10-01T00:00:00Z" # Please set the start date you want to search.
end = "2022-10-31T00:00:00Z" # Please set the end date you want to search.
# Retrieve event list using googleapis for python.
service = build("calendar", "v3", developerKey=api_key)
events_result = service.events().list(calendarId=calendar_id, timeMin=start, timeMax=end, fields="items(summary,start,end)", timeZone="UTC").execute()
# Retrieve the event title and date from all-day events.
allDayEvents = [["Event title", "Date"], *[[e.get("summary"), e.get("start").get("date")] for e in events_result.get("items", []) if e.get("start").get("date") and e.get("end").get("date")]]
# Output the retrieved values as a CSV file.
with open("sample.csv", "w") as f:
writer = csv.writer(f, lineterminator="\n")
writer.writerows(allDayEvents)
When this script is run, the event list is retrieved from the publicly shared Google Calendar using an API key. And, a CSV file is created by including the event title and the date. In this sample, the all-day events from October 1, 2022 to October 31, 2022 are retrieved.
You can see about the googleapis for python at here and here.
References:
Authenticate using API keys
Events: list
Related
I have this python code where "Employee" is a variable.
Whenever I search for a company in my first JSON file, it gives me the responsible employee and assign it's name to "EMPLOYEE" variable.
In another JSON file, i have a list of the employees with their address and emails.
What I want is, whenever an employee is fetched using the first JSON file, I would want the second file to get on board, and pulls his email + home address.
Context:
Context: A company wants an appointment, so we check who's free to assign this company a calendar slot.
Users Job : insert a date, time, and company name.
Purpose of the code: each company in my file has 3 assigned employees to it, by order (1st JSON file). The code will check the first one, then check in my google calendar if he's busy, if he is, it will check the second one...and so on.
# Reads the json data
with open('convertcsv.json') as json_file:
data = json.load(json_file)
employeesChosen = []
event_email = 'abc#abc.com'
event_start = '2020-05-9T13:00:00'
event_end = '2020-05-09T15:00:00'
employeeInsert = False
# Adds all the current employees for the company picked
for i in range(len(data)):
if data[i]['name_enterprise'] == event_fabricant:
employeesChosen.append(data[i]['employee1'])
employeesChosen.append(data[i]['employee2'])
employeesChosen.append(data[i]['employee3'])
location = data[i]['location']
print("Employees found")
break
If you want a data change trigger some other action, you'll need command/script. If you have them in a git repo, you could use a kind of git-hooks. Or any cron jobs to check the content of the first file and do something out of the second file (e.g. getting the email and address).
Is there a way to refresh Tableau embedded datasource using python. I am currently using Tableau server client library to refresh published datasources which is actually working fine. Can someone help me to figure out a way?
The way you can reach them is kinda annoying from my perpective.
You need to use populate_connections() function to load embedded datasources. It would be easier if you know the name of the workbook.
import tableauserverclient as TSC
#sign in using personal access token
server = TSC.Server(server_address='server_name', use_server_version=True)
server.auth.sign_in_with_personal_access_token(auth_req=TSC.PersonalAccessTokenAuth(token_name='tokenName', personal_access_token='tokenValue', site_id='site_name'))
#use RequestOptions() with a filter to pull an specific workbook
def get_workbook(name):
req_opt = TSC.RequestOptions()
req_opt.filter.add(TSC.Filter(req_opt.Field.Name, req_opt.Operator.Equals, name))
return server.workbooks.get(req_opt)[0][0] #workbooks.get () function is intended to return a list items that you can iterate, but here we are assuming it will be find only one result
workbook = get_workbook(name='workbook_name') #gets the workbook
server.workbooks.populate_connections(workbook) #this function will load all the embedded datasources in the workbook
for datasource in workbook.connections: #iterate in datasource list
#Note: each element of this list is not an TSC.DatabaseItem, so, you will need to load a valid one using the "datasource_id" attribute from the element.
#If you try server.datasources.refresh(datasource) it will fail
ds = server.datasources.get_by_id(datasource.datasource_id) #loads a valid TSC.DatabaseItem
server.datasources.refresh(ds) #finally, you will be able to refresh it
...
The best practice is do not embeddeding datasources but publish them independently.
Update:
There is an easy way to achieve this. There are two types of extract tasks, Workbook and Data source. So, for embedded data sources, you need to perform a workbook refresh.
workbook = get_workbook(name='workbook_name')
server.workbooks.refresh(workbook.id)
You can use "tableauserverclient" Python package. You can pip install it from PyPy.
After installing it, you can consult the docs.
I will attach an example I used some time ago:
import tableauserverclient as TSC
tableau_auth = TSC.TableauAuth('user', 'pass', 'homepage')
server = TSC.Server('server')
with server.auth.sign_in(tableau_auth):
all_datasources, pagination_item = server.datasources.get()
print("\nThere are {} datasources on
site:".format(pagination_item.total_available))
print([datasource.name for datasource in all_datasources])
I would like to create app, that will show lately updated repository at somebody's (organisation) GitHub account.
I tried PyGitHub, I tried json in many ways (with parameters, different iterations, relating to keys) but with no result.
Can somebody help?
from github import Github
import requests
import json
parameters = {"sort": 'pushed'}
r = requests.get("https://api.github.com/users/:github_user_name:/repos.json", params=parameters)
resp = r.json()
for item in resp['updated_at']:
print(item['updated_at'])
You can call the updated_at method on a repository and store the date for a comparison the next time you check if the repo was updated.
Getting the date of the last update:
from github import Github
g = Github('username', 'password')
repo = g.get_repo('CouchPotato/CouchPotatoServer') # full repo name or id
date = repo.updated_at
date = 2017-05-02 13:48:58
then you just need to:
1. store the date associated with the repository
2. call the function once every X hours or whatever interval you choose.
3. then compare the stored date with the new date.
I need to pull information on a long list of JIRA issues that live in a CSV file. I'm using the JIRA REST API in Python in a small script to see what kind of data I can expect to retrieve:
#!/usr/bin/python
import csv
import sys
from jira.client import JIRA
*...redacted*
csvfile = list(csv.reader(open(sys.argv[1])))
for row in csvfile:
r = str(row).strip("'[]'")
i = jira.issue(r)
print i.id,i.fields.summary,i.fields.fixVersions,i.fields.resolution,i.fields.resolutiondate
The ID (Key), Summary, and Resolution dates are human-readable as expected. The fixVersions and Resolution fields are resources as follows:
[<jira.resources.Version object at 0x105096b11>], <jira.resources.Resolution object at 0x105096d91>
How do I use the API to get the set of available fixVersions and Resolutions, so that I can populate this correctly in my output CSV?
I understand how JIRA stores these values, but the documentation on the jira-python code doesn't explain how to harness it to grab those base values. I'd be happy to just snag the available fixVersion and Resolution values globally, but the resource info I receive doesn't map to them in an obvious way.
You can use fixVersion.name and resolution.name to get the string versions of those values.
User mdoar answered this question in his comment:
How about using version.name and resolution.name?
I have used the gspread library to read a CSV file from Google docs but it first requires me to log in.
gc = gspread.login('email','password')
sheetData = gc.open("NSEport").sheet1
I want to directly open a spreadsheet using the key generated when we shared the spreadsheet, without logging in to a Google account.
full_doc = gc.open("NSEport")
list_of_worksheets = full_doc.worksheets()
one_I_want = full_doc.get_worksheet(0) # gets first worksheet