Refactored some codes into modules

This commit is contained in:
Rakan Alhneiti 2016-02-11 20:21:50 +01:00
parent 2ec1f49880
commit d26d561e80
3 changed files with 194 additions and 0 deletions

109
app.py Normal file
View file

@ -0,0 +1,109 @@
import math
import time
from datetime import datetime
from operator import itemgetter
from pocket import Pocket
from progress.spinner import Spinner
from config import Configs
from storage import Storage
class PocketApp:
DEFAULT_WORDS_PER_MINUTE = 180
def __init__(self):
self._configs = Configs()
self._storage = Storage()
self._pocket = Pocket(
self._configs.get('consumer_key'),
self._configs.get('access_token')
)
def get_articles(self, limit=None, order=None):
if self._storage.is_empty():
self.fetch_articles(True)
return self._storage.read(limit, order)
def archive_article(self, item_id):
self._pocket.archive(int(item_id)).commit()
def find_article(self, item_id):
index = self._storage.read()
for article in index:
if str(article['id']) == str(item_id):
return article
return None
def fetch_articles(self, output_progress=False):
spinner = None
if output_progress:
spinner = Spinner('Loading articles ')
articles_index = []
wpm = self._configs.get('words_per_minute')
if not wpm:
wpm = self.DEFAULT_WORDS_PER_MINUTE
last_fetch = self._configs.get('last_fetch')
offset = 0
count = 20
while(True):
articles = self._pocket.retrieve(
state='unread',
count=count,
offset=offset,
since=last_fetch
)
if not articles['list']:
break
for article in articles['list'].values():
word_count = int(article['word_count'])
if word_count == 0:
reading_time = -1
else:
reading_time = math.ceil(word_count / wpm)
title = article['resolved_title']
if not title:
title = article['given_title']
url = article['resolved_url']
if not url:
url = article['given_url']
index = {
'id': article['item_id'],
'title': title,
'url': url,
'word_count': article['word_count'],
'reading_time': reading_time
}
articles_index.append(index)
offset += count
if spinner:
spinner.next()
if spinner:
spinner.finish()
articles_index = sorted(articles_index,
key=itemgetter('reading_time'))
self._storage.write(articles_index)
self._configs.set('last_fetch', self._get_timestamp(datetime.now()))
self._configs.write()
def _get_timestamp(self, date):
return int(time.mktime(date.timetuple()))

33
config.py Normal file
View file

@ -0,0 +1,33 @@
import os
import configparser
class Configs:
_section_name = 'pocket'
def __init__(self):
path = self._get_file_path()
self._config_parser = configparser.ConfigParser()
if not os.path.exists(path):
return
self._config_parser.readfp(open(path))
def get(self, name):
try:
value = self._config_parser.get(self._section_name, name)
except (configparser.NoSectionError, configparser.NoOptionError):
value = None
return value
def set(self, name, value):
self._config_parser.set(self._section_name, name, str(value))
def write(self):
self._config_parser.write(open(self._get_file_path(), 'w'))
def _get_file_path(self):
return '{}/.pocket-time'.format(os.path.expanduser('~'))

52
storage.py Normal file
View file

@ -0,0 +1,52 @@
import csv
import os
class Storage:
def __init__(self):
self._filename = '{}/.pocket-time-index'.format(
os.path.expanduser('~'))
def is_empty(self):
if not os.path.exists(self._filename):
return True
if os.stat(self._filename).st_size == 0:
return True
return False
def write(self, data):
if not data:
return
write_header = False
if self.is_empty():
write_header = True
with open(self._filename, 'a+') as csv_file:
dict_writer = csv.DictWriter(csv_file, data[0].keys())
if write_header:
dict_writer.writeheader()
dict_writer.writerows(data)
def read(self, limit=10, order='asc'):
index = []
row_counter = 0
with open(self._filename, 'r') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
index.append(row)
if order == 'asc':
row_counter += 1
if row_counter == limit:
break
if order == 'desc':
index = index[::-1]
return index[0:limit]