Skip to content

Conversation

@candc-admin
Copy link
Contributor

@candc-admin candc-admin commented Aug 19, 2021

Pull Request Template

AutoCalender -

**from future import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from task import Task
from datetime import timedelta, datetime

categories = {
# Lavender
'#a4bdfc': '',
# Blueberry
'#5484ed': '',
# Peacock
'#46d6db': 'Exercise',
# Sage
'#7ae7bf': 'My Apps',
# Basil
'#51b749': 'App',
# Tangerine
'#ffb878': '',
# Banana
'#fbd75b': '',
# Flamingo
'#ff887c': '',
# Tomato
'#dc2127': 'YouTube',
# Mandarine
'#fa573c': '',
# Grape
'#dbadff': 'Work',
# Graphite
'#e1e1e1': 'School'
}

EVENTS_TO_LOOK_THROUGH = 60

If modifying these scopes, delete the file token.pickle.

SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']

def main():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)

service = build('calendar', 'v3', credentials=creds)

# Call the Calendar API
start_day = datetime.utcnow()
now = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
last_monday = (datetime.utcnow()- timedelta(start_day.weekday())).isoformat() + 'Z' # 'Z' indicates UTC time
week_end_time = str(datetime.utcnow() + timedelta(days=7)) + 'Z'
print('Getting the upcoming 10 events')
print('**************************************************************\n')
events_result = service.events().list(calendarId='primary', timeMin=last_monday, timeMax=now, 
                                    maxResults=EVENTS_TO_LOOK_THROUGH, singleEvents=True,
                                    orderBy='startTime').execute()
colors = service.colors().get(fields='event').execute()
defaultColor = (service.calendarList().get(calendarId="primary").execute())['backgroundColor']
events = events_result.get('items', [])
tasks = []

if not events:
    print('No upcoming events found.')
for event in events:
    start_string = event['start'].get('dateTime', event['start'].get('date'))
    end_string = event['end'].get('dateTime', event['end'].get('date'))
    
    name = event['summary']
    try: 
        color = colors['event'][event['colorId']]['background']
    except KeyError:
        color = defaultColor
    task = Task(name, parse_time(start_string), parse_time(end_string), color)
    task.date = parse_time(start_string)
    task.get_time_of_task()
    tasks.append(task)

total_tasks = []
for color, category in categories.items():
    if len(category) > 1: 
        total_time = timedelta(hours=0)
        for task in tasks:
            if task.color == color:
                total_time += task.total_time
        text = "For " + category + " you have planned to spend:"
        number_of_spaces = 15
        number_of_spaces -= len(category)
        string_length=len(text)+number_of_spaces    # will be adding 10 extra spaces
        string_revised=text.ljust(string_length)
        print("\n-----------------------------------------------------------------")
        print(string_revised + format_timedelta(total_time) + "hrs this week")
print("\n-----------------------------------------------------------------\n")
print("\n********************************************************\n")

def parse_time(timestamp):
# Takes a timestamp string and returns a datetime object
try:
time_object = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z")
except ValueError:
# If no start and end time is specified the format string must be different
time_object = datetime.strptime(timestamp, "%Y-%m-%d")
return time_object

def format_timedelta(timedelta):
# Takes a timedelta and returns a string
hours = timedelta.total_seconds() / 3600
return("%.2f" % hours)

if name == 'main':
main()
**

Issue no.(must) -

Self Check(Tick After Making pull Request)

  • This issue was assigned to me.
  • One Change in one Pull Request
  • My file is in proper folder (Name of folder should be in lowercase with no space in between)
  • I am following clean code and Documentation and my code is well linted with flake8.
  • I have added README.md and requirements.txt with my script

If issue was not assigned to you Please don't make a PR. It will marked as invalid.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulations!! 🎉 @candc-admin for making your first PR. We will review the changes soon and merge finally.😊 Do give a star ⭐ meanwhile if you like this project.

@pawangeek
Copy link
Contributor

Hey @candc-admin , you haven't added the script in PR

@candc-admin
Copy link
Contributor Author

Hey @pawangeek sorry this is my first time doing this have worked generally projects ..not using github or community im learning and doing.
i would appreciate help from if possible
Thanks

@pawangeek
Copy link
Contributor

No problem @candc-admin buddy, This repo is for learning and kickstart your journey in open source, Basically, you have to clone this repo, add a folder containing your script and send a PR to merge. Check out youtube for a detailed approach. Hope this helps :)

@pawangeek pawangeek added spam Just spam ones and removed spam Just spam ones labels Aug 19, 2021
@candc-admin
Copy link
Contributor Author

Thank you it helps a lot ..I'll do it now

@pawangeek pawangeek linked an issue Aug 19, 2021 that may be closed by this pull request
1 task
@pawangeek pawangeek added the invalid This doesn't seem right label Sep 29, 2021
@pawangeek pawangeek removed a link to an issue Sep 29, 2021
1 task
@pawangeek pawangeek closed this Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

invalid This doesn't seem right

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants