def form_valid(self, form): pocket_instance = Pocket(settings.CONSUMER_KEY, self.request.session['access_token']) file = self.request.FILES['file'] for url in file.readlines(): pocket_instance.add(url.decode().rstrip()) # pocket_instance.bulk_add(item_id='', url=url.decode().rstrip()) # pocket_instance.commit() return super(HomeView, self).form_valid(form)
def main(): parsed_args = parse_args() try: global keyring, Pocket, PocketException import keyring from pocket import Pocket, PocketException except ImportError: print_import_error() print_refresh() return consumer_key, access_token = get_secrets() pocket = Pocket(consumer_key=consumer_key, access_token=access_token) if parsed_args.add: new_url = get_input('\"Save an item to Pocket:\"') if new_url: pocket.add(url=new_url) return elif parsed_args.delete: pocket.delete(parsed_args.delete).commit() return elif parsed_args.secrets: update_secrets() return raw_articles = {} if parsed_args.full else get_cache(CACHE_PATH) try: raw_answer = pocket.retrieve(detailType='simple', since=raw_articles.get('since')) except PocketException as e: if e.http_code in (400, 401): print_secrets_error() else: print_error(e) print_refresh() return except Exception as e: print_error(e) print_refresh() return raw_articles = update_from_cache(raw_articles, raw_answer) set_cache(CACHE_PATH, raw_articles) adapted_articles = [Article( id=i.get('item_id'), link=i.get('resolved_url', i.get('given_url')), title=i.get('resolved_title', i.get('given_title')), cmd=CMD ) for i in sorted(raw_articles['list'].values(), key=lambda x: x.get('time_added', ''), reverse=True) if i['status'] == '0'] print(f'{len(adapted_articles)}|font=Verdana size=14 templateImage={pocket_icon()}') print('---') print(*adapted_articles, sep='\n') print('---') print(f'➕ Save a URL|bash={CMD} param1=--add terminal=false refresh=true') print_refresh()
def push_to_pocket(link_id): link = Document.find_one({'user': g.user._id, '_id': ObjectId(link_id)}) if link: user = User.find_one({'_id': link.user}) pocket_instance = Pocket(POCKET_APP_TOKEN, user.credentials['pocket']['token']) pocket_instance.add(link.url, wait=False) link.read = True link.save() return "OK"
def share_group_urls(group_id): headers = {} group = PocketGroup.objects.get(id=group_id) members = group.members.all() for member in members: if member.pocket_access_token: last_article = group.feed.order_by('time_updated').filter(shared_by=member).last() if last_article: last_update = last_article.time_updated else: last_update = int(dateformat.format(now() - datetime.timedelta(days=1), 'U')) last_update += 1 # try: pocket_cli = Pocket(settings.POCKET_CONSUMER_KEY, member.pocket_access_token) response, headers = pocket_cli.get( state='all', tag=group.tag, detailType='complete', sort='oldest', since=last_update ) process_and_add_to_feed(group, member, response) # except: # logger.info('Could not fetch data for %s' % member) feed = group.feed.order_by('time_updated') if group.last_synced_article: feed = feed.filter(id__gt=group.last_synced_article.id) feed = feed.all() for member in members: if member.pocket_access_token: try: pocket_cli = Pocket(settings.POCKET_CONSUMER_KEY, member.pocket_access_token) for article in feed: if article.shared_by != member: logger.info('Sharing: %s - %s' % (member.pocket_username, article.link)) pocket_cli.add( url=article.link, tags='sharereads,' + group.tag + ',' + article.shared_by.pocket_username ) except: pass group.last_synced_article = group.feed.order_by('id').last() group.save() logger.info(headers) if 'x-limit-key-remaining' in headers: logger.info('Remaining API calls (for the current hour): %s' % headers['x-limit-key-remaining'])
def add_item(item): if item is not None: access_token = wf.get_password('pocket_access_token') pocket_instance = Pocket(config.CONSUMER_KEY, access_token) try: pocket_instance.add( url=item['url'], title=item['title'], tags="alfred") return True except InvalidQueryException: pass return False
def send_stories_to_pocket(): stories = get_best_daily_stories() consumer_key = os.getenv('CONSUMER_KEY') access_token = os.getenv('ACCESS_TOKEN') pocket = Pocket(consumer_key, access_token) for story in stories: url = story.get('url') title = story.get('title') print(f'{title} - {url}') try: pocket.add(url, tags='hacker-news-app') except Exception: pass
def main(argv): parsed_args = parse_args() try: from pocket import Pocket, PocketException except ImportError: print_error('You need to `pip3 install pocket-api`') print_refresh() sys.exit(1) pocket = Pocket(consumer_key=CONSUMER_KEY, access_token=ACCESS_TOKEN) if parsed_args.delete: pocket.delete(parsed_args.delete).commit() sys.exit() elif parsed_args.add: new_url = get_url() if new_url: pocket.add(url=new_url) sys.exit() try: raw_answer = pocket.retrieve(sort='newest') except (Exception, PocketException) as error: print_error(error) print_refresh() sys.exit(1) adapted_articles = [ Article(i.get('item_id'), i.get('resolved_url', i.get('given_url')), i.get('resolved_title', i.get('given_title')), argv[0]) for i in raw_answer['list'].values() ] print( f'{len(adapted_articles)}|font=Verdana templateImage={pocket_icon()}') print('---') print(*adapted_articles, sep='\n') print('---') print( f'➕ Save a URL|bash={argv[0]} param1=--add terminal=false refresh=true' ) print_refresh()
class DoPocket(): '''A class for fetching links and sending them to pocket.''' def __init__(self, cache, opts, config): self.logger = logging.getLogger(__name__) self.opts = opts self.cache = cache self.pocket_instance = Pocket(config['USEROPTS']['CONSUMER_KEY'], config['USEROPTS']['ACCESS_TOKEN']) def savetopocket(self, _f, _link, _title=''): '''Save a link to pocket and cache the result.''' _title = _title or 'Morty' if not self.cache.haskey('links', hashstring(_f)): self.cache.set([], 'links', hashstring(_f)) self.logger.info("Adding new key for %s in links.", _f) if self.cache.has(_link, 'links', hashstring(_f)): return False if not checkurl(_link): self.logger.warning( "Not saving %s to pocket because it did not load.", _link) return False if not self.opts.cacheonly: self.logger.debug('Saving %s (%s) to Pocket', _f, _link) if not self.opts.dryrun: try: _, _ = self.pocket_instance.add(_link, title=_title) self.cache.append_unique(_link, 'links', hashstring(_f)) return True except PocketException as msg: self.logger.error("Error adding %s to pocket: %s", _link, str(msg)) except AttributeError: self.logger.error("No pocket instance, not saving.") else: self.logger.debug('Caching %s (%s) to Pocket', _title, _link) self.cache.append_unique(_link, 'links', hashstring(_f)) return True return False def rsstopocket(self, rss_feeds): '''Crawl and RSS feed and upload URLs to Pocket''' _cached = [] for _f in rss_feeds: feed = feedparser.parse('https://' + _f) if feed['bozo'] == 1: self.logger.error(feed['bozo_exception']) continue for item in feed['entries']: if 'title' in item: title = item['title'] else: title = 'No Title' _cached.append(self.savetopocket(_f, item['link'], title)) return _cached
def main(): p = Pocket( # TODO: use configuration file... for now, just modify: consumer_key='', access_token='') os.chdir('./Keep') dirs = os.listdir('.') links = [create_and_init_link(fn) for fn in dirs if fn[-4:] == 'html'] count = 0 for link in links: #add(url, title, tags if link.link: print(link) try: p.add(link.link, title=link.title, tags=','.join(link.tags)) except Exception as e: print('Error', e) continue count += 1 print('{} links imported out of {}'.format(count, len(links)))
def send_to_pocket(url): if mconfig: if 'share_settings' in mconfig: if 'pocket' in mconfig['share_settings']: pocket_config = mconfig['share_settings']['pocket'] pc = Pocket( consumer_key=pocket_config['consumer_key'], access_token=pocket_config['access_token'] ) return pc.add( url=url ) else: return {"status":0,"message":"Pocket not configured."} else: return {"status":0,"message":"No configuration file found."}
class ServicePocket(ServicesMgr): """ Service Pocket """ def __init__(self, token=None, **kwargs): super(ServicePocket, self).__init__(token, **kwargs) self.consumer_key = settings.TH_POCKET_KEY['consumer_key'] self.token = token self.oauth = 'oauth1' self.service = 'ServicePocket' if token: try: self.pocket = Pocket(self.consumer_key, token) except (AuthException, RateLimitException) as e: us = UserService.objects.get(token=token) logger.error(e.msg, e.error_code) update_result(us.trigger_id, msg=e.msg, status=False) def _create_entry(self, url, title, tags): """ Create an entry :param url: url to save :param title: title to set :param tags: tags to set :return: status """ try: self.pocket.add(url=url, title=title, tags=tags) sentence = str('pocket {} created').format(url) logger.debug(sentence) status = True except Exception as e: logger.critical(e) update_result(self.trigger_id, msg=e, status=False) status = False return status def read_data(self, **kwargs): """ get the data from the service As the pocket service does not have any date in its API linked to the note, add the triggered date to the dict data thus the service will be triggered when data will be found :param kwargs: contain keyword args : trigger_id at least :type kwargs: dict :rtype: list """ trigger_id = kwargs.get('trigger_id') date_triggered = kwargs.get('date_triggered') data = list() # pocket uses a timestamp date format since = arrow.get(date_triggered).timestamp if self.token is not None: # get the data from the last time the trigger have been started # timestamp form pockets = self.pocket.get(since=since, state="unread") content = '' if pockets is not None and len(pockets[0]['list']) > 0: for my_pocket in pockets[0]['list'].values(): if my_pocket.get('excerpt'): content = my_pocket['excerpt'] elif my_pocket.get('given_title'): content = my_pocket['given_title'] my_date = arrow.get(str(date_triggered), 'YYYY-MM-DD HH:mm:ss').to(settings.TIME_ZONE) data.append({'my_date': str(my_date), 'tag': '', 'link': my_pocket['given_url'], 'title': my_pocket['given_title'], 'content': content, 'tweet_id': 0}) # digester self.send_digest_event(trigger_id, my_pocket['given_title'], my_pocket['given_url']) cache.set('th_pocket_' + str(trigger_id), data) return data def save_data(self, trigger_id, **data): """ let's save the data :param trigger_id: trigger ID from which to save data :param data: the data to check to be used and save :type trigger_id: int :type data: dict :return: the status of the save statement :rtype: boolean """ if data.get('link'): if len(data.get('link')) > 0: # get the pocket data of this trigger from th_pocket.models import Pocket as PocketModel trigger = PocketModel.objects.get(trigger_id=trigger_id) title = self.set_title(data) # convert htmlentities title = HtmlEntities(title).html_entity_decode status = self._create_entry(url=data.get('link'), title=title, tags=(trigger.tag.lower())) else: msg = "no link provided for trigger ID {}, so we ignore it".format(trigger_id) logger.warning(msg) update_result(trigger_id, msg=msg, status=True) status = True else: msg = "no token provided for trigger ID {}".format(trigger_id) logger.critical(msg) update_result(trigger_id, msg=msg, status=False) status = False return status def auth(self, request): """ let's auth the user to the Service :param request: request object :return: callback url :rtype: string that contains the url to redirect after auth """ callback_url = self.callback_url(request) request_token = Pocket.get_request_token(consumer_key=self.consumer_key, redirect_uri=callback_url) # Save the request token information for later request.session['request_token'] = request_token # URL to redirect user to, to authorize your app auth_url = Pocket.get_auth_url(code=request_token, redirect_uri=callback_url) return auth_url def callback(self, request, **kwargs): """ Called from the Service when the user accept to activate it :param request: request object :return: callback url :rtype: string , path to the template """ access_token = Pocket.get_access_token(consumer_key=self.consumer_key, code=request.session['request_token']) kwargs = {'access_token': access_token} return super(ServicePocket, self).callback(request, **kwargs)
def add_item(item): access_token = wf.get_password('pocket_access_token') pocket_instance = Pocket(CONSUMER_KEY, access_token) pocket_instance.add(url=item['url'], title=item['title'], tags="alfred")
class ServicePocket(ServicesMgr): def __init__(self, token=None): self.consumer_key = settings.TH_POCKET['consumer_key'] if token: self.pocket = Pocket(self.consumer_key, token) def read_data(self, token, trigger_id, date_triggered): """ get the data from the service as the pocket service does not have any date in its API linked to the note, add the triggered date to the dict data thus the service will be triggered when data will be found :param trigger_id: trigger ID to process :param date_triggered: the date of the last trigger :type trigger_id: int :type date_triggered: datetime :return: list of data found from the date_triggered filter :rtype: list """ data = list() # pocket uses a timestamp date format since = int( time.mktime(datetime.datetime.timetuple(date_triggered))) if token is not None: # get the data from the last time the trigger have been started # timestamp form pockets = self.pocket.get(since=since, state="unread") if pockets is not None and len(pockets[0]['list']) > 0: for my_pocket in pockets[0]['list'].values(): if my_pocket['excerpt']: content = my_pocket['excerpt'] elif my_pocket['given_title']: content = my_pocket['given_title'] my_date = arrow.get(str(date_triggered), 'YYYY-MM-DD HH:mm:ss')\ .to(settings.TIME_ZONE) data.append({'my_date': str(my_date), 'tag': '', 'link': pocket['given_url'], 'title': pocket['given_title'], 'content': content, 'tweet_id': 0}) cache.set('th_pocket_' + str(trigger_id), data) return data def process_data(self, trigger_id): """ get the data from the cache :param trigger_id: trigger ID from which to save data :type trigger_id: int """ cache_data = cache.get('th_pocket_' + str(trigger_id)) return PublishingLimit.get_data('th_pocket', cache_data, trigger_id) def save_data(self, token, trigger_id, **data): """ let's save the data :param trigger_id: trigger ID from which to save data :param **data: the data to check to be used and save :type trigger_id: int :type **data: dict :return: the status of the save statement :rtype: boolean """ from th_pocket.models import Pocket as PocketModel status = False if token and 'link' in data and data['link'] is not None\ and len(data['link']) > 0: # get the pocket data of this trigger trigger = PocketModel.objects.get(trigger_id=trigger_id) title = '' title = (data['title'] if 'title' in data else '') # convert htmlentities title = HtmlEntities(title).html_entity_decode try: self.pocket.add( url=data['link'], title=title, tags=(trigger.tag.lower())) sentence = str('pocket {} created').format(data['link']) logger.debug(sentence) status = True except Exception as e: logger.critical(e) status = False else: logger.critical("no token provided for trigger ID %s ", trigger_id) return status def auth(self, request): """ let's auth the user to the Service """ callback_url = 'http://%s%s' % ( request.get_host(), reverse('pocket_callback')) request_token = Pocket.get_request_token( consumer_key=self.consumer_key, redirect_uri=callback_url) # Save the request token information for later request.session['request_token'] = request_token # URL to redirect user to, to authorize your app auth_url = Pocket.get_auth_url( code=request_token, redirect_uri=callback_url) return auth_url def callback(self, request): """ Called from the Service when the user accept to activate it """ try: # finally we save the user auth token # As we already stored the object ServicesActivated # from the UserServiceCreateView now we update the same # object to the database so : # 1) we get the previous object us = UserService.objects.get( user=request.user, name=ServicesActivated.objects.get(name='ServicePocket')) # 2) then get the token access_token = Pocket.get_access_token( consumer_key=self.consumer_key, code=request.session['request_token']) us.token = access_token # 3) and save everything us.save() except KeyError: return '/' return 'pocket/callback.html'
class ServicePocket(ServicesMgr): def __init__(self, token=None): super(ServicePocket, self).__init__(token) self.consumer_key = settings.TH_POCKET['consumer_key'] self.token = token if token: self.pocket = Pocket(self.consumer_key, token) def read_data(self, **kwargs): """ get the data from the service as the pocket service does not have any date in its API linked to the note, add the triggered date to the dict data thus the service will be triggered when data will be found :param kwargs: contain keyword args : trigger_id at least :type kwargs: dict :rtype: list """ trigger_id = kwargs['trigger_id'] date_triggered = kwargs['date_triggered'] data = list() # pocket uses a timestamp date format since = int( time.mktime(datetime.datetime.timetuple(date_triggered))) if self.token is not None: # get the data from the last time the trigger have been started # timestamp form pockets = self.pocket.get(since=since, state="unread") content = '' if pockets is not None and len(pockets[0]['list']) > 0: for my_pocket in pockets[0]['list'].values(): if my_pocket['excerpt']: content = my_pocket['excerpt'] elif my_pocket['given_title']: content = my_pocket['given_title'] my_date = arrow.get(str(date_triggered), 'YYYY-MM-DD HH:mm:ss')\ .to(settings.TIME_ZONE) data.append({'my_date': str(my_date), 'tag': '', 'link': my_pocket['given_url'], 'title': my_pocket['given_title'], 'content': content, 'tweet_id': 0}) cache.set('th_pocket_' + str(trigger_id), data) return data def process_data(self, **kwargs): """ get the data from the cache :param kwargs: contain keyword args : trigger_id at least :type kwargs: dict """ kw = {'cache_stack': 'th_pocket', 'trigger_id': str(kwargs['trigger_id'])} return super(ServicePocket, self).process_data(**kw) def save_data(self, trigger_id, **data): """ let's save the data :param trigger_id: trigger ID from which to save data :param data: the data to check to be used and save :type trigger_id: int :type data: dict :return: the status of the save statement :rtype: boolean """ from th_pocket.models import Pocket as PocketModel status = False if self.token and 'link' in data and data['link'] is not None\ and len(data['link']) > 0: # get the pocket data of this trigger trigger = PocketModel.objects.get(trigger_id=trigger_id) title = self.set_title(data) # convert htmlentities title = HtmlEntities(title).html_entity_decode try: self.pocket.add( url=data['link'], title=title, tags=(trigger.tag.lower())) sentence = str('pocket {} created').format(data['link']) logger.debug(sentence) status = True except Exception as e: logger.critical(e) status = False else: logger.critical("no token provided for trigger ID %s ", trigger_id) return status def auth(self, request): """ let's auth the user to the Service :param request: request object :return: callback url :rtype: string that contains the url to redirect after auth """ callback_url = self.callback_url(request, 'pocket') request_token = Pocket.get_request_token( consumer_key=self.consumer_key, redirect_uri=callback_url) # Save the request token information for later request.session['request_token'] = request_token # URL to redirect user to, to authorize your app auth_url = Pocket.get_auth_url( code=request_token, redirect_uri=callback_url) return auth_url def callback(self, request, **kwargs): """ Called from the Service when the user accept to activate it :param request: request object :return: callback url :rtype: string , path to the template """ access_token = Pocket.get_access_token( consumer_key=self.consumer_key, code=request.session['request_token']) kwargs = {'access_token': access_token, 'service': 'ServicePocket', 'return': 'pocket'} return super(ServicePocket, self).callback(request, **kwargs)
def add_url(url, tag): print 'adding', url pocket_instance = Pocket(consumer_key, access_token) print pocket_instance.add(url=url, tags=[tag])
consumer_secret = os.environ.get('API_secretkey') #Pocket keys p_consumer_key = os.environ.get('Pocket_consumer_key') p_access_token = os.environ.get('Pocket_access_token') #authenticate and call twitter api auth = tweepy.OAuthHandler(consumer_key, consumer_secret) api = tweepy.API(auth, parser=tweepy.parsers.JSONParser()) p = Pocket(consumer_key=p_consumer_key, access_token=p_access_token) #gets JSON of liked tweets fav = api.favorites('importhuman', count=100, tweet_mode='extended') links = [] n = 0 for status in fav: url_list = status['entities']['urls'] if url_list != []: for item in url_list: link = item['expanded_url'] if link not in links: if re.search("//twitter.com/", link) is None: links.append(link) p.add(link) n += 1 print(links) print(n)
class ServicePocket(ServicesMgr): """ Service Pocket """ def __init__(self, token=None, **kwargs): super(ServicePocket, self).__init__(token, **kwargs) self.consumer_key = settings.TH_POCKET['consumer_key'] self.token = token self.oauth = 'oauth1' self.service = 'ServicePocket' if token: try: self.pocket = Pocket(self.consumer_key, token) except (AuthException, RateLimitException) as e: us = UserService.objects.get(token=token) logger.error(e.msg, e.error_code) update_result(us.trigger_id, msg=e.msg, status=False) def _create_entry(self, url, title, tags): """ Create an entry :param url: url to save :param title: title to set :param tags: tags to set :return: status """ try: self.pocket.add(url=url, title=title, tags=tags) sentence = str('pocket {} created').format(url) logger.debug(sentence) status = True except Exception as e: logger.critical(e) update_result(self.trigger_id, msg=e, status=False) status = False return status def read_data(self, **kwargs): """ get the data from the service as the pocket service does not have any date in its API linked to the note, add the triggered date to the dict data thus the service will be triggered when data will be found :param kwargs: contain keyword args : trigger_id at least :type kwargs: dict :rtype: list """ trigger_id = kwargs.get('trigger_id') date_triggered = kwargs.get('date_triggered') data = list() # pocket uses a timestamp date format since = arrow.get(date_triggered).timestamp if self.token is not None: # get the data from the last time the trigger have been started # timestamp form pockets = self.pocket.get(since=since, state="unread") content = '' if pockets is not None and len(pockets[0]['list']) > 0: for my_pocket in pockets[0]['list'].values(): if my_pocket.get('excerpt'): content = my_pocket['excerpt'] elif my_pocket.get('given_title'): content = my_pocket['given_title'] my_date = arrow.get(str(date_triggered), 'YYYY-MM-DD HH:mm:ss')\ .to(settings.TIME_ZONE) data.append({ 'my_date': str(my_date), 'tag': '', 'link': my_pocket['given_url'], 'title': my_pocket['given_title'], 'content': content, 'tweet_id': 0 }) cache.set('th_pocket_' + str(trigger_id), data) return data def save_data(self, trigger_id, **data): """ let's save the data :param trigger_id: trigger ID from which to save data :param data: the data to check to be used and save :type trigger_id: int :type data: dict :return: the status of the save statement :rtype: boolean """ if data.get('link'): if len(data.get('link')) > 0: # get the pocket data of this trigger from th_pocket.models import Pocket as PocketModel trigger = PocketModel.objects.get(trigger_id=trigger_id) title = self.set_title(data) # convert htmlentities title = HtmlEntities(title).html_entity_decode status = self._create_entry(url=data.get('link'), title=title, tags=(trigger.tag.lower())) else: msg = "no link provided for trigger ID {}," \ " so we ignore it".format(trigger_id) logger.warning(msg) update_result(trigger_id, msg=msg, status=True) status = True else: msg = "no token provided for trigger ID {}".format(trigger_id) logger.critical(msg) update_result(trigger_id, msg=msg, status=False) status = False return status def auth(self, request): """ let's auth the user to the Service :param request: request object :return: callback url :rtype: string that contains the url to redirect after auth """ callback_url = self.callback_url(request) request_token = Pocket.get_request_token( consumer_key=self.consumer_key, redirect_uri=callback_url) # Save the request token information for later request.session['request_token'] = request_token # URL to redirect user to, to authorize your app auth_url = Pocket.get_auth_url(code=request_token, redirect_uri=callback_url) return auth_url def callback(self, request, **kwargs): """ Called from the Service when the user accept to activate it :param request: request object :return: callback url :rtype: string , path to the template """ access_token = Pocket.get_access_token( consumer_key=self.consumer_key, code=request.session['request_token']) kwargs = {'access_token': access_token} return super(ServicePocket, self).callback(request, **kwargs)
from pocket import Pocket ckey = '47167-9324a229a155827bc214aa2a' redir = 'http://google.com' request_token = Pocket.get_request_token(consumer_key=ckey, redirect_uri=redir) # URL to redirect user to, to authorize your app auth_url = Pocket.get_auth_url(code=request_token, redirect_uri=redir) print(auth_url) input() user_credentials = Pocket.get_credentials(consumer_key=ckey, code=request_token) access_token = user_credentials['access_token'] pocketi = Pocket(ckey, access_token) for link in links[500:]: pocketi.add(link) import numpy as np import matplotlib.pyplot as plt xaxis = range(1,50) plt.plot(xaxis, [np.sqrt(6*sum([1./n**2 for n in range(1,x)])) for x in xaxis]) plt.axhline(np.pi, color='r') plt.show()z
class ServicePocket(ServicesMgr): def __init__(self, token=None): self.consumer_key = settings.TH_POCKET['consumer_key'] if token: self.pocket = Pocket(self.consumer_key, token) def read_data(self, token, trigger_id, date_triggered): """ get the data from the service as the pocket service does not have any date in its API linked to the note, add the triggered date to the dict data thus the service will be triggered when data will be found :param trigger_id: trigger ID to process :param date_triggered: the date of the last trigger :type trigger_id: int :type date_triggered: datetime :return: list of data found from the date_triggered filter :rtype: list """ data = list() # pocket uses a timestamp date format since = int( time.mktime(datetime.datetime.timetuple(date_triggered))) if token is not None: # get the data from the last time the trigger have been started # timestamp form pockets = self.pocket.get(since=since, state="unread") if pockets is not None and len(pockets[0]['list']) > 0: for my_pocket in pockets[0]['list'].values(): if my_pocket['excerpt']: content = my_pocket['excerpt'] elif my_pocket['given_title']: content = my_pocket['given_title'] my_date = arrow.get(str(date_triggered), 'YYYY-MM-DD HH:mm:ss')\ .to(settings.TIME_ZONE) data.append({'my_date': str(my_date), 'tag': '', 'link': pocket['given_url'], 'title': pocket['given_title'], 'content': content, 'tweet_id': 0}) cache.set('th_pocket_' + str(trigger_id), data) return data def process_data(self, trigger_id): """ get the data from the cache :param trigger_id: trigger ID from which to save data :type trigger_id: int """ return cache.get('th_pocket_' + str(trigger_id)) def save_data(self, token, trigger_id, **data): """ let's save the data :param trigger_id: trigger ID from which to save data :param **data: the data to check to be used and save :type trigger_id: int :type **data: dict :return: the status of the save statement :rtype: boolean """ from th_pocket.models import Pocket as PocketModel status = False if token and 'link' in data and data['link'] is not None\ and len(data['link']) > 0: # get the pocket data of this trigger trigger = PocketModel.objects.get(trigger_id=trigger_id) title = '' title = (data['title'] if 'title' in data else '') # convert htmlentities title = HtmlEntities(title).html_entity_decode try: self.pocket.add( url=data['link'], title=title, tags=(trigger.tag.lower())) sentence = str('pocket {} created').format(data['link']) logger.debug(sentence) status = True except Exception as e: logger.critical(e) status = False else: logger.critical("no token provided for trigger ID %s ", trigger_id) return status def auth(self, request): """ let's auth the user to the Service """ callback_url = 'http://%s%s' % ( request.get_host(), reverse('pocket_callback')) request_token = Pocket.get_request_token( consumer_key=self.consumer_key, redirect_uri=callback_url) # Save the request token information for later request.session['request_token'] = request_token # URL to redirect user to, to authorize your app auth_url = Pocket.get_auth_url( code=request_token, redirect_uri=callback_url) return auth_url def callback(self, request): """ Called from the Service when the user accept to activate it """ try: # finally we save the user auth token # As we already stored the object ServicesActivated # from the UserServiceCreateView now we update the same # object to the database so : # 1) we get the previous object us = UserService.objects.get( user=request.user, name=ServicesActivated.objects.get(name='ServicePocket')) # 2) then get the token access_token = Pocket.get_access_token( consumer_key=self.consumer_key, code=request.session['request_token']) us.token = access_token # 3) and save everything us.save() except KeyError: return '/' return 'pocket/callback.html'
from pocket import Pocket, PocketException import os params = { 'username': os.environ['WALLABAG_USER'], 'password': os.environ['WALLABAG_PASS'], 'client_id': os.environ['WALLABAG_CLIENT'], 'client_secret': os.environ['WALLABAG_SECRET'], 'host': os.environ['WALLABAG_HOST'] } token = Wallabag.get_token(**params) wb = Wallabag(params['host'], token, params['client_id'], params['client_secret']) entries = wb.get_entries() urls = [] while entries['page'] <= entries['pages']: for item in entries['_embedded']['items']: urls.append(item['url']) entries = wb.get_entries(page=entries['page'] + 1) print(len(urls), "urls fetched from wallabag") p = Pocket(consumer_key=os.environ['POCKET_KEY'], access_token=os.environ['POCKET_TOKEN']) for i, url in enumerate(urls): print(i, url) p.add(url)
lasturl = "http://www.economist.com/news/asia/21678115-display-amity-points-tougher-times-ahead-leaders-taiwan-and-china-hold-historic" while True: stack = [] import itertools for i in itertools.count(0): from urllib.request import urlopen, Request from bs4 import BeautifulSoup request = Request( "http://www.economist.com/latest-updates?page=" + str(i), headers={ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0}" }) bs = BeautifulSoup(urlopen(request)) for article in bs.find_all("article"): from urllib.parse import urljoin url = urljoin(request.full_url, article.a['href']) if url == lasturl: break stack.append(url) if url == lasturl: break for url in reversed(stack): print(url) pocket.add(url, wait=False) if len(stack) > 0: lasturl = stack[-1] from time import sleep sleep(300)
class PocketApp: DEFAULT_WORDS_PER_MINUTE = 180 REDIRECT_URL = 'http://www.google.com' def __init__(self): self._configs = Configs() self._storage = Storage() self._pocket = Pocket(self._configs.get('consumer_key'), self._configs.get('access_token')) def configure(self, consumer_key, access_token, words_per_minute, sort_field): self._configs.set('consumer_key', consumer_key) self._configs.set('access_token', access_token) self._configs.set('words_per_minute', words_per_minute) self._configs.set('sort_field', sort_field) self._configs.set('last_fetch', 0) self._configs.write() self._storage.clear() self._pocket = Pocket(consumer_key, access_token) def init_consumer_key(self, consumer_key): self._pocket = Pocket(consumer_key) def get_request_token(self): return self._pocket.get_request_token(self.REDIRECT_URL) def get_access_token(self, request_token): return self._pocket.get_access_token(request_token) def add_article(self, url, title=None, tags=None): if isinstance(tags, tuple): tags = ','.join(list(tags)) try: return self._pocket.add(url, title, tags) except PocketException as e: raise_from(self._check_exception(e), e) def get_articles(self, limit=None, order=None): if self._storage.is_empty(): self.fetch_articles(True) articles = self._storage.read(limit, order) sort_field = self._configs.get('sort_field') if not sort_field: sort_field = 'reading_time' articles = sorted(articles, key=itemgetter(sort_field)) return articles def search(self, search, state, tag, sort): try: # search_state = self._configs.get('search_state') # if search_state and not state: # state = search_state articles = self._pocket.retrieve(search=search, state=state, tag=tag, sort=sort) return self._get_articles_index(articles) except PocketException as e: raise_from(self._check_exception(e), e) def archive_article(self, item_id): try: self._pocket.archive(int(item_id)).commit() except PocketException as e: raise_from(self._check_exception(e), e) 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 = [] last_fetch = self._configs.get('last_fetch') offset = 0 count = 20 while (True): try: articles = self._pocket.retrieve(state='unread', count=count, offset=offset, since=last_fetch) except PocketException as e: spinner.finish() raise_from(self._check_exception(e), e) if not articles['list']: break articles_index.extend(self._get_articles_index(articles)) offset += count if spinner: spinner.next() if spinner: spinner.finish() sort_field = self._configs.get('sort_field') if not sort_field: sort_field = 'reading_time' articles_index = sorted(articles_index, key=itemgetter(sort_field)) self._storage.write(articles_index) self._configs.set('last_fetch', self._get_timestamp(datetime.now())) self._configs.write() def _get_articles_index(self, articles): wpm = self._configs.get('words_per_minute') if not wpm: wpm = self.DEFAULT_WORDS_PER_MINUTE wpm = int(wpm) articles_index = [] articles_list = articles['list'] if isinstance(articles_list, list) and len(articles_list) == 0: return articles_index for article in articles_list.values(): word_count = int(article.get('word_count', 0)) if word_count == 0: reading_time = -1 else: reading_time = int(math.ceil(word_count / wpm)) title = article.get('resolved_title', None) if not title: title = article['given_title'] url = article.get('resolved_url', None) if not url: url = article['given_url'] index = { 'id': article['item_id'], 'title': title, 'url': url, 'word_count': word_count, 'reading_time': reading_time } articles_index.append(index) return articles_index def _get_timestamp(self, date): return int(time.mktime(date.timetuple())) def _check_exception(self, e): if isinstance(e, PocketAutException): raise AppNotConfigured('Application is not configured') raise AppException(e.message)
class PocketApp: DEFAULT_WORDS_PER_MINUTE = 180 REDIRECT_URL = 'http://www.google.com' def __init__(self): self._configs = Configs() self._storage = Storage() self._pocket = Pocket( self._configs.get('consumer_key'), self._configs.get('access_token') ) def configure(self, consumer_key, access_token, words_per_minute, sort_field): self._configs.set('consumer_key', consumer_key) self._configs.set('access_token', access_token) self._configs.set('words_per_minute', words_per_minute) self._configs.set('sort_field', sort_field) self._configs.set('last_fetch', 0) self._configs.write() self._storage.clear() def get_request_token(self, consumer_key): return self._pocket.get_request_token( consumer_key, self.REDIRECT_URL ) def get_access_token(self, consumer_key, request_token): return self._pocket.get_access_token( consumer_key, request_token ) def add_article(self, url, title=None, tags=None): if isinstance(tags, tuple): tags = ','.join(list(tags)) try: return self._pocket.add(url, title, tags) except PocketException as e: raise_from(self._check_exception(e), e) def get_articles(self, limit=None, order=None): if self._storage.is_empty(): self.fetch_articles(True) articles = self._storage.read(limit, order) sort_field = self._configs.get('sort_field') if not sort_field: sort_field = 'reading_time' articles = sorted(articles, key=itemgetter(sort_field)) return articles def search(self, search, state, tag, sort): try: articles = self._pocket.retrieve(search=search, state=state, tag=tag, sort=sort) return self._get_articles_index(articles) except PocketException as e: raise_from(self._check_exception(e), e) def archive_article(self, item_id): try: self._pocket.archive(int(item_id)).commit() except PocketException as e: raise_from(self._check_exception(e), e) 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 = [] last_fetch = self._configs.get('last_fetch') offset = 0 count = 20 while(True): try: articles = self._pocket.retrieve( state='unread', count=count, offset=offset, since=last_fetch ) except PocketException as e: spinner.finish() raise_from(self._check_exception(e), e) if not articles['list']: break articles_index.extend(self._get_articles_index(articles)) offset += count if spinner: spinner.next() if spinner: spinner.finish() sort_field = self._configs.get('sort_field') if not sort_field: sort_field = 'reading_time' articles_index = sorted(articles_index, key=itemgetter(sort_field)) self._storage.write(articles_index) self._configs.set('last_fetch', self._get_timestamp(datetime.now())) self._configs.write() def _get_articles_index(self, articles): wpm = self._configs.get('words_per_minute') if not wpm: wpm = self.DEFAULT_WORDS_PER_MINUTE wpm = int(wpm) articles_index = [] articles_list = articles['list'] if isinstance(articles_list, list) and len(articles_list) == 0: return articles_index 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) return articles_index def _get_timestamp(self, date): return int(time.mktime(date.timetuple())) def _check_exception(self, e): if isinstance(e, PocketAutException): raise AppNotConfigured('Application is not configured') raise AppException(e.message)
entry_score = 0 for keyword in keywords: entry_score += entry['summary'].count(keyword) sc = entry_score # Connect to DB and add unique items to the list conn = sqlite3.Connection("Pocket.db") c = conn.cursor() c.execute('''CREATE TABLE IF NOT EXISTS results( ID UNIQUE, Title TEXT, Link TEXT, Summary TEXT, Score INTEGER, Pocket INTEGER) ''') c.execute( '''INSERT OR IGNORE INTO results VALUES (?, ?, ?, ?, ?, 0); ''', (ident, t, l, su, sc)) conn.commit() conn.close() # Check score and add high scores to Pocket p = Pocket(consumer_key=p_c_key, access_token=p_a_token) conn = sqlite3.Connection("Pocket.db") c = conn.cursor() c.execute('SELECT Link FROM results WHERE Score > 2 AND Pocket = 0 LIMIT 1') all_rows = c.fetchall() try: add = all_rows[0][0] p.add(add, tags="Amber") except IndexError: print("No new articles meet the threshold") c.execute( 'UPDATE results SET Pocket = 1 WHERE (Score > 2 AND Pocket = 0) LIMIT 1') conn.commit() conn.close
def assistant(command): "if statements for executing commands" if 'open reddit' in command: reg_ex = re.search('open reddit (.*)', command) url = 'https://www.reddit.com/' if reg_ex: subreddit = reg_ex.group(1) url = url + 'r/' + subreddit webbrowser.open(url) print('Done!') elif 'open website' in command: reg_ex = re.search('open website (.+)', command) if reg_ex: domain = reg_ex.group(1) url = 'https://www.' + domain webbrowser.open(url) print('Done!') else: pass elif 'sing' in command: talkToMe('What Is The Name Of The Song?') SONG = myCommand() talkToMe('Who Sing That Song?') SINGER = myCommand() lyrics = lyricwikia.get_lyrics(SINGER, SONG) mystring = lyrics.replace('\n', ' ').replace('\r', '').replace( '\'', '').replace('(', '').replace(')', '') talkToMe(mystring) elif 'search for' in command: reg_ex = re.search('search for (.+)', command) if reg_ex: domain = reg_ex.group(1) url = 'https://www.google.com/search?q=' + domain webbrowser.open(url) print('Done!') else: pass elif 'say hi to' in command: reg_ex = re.search('say hi to (.+)', command) if reg_ex: domain = reg_ex.group(1) talkToMe('Hi ' + domain) else: pass elif 'tell me about' in command: reg_ex = re.search('tell me about (.+)', command) if reg_ex: consumer_key = "84071-393fa2595527eaf671022012" access_token = "6f0441b8-3122-6194-706b-4ec7e6" pocket_instance = pocket.Pocket(consumer_key, access_token) p = Pocket(consumer_key="84071-393fa2595527eaf671022012", access_token="6f0441b8-3122-6194-706b-4ec7e6") topic = reg_ex.group(1) w = wikipedia.page(topic) u = w.url TopicPage = wikipedia.summary(topic, sentences=3) x = re.sub('[^ a-zA-Z0-9]', '', TopicPage) talkToMe(str(x)) time.sleep(1) talkToMe('Do You Want Me To Save It To Your Pocket?') order = myCommand() if 'yes' in order: p.add(u) talkToMe('Great This Article Now In Your Pocket ') elif 'No' or 'no' or 'not' in order: talkToMe('Okay Maybe Another Time.') else: pass else: pass elif 'play' in command: talkToMe('what song do you want me to play ?') textToSearch = myCommand() query = urllib.parse.quote(textToSearch) url = "https://www.youtube.com/results?search_query=" + query response = urllib.request.urlopen(url) html = response.read() soup = BeautifulSoup(html, features="lxml") Links = [] for vid in soup.findAll(attrs={'class': 'yt-uix-tile-link'}): Links.append('https://www.youtube.com' + vid['href']) url2 = Links[0] webbrowser.open(url2) talkToMe('Playing' + query) elif 'random' in command: L = (glob.glob("/home/mohamed/Music/*.mp3")) x = random.randint(0, len(L)) name = L[x] name2 = name[20:-4] webbrowser.open(L[x]) talkToMe('Playing ' + name2) elif 'what are you doing' in command: talkToMe('Just doing my thing') elif 'good morning' in command: talkToMe('Good Morning My Friend ') elif 'introduce yourself' in command: talkToMe( '''Hello There , My Name Is Robot,I Was Created By Group Of Amazing Engineers In Nile Academy,You Can Call Me Hamo Bika If You Want!''' ) elif 'who is your friend' in command: talkToMe('my friend is ahmed emad') elif 'who is your creator' in command: talkToMe( 'i have been created with a group of amazing engineers From Nile Academy ' ) talkToMe('mohamed wael is one of them') elif 'joke' in command: res = requests.get('https://icanhazdadjoke.com/', headers={"Accept": "application/json"}) if res.status_code == requests.codes.ok: talkToMe(str(res.json()['joke'])) else: talkToMe('oops!I ran out of jokes') elif 'current weather in' in command: reg_ex = re.search('current weather in (.*)', command) if reg_ex: city_name = reg_ex.group(1) api_key = "42f0b6e848247352c0c59f6d72f05c69" base_url = "http://api.openweathermap.org/data/2.5/weather?" complete_url = base_url + "appid=" + api_key + "&q=" + city_name response = requests.get(complete_url) x = response.json() y = x["main"] current_temperature = y["temp"] current_pressure = y["pressure"] current_humidiy = y["humidity"] z = x["weather"] weather_description = z[0]["description"] tmp = str(int(current_temperature - 273)) wea = str(weather_description) talkToMe('The Weather In ' + city_name + ' City ') talkToMe('It Seems To Be ' + wea + ' Day') talkToMe('And It Will Be ' + tmp + ' Celsius Degrees') elif 'weather forecast in' in command: reg_ex = re.search('weather forecast in (.*)', command) if reg_ex: city_name = reg_ex.group(1) api_key = "42f0b6e848247352c0c59f6d72f05c69" base_url = "http://api.openweathermap.org/data/2.5/forecast?" complete_url2 = base_url + "appid=" + api_key + "&q=" + city_name + "&cnt=1" response = requests.get(complete_url2) x = response.json() y = x["list"][0]['main'] current_temperature = y["temp"] z = x['list'][0]["weather"] weather_description = z[0]["description"] tmp = str(int(current_temperature - 273)) wea = str(weather_description) talkToMe('The Weather In The Next Three To Five Days In ' + city_name + ' City ') talkToMe('It Seems That It Will Be ' + wea + ' Days') talkToMe('And It Will Be About ' + tmp + ' Celsius Degrees') elif 'news' in command: NEWST = [] NEWSD = [] response = requests.get( ' https://api.currentsapi.services/v1/search?keyword=Egypt', headers={ 'Authorization': 'rpb2wnUpmm9gzCOAT8hKPuC1KJFnH_KvacMAz5YOpNNgUVuM' }) length = len(response.json()['news']) for i in range(length): t = response.json()['news'][i]['title'] d = response.json()['news'][i]['description'] NEWST.append(t) NEWSD.append(d) O = random.randint(0, len(NEWST)) talkToMe( str(NEWST[O]).replace('\n', ' ').replace('\r', '').replace( '\'', '').replace('(', '').replace(')', '').replace('\"', '')) talkToMe( str(NEWSD[O]).replace('\n', ' ').replace('\r', '').replace( '\'', '').replace('(', '').replace(')', '').replace('\"', '')) elif 'quotation' in command: response = requests.get( 'http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1' ) x = response.json()[0]['content'] y = response.json()[0]['title'] label = (str(x).replace('\n', ' ').replace('8217', '\"').replace( '\r', '').replace('\'', '').replace('(', '').replace(')', '').replace( '\"', '').replace('*', '').replace('/', '').replace('+', '').replace( '^', '').replace('$', '').replace('#', '').replace( '-', '').replace('_', '').replace('>', '').replace( '<', '').replace(':', '').replace(';', '').replace('&', '')) l = label[1:-2] talkToMe(y + ' Said ') talkToMe(l) elif 'email' in command: talkToMe('Who is the recipient?') recipient = myCommand() if 'muhammad' or 'Mohamed' or 'Muhammad' in recipient: talkToMe('What should I say?') content = myCommand() #init gmail SMTP mail = smtplib.SMTP('smtp.gmail.com', 587) #identify to server mail.ehlo() #encrypt session mail.starttls() #login mail.login('*****@*****.**', 'PASS') #send message mail.sendmail('mohamed', '*****@*****.**', content) #end mail connection mail.close() talkToMe('Email sent.') else: talkToMe('I don\'t know what you mean!') else: talkToMe('I don\'t know what you mean!')