class BatchMovesClient(object): def __init__(self, client_id, client_secret, listening_port, token_cache): self.moves = MovesClient(client_id, client_secret) self.listening_port = listening_port self.client_id = client_id self.client_secret = client_secret self.token_cache_file = token_cache self.load_token() self.moves.access_token = self.token def load_token(self): try: self.token = open(self.token_cache_file).read().strip() except IOError: self.token = None else: try: self.moves.user_profile(access_token=self.token) except OSError: self.token = None os.unlink(self.token_cache_file) if self.token is None: self.token_receiver = TokenReceiver(self.listening_port, self.moves, self.client_secret) thread.start_new_thread(self.token_receiver.run, ()) self.token = self.token_receiver.wait_for_token() open(self.token_cache_file, 'w').write(self.token) def __getattr__(self, name): return getattr(self.moves, name)
def daily_places_report(today=None, month=None): if month is None: if today is None: today = datetime.date.today() month = today.strftime('%Y%m') moves = MovesClient() api_path = 'user/places/daily/%s' % month resp = moves.api(api_path, 'GET', params={'access_token': secrets.moves['access_token']}) stats_defs = { 'work': { 'place_names': {'Octopart.com'}, 'stats': {'hours', 'arrive_morning', 'depart_evening'} }, 'home': { 'place_names': {'Home'}, 'stats': {'depart_morning'} }, 'gym': { 'place_names': { 'Blink Fitness Williamsburg', 'Blink Fitness Chelsea', 'Blink Fitness Bushwick', 'Blink Fitness Gates'}, 'stats': {'hours'} }, } stats = {} for day_dict in resp.json(): (date, day_stats) = extract_daily_place_stats(day_dict, stats_defs) if date and day_stats: stats[date] = day_stats return stats
def __init__(self, client_id, client_secret, listening_port, token_cache): self.moves = MovesClient(client_id, client_secret) self.listening_port = listening_port self.client_id = client_id self.client_secret = client_secret self.token_cache_file = token_cache self.load_token() self.moves.access_token = self.token
def main(): # data from https://dev.moves-app.com/apps client_id = '' client_secret = '' refresh_token = '' api = MovesClient( client_id=client_id, client_secret=client_secret ) access_token, refresh_token = api.refresh_oauth_token(refresh_token) print(access_token, refresh_token) api.access_token = access_token info = api.tokeninfo() print(info)
def daily_places_report(today=None, month=None): if month is None: if today is None: today = datetime.date.today() month = today.strftime('%Y%m') moves = MovesClient() api_path = 'user/places/daily/%s' % month resp = moves.api(api_path, 'GET', params={'access_token': secrets.moves['access_token']}) stats_defs = { 'work': { 'place_names': {'Octopart.com'}, 'stats': {'hours', 'arrive_morning', 'depart_evening'} }, 'home': { 'place_names': {'Home'}, 'stats': {'depart_morning'} }, 'gym': { 'place_names': { 'Blink Fitness Williamsburg', 'Blink Fitness Chelsea', 'Blink Fitness Bushwick', 'Blink Fitness Gates' }, 'stats': {'hours'} }, } stats = {} for day_dict in resp.json(): (date, day_stats) = extract_daily_place_stats(day_dict, stats_defs) if date and day_stats: stats[date] = day_stats return stats
from flask import Flask, url_for, request, session, redirect from moves import MovesClient from datetime import datetime, timedelta import _keys app = Flask(__name__) Moves = MovesClient(_keys.client_id, _keys.client_secret) @app.route("/") def index(): if 'token' not in session: oauth_return_url = url_for('oauth_return', _external=True) auth_url = Moves.build_oauth_url(oauth_return_url) return 'Authorize this application: <a href="%s">%s</a>' % \ (auth_url, auth_url) return redirect(url_for('show_info')) @app.route("/oauth_return") def oauth_return(): error = request.values.get('error', None) if error is not None: return error oauth_return_url = url_for('oauth_return', _external=True) code = request.args.get("code") token = Moves.get_oauth_token(code, redirect_uri=oauth_return_url) session['token'] = token return redirect(url_for('show_info'))
from calendar import monthrange from datetime import date, datetime, timedelta from functools import wraps from json import dumps import memcache from dateutil.relativedelta import relativedelta from flask import Flask, Response, redirect, render_template, request, session, url_for from moves import MovesClient, MovesAPIError app = Flask(__name__) app.secret_key = os.environ['app_secret'] client_id = os.environ['client_id'] client_secret = os.environ['client_secret'] Moves = MovesClient(client_id, client_secret) mc = memcache.Client(['127.0.0.1:11211'], debug=0) logging.basicConfig() logging.StreamHandler(sys.stdout) logger = logging.getLogger('main') logger.setLevel(logging.DEBUG) ### decorator def require_token(func): @wraps(func) def decorated(*args, **kwargs): if 'token' not in session: return redirect(url_for('index')) return func(*args, **kwargs)
- will download the relevant data for July 2013 and store in outfile.json ''' from moves import MovesClient from config import MOVES_CLIENT_ID, MOVES_CLIENT_SECRET, MOVES_ACCESS_TOKEN from config import MOVES_FIRST_DATE, MOVES_BACKLOG_WINDOW from datetime import date, timedelta import time import sys from calendar import monthrange import json yr_mo_str = sys.argv[1] outfile = sys.argv[2] moves = MovesClient(MOVES_CLIENT_ID, MOVES_CLIENT_SECRET) moves.access_token = MOVES_ACCESS_TOKEN [dl_yr, dl_mo] = [int(d) for d in yr_mo_str.split('-')] dl_maxdy = monthrange(dl_yr, dl_mo)[1] start_date = max(MOVES_FIRST_DATE, date(dl_yr, dl_mo, 1)) end_date = min(date(dl_yr, dl_mo, dl_maxdy), date.today()) # end_date = date.today() - timedelta(days=1) moves_data = [] dl_date = start_date # Storyline download print 'Downloading Moves storyline for %s' % yr_mo_str
def preloop(self): self.mc = MovesClient()