Created
June 9, 2015 02:37
-
-
Save garettmd/8897dc18418bbb243f53 to your computer and use it in GitHub Desktop.
Full File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import webbrowser | |
import requests as r | |
from flask import Flask, request | |
from flask.ext.classy import FlaskView, route | |
import sys | |
import easygui as eg | |
__name__ = '__main__' | |
class PickitSession(FlaskView): | |
route_base = '/' | |
def __init__(self): | |
self.CONSUMER_KEY = '41815-cd31a16ee9750c7a782f9cde' | |
self.ACCESS_TOKEN = '' | |
self.REDIRECT_URI = 'http://127.0.0.1:5000' | |
self.OAUTH_REQUEST_URL = 'https://getpocket.com/v3/oauth/request' | |
self.AUTH_URL = 'https://getpocket.com/auth/authorize' | |
self.OAUTH_ACCESS_URL = 'https://getpocket.com/v3/oauth/authorize' | |
self.HEADERS = {'Content-Type': 'application/json; charset=UTF-8', 'X-Accept': 'application/json'} | |
self.USERNAME = '' | |
self.GET_URL = 'https://getpocket.com/v3/get' | |
def authenticate(self): | |
data = {'consumer_key': self.CONSUMER_KEY, 'redirect_uri': self.REDIRECT_URI} | |
response = r.post(self.OAUTH_REQUEST_URL, headers=self.HEADERS, data=json.dumps(data)) | |
json_response = json.loads(response.text) | |
self.AUTH_URL = self.AUTH_URL + '?request_token=' + json_response[ | |
'code'] + '&redirect_uri=' + self.REDIRECT_URI + '/auth_redirect/' + json_response['code'] | |
webbrowser.open_new_tab(self.AUTH_URL) | |
@route('/auth_redirect/<code>') | |
def authorize(self, code): | |
# TODO finally redirect page to success.html - Might need to change the whole redirect URI and update the | |
# route to point to /success.html or something like that | |
data = {'consumer_key': self.CONSUMER_KEY, 'code': code} | |
auth_response = r.post(self.OAUTH_ACCESS_URL, headers=self.HEADERS, data=json.dumps(data)) | |
json_response = json.loads(auth_response.text) | |
self.ACCESS_TOKEN = json_response['access_token'] | |
self.USERNAME = json_response['username'] | |
print(self.ACCESS_TOKEN, self.USERNAME) | |
main_menu() | |
shutdown_server() | |
return | |
def view_articles(self): | |
data = {'count': 20, 'detailType': 'simple', 'consumer_key': self.CONSUMER_KEY, | |
'access_token': self.ACCESS_TOKEN} | |
print(data) | |
print(self.USERNAME) | |
articles = r.post(self.GET_URL, headers=self.HEADERS, data=json.dumps(data)) | |
# TODO articlesappend('list next article') | |
# msg = 'Choose \"list next article\" to get the next articles chronologically.' | |
print(articles.text) | |
return articles.text | |
flask_app = Flask(__name__) | |
PickitSession.register(flask_app) | |
new_session = PickitSession() | |
def shutdown_server(): | |
print('shutting down') | |
func = request.environ.get("werkzeug.server.shutdown") | |
if func is None: | |
raise RuntimeError('Not running with the werkzeug server') | |
func() | |
def run_server(): | |
flask_app.run( | |
host='127.0.0.1', | |
port=5000 | |
) | |
def search_articles(): | |
pass | |
def modify_tags(): | |
pass | |
def start_screen(): | |
msg = "Authorize Pickit with Pocket" | |
choices = ['Let\'s go!', 'Quit'] | |
start_choice = eg.ccbox(msg, 'Pickit', choices=choices, default_choice='Let\'s go!') | |
if start_choice: | |
new_session.authenticate() | |
run_server() | |
else: | |
sys.exit(0) | |
def main_menu(): | |
user = new_session.USERNAME | |
msg = 'Select the operation you want to do for %s\'s account' % user | |
choices = ['View articles', 'Search articles', 'Modify tags'] | |
title = 'Pickit - Main Menu' | |
choice = eg.indexbox(msg, title, choices) | |
choice_map = { | |
0: view_articles_screen(), | |
1: search_articles_screen(), | |
2: modify_tags_screen(), | |
} | |
choice_map[choice]() | |
def view_articles_screen(): | |
msg = 'The last 20 articles from your Pocket' | |
articles = new_session.view_articles() | |
title = 'Pickit - View Articles' | |
start_screen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment