def onMediaMessage(self, entity): by = entity.getFrom(False) id = entity.getId() name = entity.getNotify() preview = None caption = "" encrypted = entity.isEncrypted url = None # Audio clips do not have captions if entity.getMediaType().capitalize() != "Audio": caption = entity.getCaption() if encrypted: # decrypt the media filename = "%s/%s%s"%(tempfile.gettempdir(),entity.getId(),entity.getExtension()) logger.info("Media file name %s" %filename) with open(filename, 'wb') as file: file.write(entity.getMediaContent()) attempts = 0 file_id = "%s/%s" %(self.account.id, os.path.basename(filename)) logger.info("File key %s" %file_id) while(url is None and attempts < 1): file = open(filename, 'r') conn = S3Connection(get_env('aws_key_id'), get_env('aws_secret_access_key')) bucket = conn.get_bucket(get_env('aws_s3_bucket')) key = Key(bucket) key.key = file_id key.set_contents_from_filename(filename) key.set_acl('public-read') url = key.generate_url(expires_in=0, query_auth=False) logger.info("Uploaded to %s" %url) attempts += 1 else: url = entity.url if url is not None: logger.info("Uploaded file to %s" %url) data = {'message': {'url': url, 'message_type': entity.getMediaType().capitalize(), 'phone_number': by, 'whatsapp_message_id': id, 'name': name, 'caption': caption }} self._post('upload', data) # send receipts lower self.toLower(entity.ack())
def __init__(self): self.build_secret = None self.storage_secret = None try: with open("/secrets/cloudbuild/cloudbuild-secret", "r") as self.build_secret_file: self.build_secret = self.build_secret_file.read() except Exception as e: print(e) try: with open("/secrets/cloudstorage/cloudstorage-secret", "r") as self.storage_secret_file: self.storage_secret = self.storage_secret_file.read() except Exception as e: print(e) self.gcp_project = get_env("GCP_PROJECT") self.source_bucket = get_env("SOURCE_BUCKET") self.cloudbuild_bucket = get_env("CLOUDBUILD_BUCKET") self.issuer_name = get_env("ISSUER_NAME") self.compute_zone = get_env("COMPUTE_ZONE") self.cluster_name = get_env("CLUSTER_NAME") self.dns_name = get_env("DNS_NAME") self.dns_ttl = get_env("DNS_TTL", "60")
def __init__(self, phone_number): self.connected = False self.phone_number = phone_number setup_logging(phone_number) self.logger = logging.getLogger(__name__) environment = get_env('env') rollbar_key = get_env('rollbar_key') self.yowsup_env = get_env('yowsup_env', False, 's40') # initialize rollbar for exception reporting rollbar.init(rollbar_key, environment)
def main(): args = get_args() # Get commandline arguments. expert_data = expert_policy.get_expert_data(args) # Generate expert data. env, oa_dim = util.get_env(args.env_name) policy = BCPolicy(oa_dim, max_steps=50000) # Untrained policy pi(a|o) policy.train(expert_data) # Train policy using Behaviour Cloning rollout_policy(policy, env, args)
def init_db(self): url = get_env('db') self.db = create_engine(url, echo=False, pool_size=1, pool_timeout=600, pool_recycle=600) self.session = sessionmaker(bind=self.db) logger.debug("Setup the db connection")
def get_expert_data(args): # TODO: docstring if args.load_expert_data: expert_data = load_expert_data(args.load_expert_data) else: env, _ = util.get_env(args.env_name) with tf.Session(): tf_util.initialize() expert_data = ExpertPolicy(env, args).run_expert() return expert_data
def _post(self, url, payload): post_url = get_env('url') + url payload.update(account=self.phone_number) logger.info('Sending payload : %s' % payload) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } response = requests.post(post_url, data=json.dumps(payload), headers=headers)
def loop(self): # set the yowsup environment - not supported in fork # YowsupEnv.setEnv(self.yowsup_env) stackBuilder = YowStackBuilder() # Create the default stack (a pile of layers) and add the Ongair Layer to the top of the stack stack = stackBuilder.pushDefaultLayers(True).push(OngairLayer).build() ping_interval = int(get_env('ping_interval')) # Set the phone number as a property that can be read by other layers stack.setProp(YowIqProtocolLayer.PROP_PING_INTERVAL, ping_interval) stack.setProp('ongair.account', self.phone_number) stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) # whatsapp server address stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) # Broadcast the login event. This gets handled by the OngairLayer stack.broadcastEvent(YowLayerEvent(OngairLayer.EVENT_LOGIN)) try: # Run the asyncore loop stack.loop(timeout=0.5, discrete=0.5) # this is the program mainloop except AttributeError: # for now this is a proxy for ProtocolException i.e. where yowsup has tried to read an # attribute that does not exist self.logger.exception("Attribute error") rollbar.report_exc_info() sys.exit(0) except AssertionError: # this is a proxy for a wrong expected attribute self.logger.exception("Assertion error") rollbar.report_exc_info() sys.exit(0) except KeyboardInterrupt: # manually stopped. more a development debugging issue self.logger.info("Manually interupted") sys.exit(2) except PingTimeoutError: self.logger.info("Ping timeout error") sys.exit(2) except RequestedDisconnectError: self.logger.info("We requested to disconnect") sys.exit(2) except ConnectionClosedError: self.logger.info("Disconnected") sys.exit(2) except: self.logger.exception("Unknown error") rollbar.report_exc_info() # do not restart as we are not sure what the problem is sys.exit(0)
Generated by 'django-admin startproject' using Django 1.8.2. For more information on this file, see https://docs.djangoproject.com/en/1.8/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.8/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os from util import get_env BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ENV = get_env(BASE_DIR) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '=0mjd*(*lx3*#uivvf_c(k-#r1&@3snh)65zpj7e@)ib$njpl8' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] TEMPLATE_DEBUG = True # Application definition
def onMediaMessage(self, entity): by = entity.getFrom(False) id = entity.getId() name = entity.getNotify() preview = None caption = "" encrypted = entity.isEncrypted url = None # Audio clips do not have captions if entity.getMediaType().capitalize() != "Audio": caption = entity.getCaption() if encrypted: # decrypt the media filename = "%s/%s%s" % (tempfile.gettempdir(), entity.getId(), entity.getExtension()) logger.info("Media file name %s" % filename) with open(filename, 'wb') as file: file.write(entity.getMediaContent()) attempts = 0 file_id = "%s/%s" % (self.account.id, os.path.basename(filename)) logger.info("File key %s" % file_id) while (url is None and attempts < 1): file = open(filename, 'r') conn = S3Connection(get_env('aws_key_id'), get_env('aws_secret_access_key')) bucket = conn.get_bucket(get_env('aws_s3_bucket')) key = Key(bucket) key.key = file_id key.set_contents_from_filename(filename) key.set_acl('public-read') url = key.generate_url(expires_in=0, query_auth=False) logger.info("Uploaded to %s" % url) attempts += 1 else: url = entity.url if url is not None: logger.info("Uploaded file to %s" % url) data = { 'message': { 'url': url, 'message_type': entity.getMediaType().capitalize(), 'phone_number': by, 'whatsapp_message_id': id, 'name': name, 'caption': caption } } self._post('upload', data) # send receipts lower self.toLower(entity.ack())
# -*- coding: utf-8 -*- import os from util import get_env PROJECT_NAME = u'NUKLEO' BASE_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) ENV = get_env(BASE_DIR) SECRET_KEY = ENV.get('SECRET_KEY', ENV.get('SECRET_KEY')) DEBUG = True ALLOWED_HOSTS = ENV.get('ALLOWED_HOSTS', []) SITE_URL = ENV.get('URL_SITE', '') # EMAIL EMAIL_HOST = ENV.get('EMAIL_HOST', '') EMAIL_HOST_USER = ENV.get('EMAIL_HOST_USER', '') EMAIL_HOST_PASSWORD = str(ENV.get('EMAIL_HOST_PASSWORD', '')) DEFAULT_FROM_EMAIL = ENV.get('DEFAULT_FROM_EMAIL', '') SERVER_EMAIL = ENV.get('SERVER_EMAIL', '') EMAIL_PORT = ENV.get('EMAIL_PORT', '') EMAIL_USE_TLS = True DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': ENV.get('DB_NAME'), 'USER': ENV.get('DB_USER'), 'PASSWORD': ENV.get('DB_PASSWORD'), 'HOST': 'localhost',
def _post(self, url, payload): post_url = get_env('url') + url payload.update(account=self.phone_number) logger.info('Sending payload : %s' %payload) headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} response = requests.post(post_url, data=json.dumps(payload), headers=headers)