def get_initial_company_info(): """Gets the initial information for each company""" company_dict = get_company_dict() for company in company_dict: # Gets symbol for company if company_dict[company]["symbol"] == "unknown": try: with urllib.request.urlopen( f'https://finance.yahoo.com/_finance_doubledown/' f'api/resource/searchassist;searchTerm={company_dict[company]["companyName"]}') as response: html = response.read().decode() d = json.loads(html) company_dict[company]["symbol"] = d['items'][0]['symbol'] except urllib.error.HTTPError as error: logging.debug(error) break # Gets initial share price if company_dict[company]["initialSharePrice"] == 1: stock = Pinance(company_dict[company]["symbol"]) stock.get_quotes() share = stock.quotes_data["regularMarketPrice"] company_dict[company]["initialSharePrice"] = float(share) company_dict[company]["currentSharePrice"] = float(share) with open(MONITOR, "w") as f: json.dump(company_dict, f, sort_keys=True, indent=4, ensure_ascii=False)
def get_current_shares(): """Gets current shares, writes updated version back to json""" company_dict = get_company_dict() gmt = datetime.now() market_time = gmt.replace(hour=abs(gmt.hour - 5)) # Ensure market is open (opening hours 9:30 - 4 EST) if int(str(market_time.hour) + str(market_time.minute if market_time.minute >= 10 else f'0{market_time.minute}')): for company in company_dict: try: stock = Pinance(company_dict[company]["symbol"]) stock.get_quotes() share = stock.quotes_data["regularMarketPrice"] # Gets the current share price, replaces the "current" # and adds to the sharePriceList curr_day = str(company_dict[company]["day"]) company_dict[company]["currentSharePrice"] = float(share) company_dict[company]["sharePriceList"][curr_day].append(("{:%d-%m-%Y %H:%M:%S}".format(datetime.now()), float(share))) # Gets the current share change share_change = 1.0 - (company_dict[company]["initialSharePrice"] / company_dict[company]["currentSharePrice"]) company_dict[company]["shareChange"] = share_change except TypeError as error: # Will catch the error if share returns a value other than a float logging.debug(error) with open(MONITOR, "w") as f: json.dump(company_dict, f, sort_keys=True, indent=4, ensure_ascii=False)
async def handle_DATA(self, server, session, envelope): """ Callback method which is called every time we receive message. Here we do all the magic. :param server: not used :param session: not used :param envelope: received message :return: SMTP status string """ logging.info('Incoming message. Handling data') message = email_management.Mutator(envelope.content) destination = file_uploader.Uploader(config.upload.service, config.upload.creds, config.upload.path) if not destination.service: logging.error(f'Cannot authorize to "{config.upload.service}"') return '554 Authorization failed for filesharing service' with open_fs('mem://') as ramdisk: attachments = message.extract_attachments(ramdisk) if attachments: if not destination.upload(attachments, ramdisk): logging.error('Cannot upload files') return '554 Error while uploading attachments to filesharing service' message.content = message.strip_attachments() message.content = message.add_links(attachments) elif attachments is not None: logging.debug('No attachments, passing mail untouched') else: return '554 Cannot save attachment(s) to process' sendmail = mail_sender.Sender(config.mail) error = sendmail.service(message.content) return error or '250 Message accepted for delivery'
async def smtp_server(hostname, port): """Function to start aiosmtpd as receiving SMTP server on defined hostname and port""" logging.debug('Starting SMTP server') handler = AttachHarvester() controller = SMTPController(handler, hostname=hostname, port=port) controller.start() logging.debug('SMTP server started')
def get_latest_tweet(self): """Checks the twitter handle for new tweets. If there has been a new tweet, it will return the Tweet to be checked for companies""" try: latest_tweet = self.api.user_timeline(screen_name=self.handle, count=1)[0] tweet = latest_tweet.text.encode('ascii', 'ignore').decode( 'utf-8') # Removes emjois with open(f'{LATEST_TWEET}{self.handle}.txt', "r") as f: old_tweet = f.read() if tweet != old_tweet: with open(f'{LATEST_TWEET}{self.handle}.txt', 'w') as f: f.write(tweet) self.tweet_id = latest_tweet.id_str self.tweet = tweet return tweet except tweepy.TweepError as error: logging.debug(error)
def check_mentions(self): """Checks mentions for sign up's via email or twitter via "Sign up / Sign up [email]""" try: mentions = self.api.mentions_timeline(count=3) for mention in mentions: if "stop" in mention.text.lower(): # Unsubscribe for email if len(mention.text.split()) == 3: email = mention.text.split()[2] with open(EMAILS, 'r') as f: email_list = f.read().split() if email in email_list: email_list.remove(email) with open(EMAILS, 'w') as f: f.write(' '.join(email_list)) # Unsubscribe for Twitter handle else: twitter_name = mention.user.screen_name with open(TWITTER_NAMES, 'r') as f: twitter_name_list = f.read().split() if twitter_name in twitter_name_list: twitter_name_list.remove(twitter_name) with open(TWITTER_NAMES, 'w') as f: f.write(' '.join(twitter_name_list)) elif "sign up" in mention.text.lower(): # Email sign up if len(mention.text.split()) > 3: email = mention.text.split()[3] with open(EMAILS, 'r') as f: email_list = f.read().split() if email not in email_list: with open(EMAILS, 'a') as f: f.write(f' {email}') # Twitter handle sign up else: twitter_name = mention.user.screen_name with open(TWITTER_NAMES, 'r') as f: twitter_name_list = f.read().split() if twitter_name not in twitter_name_list: with open(TWITTER_NAMES, 'a') as f: f.write(f' {twitter_name}') except tweepy.TweepError as error: logging.debug(error)
def __init__(self, level): """Initializing of WithoutOneLevelLogs class. Args: level (logging level): only this level will not be caught """ logging.debug('initializing of WithoutOneLevelLogs class') logging.debug('arguments for __init__ and another locals: %s', locals()) self.level = level
def sendInt(self, mystring, myint): if useredis: params = r.get("PARAMS") params = json.loads(params) params[mystring] = myint paramsdump = json.dumps(params) r.mset({"PARAMS": paramsdump}) logging.debug(f'Input {mystring} set: {myint}') self.modeSelected.emit(self._currMode)
def share_output(self): """Calls difference_in_shares from the Companies module, Outputs the data to twitter.""" share_dict = company.get_company_dict() for comp in share_dict: try: self.api.update_status( f'Since {share_dict[comp]["handle"]} mentioned {comp.upper()}, {share_dict[comp]["day"]} days ago, ' f'their shares have changed from {share_dict[comp]["initialSharePrice"]:.2f} to ' f"{share_dict[comp]['currentSharePrice']:} that's a {share_dict[comp]['shareChange']:.3f}% change!" ) except tweepy.TweepError as error: logging.debug(error)
def initial_tweet(self, matches): """Tweets when a company is mentioned, along with it's sentiment.""" sentiment = self.sentiment_analysis() sentiment_dict = { "positive": u"\U00002705", "negative": u"\U0000274E", "neutral": u"\U00002796" } for comp in matches: try: self.api.update_status( f'{self.handle} just mentioned {comp.upper()} {sentiment}ly ' f'in their latest tweet! ' f'https://twitter.com/{self.handle}/status/{self.tweet_id}' ) except tweepy.TweepError as error: logging.debug(error)
def setStatus(self, status): logging.debug("status set: {}".format(status)) self._status = status
def setTrigger(self, trigger): self._currTrigger = trigger if trigger == "": logging.debug("trigger reset") else: logging.debug(f'trigger type set: {trigger}')
def setBreath(self, breath): self._currBreath = breath if breath == "": logging.debug("breath reset") else: logging.debug(f'breath type set: {breath}')
def setMode(self, mode): self._currMode = mode if mode == "": logging.debug("mode reset") else: logging.debug(f'mode set: {mode}')
def set_common_env(*args, **kwargs): """Context manager that set envs.common atributes. If one of args will be 'clean' = False, envs.common will be saved in updated state. Else envs.common will be reverted to previous state after 'with' statement. Args: *args (tuple): if argument is dict, env.common will be updated by this dict and if argument is function, env.common will be updated by executing this function and if argument is just number, string or object, it will be used as key with value = True **kwargs (dict): env.common will be updated by this dict Returns: envs.common object with most of all atributes Examples: >>> with set_common_env('test', test1='test1') as common_env: ... common_env.__dict__ # doctest: +NORMALIZE_WHITESPACE {'test1': 'test1', 'functions': {}, 'ssh_port_option': '-p', 'split_user': '******', 'split_function': ':', 'ssh_port': 22, 'scp_binary': 'scp', 'user': ..., 'arithmetic_symbols': ('=', '!', '>', '<', '+', '-', '*', '/', '%'), 'split_port': ':', 'scp_port_option': '-P', 'hosts': ['localhost'], 'default_shell': 'sh', 'split_args': ',', 'test': True, 'ssh_binary': 'ssh', 'split_hosts': ',', 'parallel': False, 'localhost': ['localhost', '127.0.0.1', ...], 'interactive': True} """ logging.debug('initializing of set_common_env') logging.debug('arguments and another locals: %s', locals()) try: dict={} if args: for a in args: if type(a) is dict: dict.update(a) elif callable(a): try: dict.update(a()) except: logging.warning() continue else: dict[a] = True if kwargs: dict.update(kwargs) clean = dict.pop('clean', True) if clean: old = {} new = [] for key in dict.keys(): try: old [key] = envs.common[key] except KeyError: new.append(key) envs.common.update(dict) yield envs.common finally: if clean: logging.debug('reverted global envs.common to previous state') envs.common.update(old) for k in new: del envs.common[k]
def set_connect_env(connect_string, con_args=''): """Context manager that set envs.connect atributes. Args: connect_string (str): [user@]host[:port] con_args (str): options for ssh Connect_env attibutes: connect_string (str): [user@]host[:port] user (str): username for connect, default is getpass.getuser() host (str): hostname or ip for connect port (str): port for connect con_args (str): options for ssh logger (logging.logger object): logger object for this connect check_is_root (bool): True if connected as root, else False Returns: envs.connect object with most of all atributes Examples: >>> from api import * >>> with set_connect_env('user@host:port', '') as connect_env: ... connect_env.__dict__ # doctest: +NORMALIZE_WHITESPACE user@host in: id -u user@host err: Bad port 'port' <BLANKLINE> {'check_is_root': False, 'connect_string': 'user@host:port', 'con_args': '', 'host': 'host', 'user': '******', 'logger': ..., 'port': 'port'} """ logging.debug('initializing of set_connect_env') logging.debug('arguments and another locals: %s', locals()) try: # save envs.connect that was before old_dict = {} old_dict.update(envs.connect.__dict__) # check if connect already exists cs = connect_string if not envs.common.split_user in cs: cs = envs.common.split_user.join((envs.common.user, cs)) if not envs.common.split_port in cs: cs = envs.common.split_port.join((cs, str(envs.common.ssh_port))) if cs in connects.keys(): envs.connect.replace(connects[cs]) yield envs.connect else: envs.connect.connect_string = connect_string if envs.common.split_user in connect_string: envs.connect.user, connect_string = connect_string.split( envs.common.split_user ) else: envs.connect.user = envs.common.user if envs.common.split_port in connect_string: envs.connect.host, envs.connect.port = connect_string.split( envs.common.split_port ) else: envs.connect.host = connect_string envs.connect.port = envs.common.ssh_port envs.connect.con_args = con_args logger_name = ''.join((envs.connect.user, envs.common.split_user, envs.connect.host )) if logger_name in logging.root.manager.loggerDict.keys(): envs.connect.logger = logging.getLogger(logger_name) else: envs.connect.logger = logging.getLogger(logger_name) # add logging to interactive output if envs.common.interactive: logging.debug('adding logging to interactive output') # only info for stdout info = logging.StreamHandler(sys.stdout) info.addFilter(OnlyOneLevelLogs(logging.INFO)) info.setFormatter(logging.Formatter('%(name)s %(message)s')) envs.connect.logger.addHandler(info) # all another to stderr error = logging.StreamHandler(sys.stderr) error.addFilter(WithoutOneLevelLogs(logging.INFO)) error.setFormatter(logging.Formatter('%(name)s %(message)s')) envs.connect.logger.addHandler(error) from operations import check_is_root with hide('stdout'): envs.connect.check_is_root = check_is_root() logging.debug('envs.connect: %s', envs.connect) connects[cs] = envs.connect.__dict__ yield envs.connect finally: # Reinitialized global envs.connect as AttributedDict class. logging.debug('reinitialization global envs.connect as AttributedDict class') envs.connect.replace(old_dict)