Skip to content

Commit

Permalink
Remove URL Shortening [fixes #440] (#443)
Browse files Browse the repository at this point in the history
Google has removed the urlshortening service.  After some discussion,
it was decided to remove the shortening facilities instead of trying to
integrate with a separate provider.  This makes our code simpler and
folks who need url shortening can hopefully layer on what they need.
  • Loading branch information
jcrowgey authored Apr 16, 2019
1 parent 02d6dd1 commit 428378a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 54 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v4.1.0
* Removed url shortening due to Google deprecation #440

v4.0.4
* Minor bugfixes: conky colors, issues with setup.py

Expand Down
21 changes: 5 additions & 16 deletions gcalcli/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
from oauth2client import tools
import copy as _copy

DETAILS = ['all', 'calendar', 'location', 'length', 'reminders', 'description',
'longurl', 'shorturl', 'url', 'attendees', 'email', 'attachments']
DETAILS = ['calendar', 'location', 'length', 'reminders', 'description',
'url', 'attendees', 'email', 'attachments']

BOOL_DETAILS = ['calendar', 'location', 'length', 'reminders', 'description',
'attendees', 'email', 'attachments']

PROGRAM_OPTIONS = {
'--client-id': {'default': gcalcli.__API_CLIENT_ID__,
Expand Down Expand Up @@ -60,18 +58,9 @@ def __call__(self, parser, namespace, value, option_string=None):
details = _copy.copy(getattr(namespace, self.dest, {}))

if value == 'all':
details.update({d: True for d in BOOL_DETAILS})
if 'url' not in details:
# url isn't boolean, but should be included in 'all'
# but we don't want to override this if they specifically ask
# for longurl (ie --details=longurl --details=all should work)
details['url'] = 'short'
elif value in BOOL_DETAILS:
details.update({d: True for d in DETAILS})
else:
details[value] = True
elif value in ['shorturl', 'url']:
details['url'] = 'short'
elif value == 'longurl':
details['url'] = 'long'

setattr(namespace, self.dest, details)

Expand All @@ -95,7 +84,7 @@ def get_details_parser():
details_parser = argparse.ArgumentParser(add_help=False)
details_parser.add_argument(
'--details', default={}, action=DetailsAction,
choices=DETAILS,
choices=DETAILS + ['all'],
help='Which parts to display, can be: ' + ', '.join(DETAILS))
return details_parser

Expand Down
39 changes: 9 additions & 30 deletions gcalcli/gcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class GoogleCalendarInterface:
max_retries = 5
auth_http = None
cal_service = None
url_service = None

ACCESS_OWNER = 'owner'
ACCESS_WRITER = 'writer'
Expand Down Expand Up @@ -140,8 +139,7 @@ def _google_auth(self):
OAuth2WebServerFlow(
client_id=self.options['client_id'],
client_secret=self.options['client_secret'],
scope=['https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/urlshortener'],
scope=['https://www.googleapis.com/auth/calendar'],
user_agent=__program__ + '/' + __version__
),
storage,
Expand All @@ -160,15 +158,6 @@ def get_cal_service(self):

return self.cal_service

def get_url_service(self):
if not self.url_service:
self._google_auth()
self.url_service = build(serviceName='urlshortener',
version='v1',
http=self._google_auth())

return self.url_service

def _get_cached(self):
if self.options['config_folder']:
cache_file = os.path.expanduser(
Expand Down Expand Up @@ -224,16 +213,6 @@ def _get_cached(self):
with open(cache_file, 'wb') as _cache_:
pickle.dump(self.cache, _cache_)

def _shorten_url(self, url):
if self.details.get('url', False) != 'short':
return url
# Note that when authenticated to a google account different shortUrls
# can be returned for the same longUrl. See: http://goo.gl/Ya0A9
shortUrl = self._retry_with_backoff(
self.get_url_service().url().insert(body={'longUrl': url})
)
return shortUrl['id']

def _calendar_color(self, event, override_color=False):
ansi_codes = {
'1': 'brightblue',
Expand Down Expand Up @@ -626,9 +605,9 @@ def _tsv(self, start_datetime, event_list):
_u(event['e'].strftime('%H:%M')))

if self.details.get('url'):
output += '\t%s' % (self._shorten_url(event['htmlLink'])
output += '\t%s' % (event['htmlLink']
if 'htmlLink' in event else '')
output += '\t%s' % (self._shorten_url(event['hangoutLink'])
output += '\t%s' % (event['hangoutLink']
if 'hangoutLink' in event else '')

output += '\t%s' % _u(self._valid_title(event).strip())
Expand Down Expand Up @@ -729,12 +708,12 @@ def _format_descr(descr, indent, box):
self.printer.msg(xstr, 'default')

if self.details.get('url') and 'htmlLink' in event:
hlink = self._shorten_url(event['htmlLink'])
hlink = event['htmlLink']
xstr = '%s Link: %s\n' % (details_indent, hlink)
self.printer.msg(xstr, 'default')

if self.details.get('url') and 'hangoutLink' in event:
hlink = self._shorten_url(event['hangoutLink'])
hlink = event['hangoutLink']
xstr = '%s Hangout Link: %s\n' % (details_indent, hlink)
self.printer.msg(xstr, 'default')

Expand Down Expand Up @@ -1287,7 +1266,7 @@ def QuickAddEvent(self, event_text, reminders=None):
)

if self.details.get('url'):
hlink = self._shorten_url(new_event['htmlLink'])
hlink = new_event['htmlLink']
self.printer.msg('New event added: %s\n' % hlink, 'green')

return new_event
Expand Down Expand Up @@ -1328,7 +1307,7 @@ def AddEvent(self, title, where, start, end, descr, who, reminders, color):
new_event = self._retry_with_backoff(request)

if self.details.get('url'):
hlink = self._shorten_url(new_event['htmlLink'])
hlink = new_event['htmlLink']
self.printer.msg('New event added: %s\n' % hlink, 'green')

return new_event
Expand Down Expand Up @@ -1562,7 +1541,7 @@ def CreateEventFromVOBJ(ve):
body=event
)
)
hlink = self._shorten_url(new_event.get('htmlLink'))
hlink = new_event.get('htmlLink')
self.printer.msg(
'New event added: %s\n' % hlink, 'green'
)
Expand All @@ -1581,7 +1560,7 @@ def CreateEventFromVOBJ(ve):
body=event
)
)
hlink = self._shorten_url(new_event.get('htmlLink'))
hlink = new_event.get('htmlLink')
self.printer.msg('New event added: %s\n' % hlink, 'green')
elif val.lower() == 'q':
sys.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'[email protected]']

setup(name='gcalcli',
version='4.0.4',
version='4.1.0',
author='Eric Davis, Brian Hartvigsen, Joshua Crowgey',
author_email=', '.join(author_emails),
maintainer='Joshua Crowgey',
Expand Down
9 changes: 2 additions & 7 deletions tests/test_argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,11 @@ def test_details_parser():
parsed_details = details_parser.parse_args(argv).details
assert parsed_details['attendees']
assert parsed_details['location']
assert parsed_details['url'] == 'short'
assert parsed_details['url']

argv = shlex.split('--details all')
parsed_details = details_parser.parse_args(argv).details
assert all(parsed_details[d] for d in argparsers.BOOL_DETAILS)

# ensure we can specify url type even with details=all
argv = shlex.split('--details all --details longurl')
parsed_details = details_parser.parse_args(argv).details
assert parsed_details['url'] == 'long'
assert all(parsed_details[d] for d in argparsers.DETAILS)


def test_handle_unparsed():
Expand Down

0 comments on commit 428378a

Please sign in to comment.