Found a better solution @cvp by digging in @JonB objc_hacks.
import requests as r
from bs4 import BeautifulSoup as soup
from objc_util import *
import notification
import ctypes
class _block_descriptor (Structure):
_fields_ = [('reserved', c_ulong), ('size', c_ulong), ('copy_helper', c_void_p), ('dispose_helper', c_void_p), ('signature', c_char_p)]
InvokeFuncType = ctypes.CFUNCTYPE(None, *[c_void_p, ctypes.c_int])
class _block_literal(Structure):
_fields_ = [('isa', c_void_p), ('flags', c_int), ('reserved', c_int), ('invoke', InvokeFuncType), ('descriptor', _block_descriptor)]
def update_forum(_cmd,_comp):
comp=ObjCInstance(_comp)
blk=_block_literal.from_address(_comp)
blk.invoke(comp,1)
with open('most_recent.txt','rb') as f:
lst=f.readlines()
most_recent,count=lst[0][:-1],lst[1]
url='https://forum.omz-software.com/recent'
page=r.get(url)
page=soup(page.content,'html.parser')
newest=page.select('meta')[8]
content_name=newest.attrs.get('content')
content_name=bytes(content_name,'utf8')
post_count=newest.find_all('span')[2].attrs.get('title')
post_count=bytes(post_count,'utf8')
if content_name != most_recent or post_count != count:
with open('most_recent.txt','wb') as f:
f.writelines([content_name,
b'\n',
post_count])
notification.schedule(
message=f'Newest on the Forum: {content_name.decode()}',
action_url=url,
title='Pythonista Forum',
identifier='Forum_Updater'
)
#else:
#notification.schedule(
message='Nothing new...',
title='Pythonista Forum',
identifier='Forum_Updater'
)
return
scheduler=ObjCClass('NSBackgroundActivityScheduler').alloc().initWithIdentifier_('Scheduler')
scheduler.setRepeats_(True)
scheduler.setInterval_(10*60)
scheduler.setTolerance_(60)
scheduler.setQualityOfService_(25)
block=ObjCBlock(update_forum,restype=None,argtypes=[c_void_p,c_void_p])
scheduler.scheduleWithBlock_(block)
Sadly, it only works when in the app. It does free up the console though