def publishFeed(profileID, searchItem, post): description = clean_html(post.description) config = ConfigHandler(profileID) webhookURL = config.getWebhookURL() # description = post.description # payload = "{\"Content\":\"" + post.title + "\\n\\n" \ # + post.published + "\\n\\n" \ # + description + "\\n\\n" \ # + post.link + "\"}" if 'hooks.slack' in webhookURL: # slack webhook label = "text" additionalSlack = ", \"username\": \"NFH\", \"icon_emoji\": \":newspaper:\"" else: # chime webhook label = "Content" additionalSlack = "" payload = "{\"" + label + "\":\"" + searchItem["searchItem"] + " - " + post.published + "\\n" \ + post.title + "\\n" \ + post.link + "\"" \ + additionalSlack \ + "}" # + additionalSlack if additionalSlack is not None + "}" # + description + "\\n" \ printable = "\\n".join(payload.split("\n")) print("post data", printable) publishToWebHook(printable, webhookURL)
def publishToWebHook(postData): config = ConfigHandler(profile) WH_ADDRESS = config.getWebhookURL() # print("webhook", WH_ADDRESS) response = requests.post(WH_ADDRESS, data=postData.encode('utf-8'), headers=POST_HEADERS) if response.status_code == 200: print("Published to webhook")
def __init__(self): super().__init__() #self.url = "https://sockets.streamlabs.com/socket.io/?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbiI6IkYwNzBBNUZDQjhDN0UxQzY5MDEwIiwicmVhZF9vbmx5Ijp0cnVlLCJwcmV2ZW50X21hc3RlciI6dHJ1ZSwidHdpdGNoX2lkIjoiNDMyMjk0NDg2In0.RCu48_ocKe_4nSmz1ZniDT0rB-ezIX1TJh5cpRSLz1g" #lg.basicConfig(level=lg.ERROR) self.config = ConfigHandler() self.model = Model(self) self.view = View(self) self.sio = socketio.Client()
def getNews(event, context): # get all profiles config = ConfigHandler() profiles = config.getProfileConfigs() for profile in profiles: print("running profiles", profiles) client.invoke(FunctionName='getNewsPerProfile', InvocationType='Event', Payload=json.dumps({ 'profileID': profile['id'], 'isTest': False })) return True
def getNewFeeds(profile, filterTime): # read search list config = ConfigHandler(profile) searchItems = config.getSearchConfig() for searchItem in searchItems: searchString = searchItem[ 'SearchString'] if 'SearchString' in searchItem else None if searchItem['RSS']: rssUrl = searchItem['RSS'] else: if searchString == 'SPECIAL_HARD_TO_SEARCH': continue if not searchString.startswith('"'): searchString = '"' + searchString if not searchString.endswith('"'): searchString = searchString + '"' rssUrl = 'https://news.google.com/rss/search?hl=en-SG&gl=SG&ceid=SG:en&q=' + searchString.replace( '"', "%22").replace(' ', '+') getNewFeedPerCustomer(profile, filterTime, searchItem, rssUrl)
def startThreads(self): self.commandHandler = CommandHandler(self) self.configHandler = ConfigHandler(self) self.guiThread = GUIThread(self) self.guiThread.start() time.sleep(1) # Wait for the GUI thread to initialize the GUI self.serialThread = SerialThread(self) self.serialThread.start()
def getNews(event, context): # get last check timestamp config = ConfigHandler(profile) lastCheckTimestamp = config.getLastCheckTimestmap() print(lastCheckTimestamp) # save new feeds ## publish start statement publishToWebHook("{\"Content\":\"/md **Daily " + profile + " news**\"}") newFeeds = getNewFeeds(profile, lastCheckTimestamp) # mark new timestamp # now = datetime.now() - timedelta(days = 30) now = datetime.now() newLastCheckTimestmap = now.isoformat() # update last check timestamp # updateLastCheckTimestamp(newLastCheckTimestmap) # publish an end statement publishToWebHook("{\"Content\":\"/md *Stay classy " + profile + "!*\"}") return True
def getNewsPerProfile(event, context): print("event", event) # get profileID profileID = event['profileID'] # get last check timestamp config = ConfigHandler(profileID) lastCheckTimestamp = config.getLastCheckTimestmap() webhookURL = config.getWebhookURL() # print(lastCheckTimestamp) # print(config.getProfileConfig()) # mark new timestamp # now = datetime.now() - timedelta(days = 30) now = datetime.now() newLastCheckTimestmap = now.isoformat() ## publish start statement publishToWebHook( "{\"Content\":\"/md **Daily " + config.getProfileConfig()['profile'] + " news**\"}", webhookURL) # is this a test if 'isTest' in event and event['isTest'] is True: print("Testing flow") testFrom = datetime.now() - timedelta( days=event['testBackDay'] if 'testBackDay' in event else 5) newFeeds = getNewFeeds(profileID, testFrom.isoformat(), isTest=True) # publish an end statement publishToWebHook( "{\"Content\":\"/md *Stay classy " + config.getProfileConfig()['profile'] + "!*\"}", webhookURL) return True # fool proof TODO refactor me newFeeds = getNewFeeds(profileID, lastCheckTimestamp, isTest=False) # update last check timestamp updateLastCheckTimestamp(profileID, newLastCheckTimestmap) # publish an end statement publishToWebHook( "{\"Content\":\"/md *Stay classy " + config.getProfileConfig()['profile'] + "!*\"}", webhookURL) return True
class Controller: def __init__(self): super().__init__() #self.url = "https://sockets.streamlabs.com/socket.io/?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbiI6IkYwNzBBNUZDQjhDN0UxQzY5MDEwIiwicmVhZF9vbmx5Ijp0cnVlLCJwcmV2ZW50X21hc3RlciI6dHJ1ZSwidHdpdGNoX2lkIjoiNDMyMjk0NDg2In0.RCu48_ocKe_4nSmz1ZniDT0rB-ezIX1TJh5cpRSLz1g" #lg.basicConfig(level=lg.ERROR) self.config = ConfigHandler() self.model = Model(self) self.view = View(self) self.sio = socketio.Client() def main(self): print("In Main Controller") self.view.mainloop() def secPerUnitHelp(self): tk.messagebox.showinfo("Help", "Seconds per Unit means:\n\nThe actual ammount of seconds an alert is beeing send to the specified DMX channel is multiplied by the ammount of Bits/Currency (whole $/€ - not cents)\n\nFor example: Alert is set to 0.2 seconds and someone sent 30 Bits.\n\nDuration of alert is 0.2 seconds * 30 = 6 seconds") def doStuff(self): #print(self.config.ini['follow']['dmxchannel']) self.dmxstartstop() #self.startSocketListener() pass def dmxstartstop(self): #print("contr - dmxstartstop called") self.model.dmxstartstop(frequency=20) def start_listening(self,stop=False): #print(f"contr - startlistening called") self.view.updateConfigData() if stop==False: try: self.model.dmxstartstop(frequency=20,port=self.config.collection.data['communication']['port']) self.startSocketListener() self.view.disableAlerts(state='disabled') self.view.changeStartButton() self.view.addToList("Started listening...") except serial.serialutil.SerialException as e: tk.messagebox.showerror("Error",str(e)) except BaseException as ex: tk.messagebox.showerror("Error",str(ex)) else: self.view.disableAlerts(state='normal') self.sio.disconnect() self.model.dmxstartstop(onlystop=True) self.view.changeStartButton() self.view.addToList("Stopped listeneing...") def clicked_save(self): self.view.updateConfigData() self.config.saveData() tk.messagebox.showinfo("Save...", "Settings saved") def clicked_getToken(self): message = ("In order to get the token do the following:\n\n\n" " - Go to Streamlabs Dashboard -> Settings -> API Settings\n (Browser should open automatically afer you've clicked yes)\n\n" " - Go to \"API Settings\"\n\n" " - Copy the Socket API Token (the longer one) into the textfield left to the Button you've just clicked\n\n" "ATTENTION: Be absolutely sure that the token is correct, as it is not checked for now.\n" "If the Socket API Token is not correct you will NOT receive any events!\n\n" "Continue with opening you Browser?") MsgBox = tk.messagebox.askquestion ('Get API Token',message) if MsgBox == 'yes': webbrowser.open('https://streamlabs.com/dashboard/#/settings/api-settings', new=1) def on_exit(self): self.model.dmxstartstop(onlystop=True) self.sio.disconnect() print("exiting....") ########### # Websocket stuff ###########w def startSocketListener(self): url_prefix= 'https://sockets.streamlabs.com/socket.io/?token=' url_with_token= url_prefix + self.config.collection.data['token']['sockettoken'] self.sio.on('event', self.message_handler) # self.wst = threading.Thread(target=self.sio.connect(url_with_token)) # self.wst.daemon = True # self.wst.start() self.sio.connect(url_with_token) #when socket.io message "event" is received (defined with sio.on 'eventname') def message_handler(self,msg): #print('Received message, type= ',msg['type']) self.view.addToList('Received message, type: '+msg['type']) self.model.got_message(msg,self.config.collection.eventlist)