예제 #1
0
  def get(self):
    urlfetch.set_default_fetch_deadline(120)

    bq = bigquery.BigQuery()

    current_events = []
    events = []
    for master_name in constants.MASTER_NAMES:
      builders = buildbot.Builders(master_name)
      available_builds = _AvailableBuilds(builders)
      recorded_builds = _RecordedBuilds(bq, builders, available_builds)
      for builder in builders:
        # Filter out recorded builds from available builds.
        build_numbers = (available_builds[builder.name] -
                         recorded_builds[builder.name])
        builder_current_events, builder_events = _TraceEventsForBuilder(
            builder, build_numbers)
        current_events += builder_current_events
        events += builder_events

    jobs = []
    if current_events:
      jobs += bq.InsertRowsAsync(
          constants.DATASET, constants.CURRENT_BUILDS_TABLE,
          current_events, truncate=True)
    if events:
      jobs += bq.InsertRowsAsync(constants.DATASET, constants.BUILDS_TABLE,
                                 events)

    for job in jobs:
      bq.PollJob(job, 60 * 20)  # 20 minutes.
예제 #2
0
	def get(self):
		urlfetch.set_default_fetch_deadline(60)
		q = self.request.get('q')
		query = q.replace(" ", "%20") 
		movies, count = getMovieList(query)
		print(movies)
		self.render('test.html', movies = movies, query = q, count = count)
예제 #3
0
파일: main.py 프로젝트: SalamiArmy/Thorin
    def post(self):
        urlfetch.set_default_fetch_deadline(120)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        if 'message' in body or 'edited_message' in body:
            message = body['message'] if 'message' in body else body['edited_message']
            text = message.get('text')
            fr = message.get('from')
            user = fr['username'] \
                if 'username' in fr \
                else fr['first_name'] + ' ' + fr['last_name'] \
                if 'first_name' in fr and 'last_name' in fr \
                else fr['first_name'] if 'first_name' in fr \
                else 'Dave'
            if 'edited_message' in body:
                user += '(edited)'
            chat = message['chat']
            chat_id = chat['id']
            chat_type = chat['type']

            if not text:
                logging.info('no text')
                return

            if text.startswith('/'):
                self.TryExecuteExplicitCommand(chat_id, user, text, chat_type)
예제 #4
0
def send_request(fields):
  config = model.Config.get()

  fields["VERSION"] = "113"
  fields["USER"] = config.paypal_user
  fields["PWD"] = config.paypal_password
  fields["SIGNATURE"] = config.paypal_signature

  form_data = urllib.urlencode(fields)

  urlfetch.set_default_fetch_deadline(30)
  result = urlfetch.fetch(url=config.paypal_api_url, payload=form_data, method=urlfetch.POST,
                          headers={'Content-Type': 'application/x-www-form-urlencoded'})
  result_map = urlparse.parse_qs(result.content)

  if 'ACK' in result_map:
    if result_map['ACK'][0] == "Success" or result_map['ACK'][0] == "SuccessWithWarning":
      return (True, result_map)

    logging.warning("Paypal returned an error:")
    logging.warning(pprint.pformat(result_map))
    return (False, result_map)

  logging.warning("Could not contact Paypal:")
  logging.warning(result.content)
  return False, result.content
예제 #5
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info("request body:")
        logging.info(body)
        self.response.write(json.dumps(body))

        hangman = t.Hangman(body["message"])  # Constrói o objeto principal
        # print(hangman)
        data = bd.configure(hangman)  # Magia negra ou gambiarra? Fica ai a duvida

        shout = bd.getShout(data)
        if shout:
            bd.checkChatBd(shout[0])
            try:
                reply(c.toDict(shout[0], shout[1].encode("utf-8")))
            except Exception, e:
                logging.info((str(e) + " = " + shout[0].encode("utf-8")))
                if str(e) == "HTTP Error 403: Forbidden":
                    bds.delChat(shout[0])
                    bds.lessPos()
                    reply(c.toDict("-27626712", ("Chat " + shout[0].encode("utf-8") + " excluído")))
                else:
                    time.sleep(0.5)
                    try:
                        reply(c.toDict(shout[0], shout[1].encode("utf-8")))
                    except Exception, e:
                        print(e)
예제 #6
0
파일: main.py 프로젝트: yosit/kinnernet_bot
    def post(self):
        urlfetch.set_default_fetch_deadline(60)

        logging.debug('Request: %s' % self.request.body)
        body = json.loads(self.request.body)
        update = telegram.Update.de_json(body)

        chat_id = update.message.chat.id
        message = update.message.text

        # if '/start' in message or '/help' in message:

        if message is not None:
            bot.sendMessage(chat_id=chat_id,
                            text=kinnernet_board.handle_request(message))

        # image = None
        # if '/latest' in message:
        #     image = DevOpsReactions.latest()
        # if '/random' in message:
        #     image = DevOpsReactions.random()

        # if image:
        #     bot.sendMessage(chat_id=chat_id,
        #                     text=image['title'])
        #     bot.sendChatAction(chat_id=chat_id,
        #                        action=telegram.ChatAction.UPLOAD_PHOTO)
        #     bot.sendDocument(chat_id=chat_id,
        #                      document=image['image_url'])

        self.response.write(json.dumps(body))
예제 #7
0
    def post(self):
        urlfetch.set_default_fetch_deadline(45)
        httplib2.Http(timeout=45)
        userid = self.request.get('userid')
        drive_service = util.create_service(
        'drive', 'v2',
        StorageByKeyName(Credentials, userid, 'credentials').get())

        id = self.request.get('id')
        logging.debug(id)
        content_type = self.request.get('contentType')
        retry_params = gcs.RetryParams(backoff_factor=1.1)
        img = gcs.open('/processed_images/' + id, 'r',
                       retry_params=retry_params)
        media_body = MediaIoBaseUpload(
            img, mimetype=content_type, resumable=True)
        logging.info(media_body)
        try:
            file = drive_service.files().insert(
                media_body=media_body,
                ocr=True,
                convert=True
            ).execute()



            taskqueue.add(url='/drivefetch', params={'fileid': file['id'], 'id': id, 'userid':userid})
        except Exception, e:
            logging.error(e)
예제 #8
0
def get_jira_client():
    # Bump up the default fetch deadline.
    # This setting is thread-specific, which is why we set it here.
    urlfetch.set_default_fetch_deadline(60)
    return jira.client.JIRA({'server': app.config['JIRA_API_BASE']},
                            basic_auth=(app.config['JIRA_USERNAME'],
                                        app.config['JIRA_PASSWORD']))
예제 #9
0
	def get(self):
		urlfetch.set_default_fetch_deadline(45)
		url = "http://www.reddit.com/r/iama"
		try:
			result = urllib2.urlopen(url)
			result = result.read()
			soup = BeautifulSoup(result)
			tables = soup.findAll('table')
			soup = BeautifulSoup(unicode(tables[0]))
			tds = soup.findAll('td')
			amas = []
			for i in range(0, len(tds), 4):
				date = tds[i].text
				time = tds[i + 1].text
				name = tds[i + 2].text
				description = tds[i + 3].text
				while """ in description:
					x = description.find(""")
					description = description[0:x]+description[x+6:len(description)]
				d = readDateTime(date, time)
				ama = {"datetime":str(d), "name":str(name), "description":description}
				soup1 = BeautifulSoup(unicode(tds[i+2]))
				for a in soup1.findAll('a', href = True):
					ama.update({"link":a['href']})
				amas.append(ama)
			self.response.write(json.dumps({"amas":amas, "status":"success"}))
		except urllib2.URLError, e:
			self.response.write(json.dumps({"amas":[], "status":"failure"}))
예제 #10
0
파일: vechai.py 프로젝트: NamPNQ/pytaku
    def search_series(self, keyword):
        d = datetime.now()
        url = 'http://vechai.info/search/items.js?v=%s-%s-%s' % (d.day, d.month + 1, d.hour)
        urlfetch.set_default_fetch_deadline(60)
        resp = urlfetch.fetch(url)
        if resp.status_code != 200:
            return []
        try:
            results = eval(resp.content.replace('var items = ', '').replace(';', ''))
            returns = []

            for r in results:
                try:
                    title = unicode(r[0].decode('utf8'))
                except:
                    continue
                r[0] = unidecode(title)
                if keyword.lower() in r[0].lower():
                    returns.append({
                        'name': r[0],
                        'url': r[1],
                        'site': 'vechai',
                    })
            return returns
        except BaseException, e:
            logging.error(e)
            return []
예제 #11
0
파일: util.py 프로젝트: VertNet/usagestats
def api_query(api_url, params):
    """Launch query to an API.

Send the specified query and retrieve the specified field.
"""
    urlfetch.set_default_fetch_deadline(60)
    finished = False
    max_retries = 3
    retries = 0
    while not finished:
        retries += 1
        if retries >= max_retries:
            err_msg = "Query failed after maximum retries"
            logging.error(err_msg)
            raise ApiQueryMaxRetriesExceededError(err_msg)
        d = urlfetch.fetch(
            url=api_url,
            method=urlfetch.POST,
            payload=urlencode(params)
        ).content
        d = json.loads(d)
        if "error" in d.keys():
            logging.warning("Warning, something went wrong with the query.")
            logging.warning(d['error'])
            logging.warning("This is the call that caused it:")
            logging.warning(api_url)
            logging.warning(urlencode(params))
            logging.warning("Retrying in 3 seconds"
                            " attempt %d of %d" % (retries+1, max_retries))
            time.sleep(3)
        else:
            finished = True
            logging.info("Got response from %s" % api_url)
            return d
예제 #12
0
    def open(self, url='', data=None, params=None, headers=None, method='GET'):
        # Create a correct absolute URL and set it.
        self.url = absoluteURL(self.url, url)
        logging.info(self.url)

        # Create the full set of request headers
        requestHeaders = self.requestHeaders.copy()
        if headers:
            requestHeaders.update(headers)

        # Let's now reset all response values
        self._reset()

        # Store all the request data
        self._requestData = (url, data, params, headers, method)

        # Make a connection and retrieve the result
        pieces = urlparse.urlparse(self.url)
        #if pieces[0] == 'https':
            #connection = self.sslConnectionFactory(pieces[1])
        #else:
            #connection = self.connectionFactory(pieces[1])
        urlfetch.set_default_fetch_deadline(30)
        try:
            response = requests.request(method, url=self.url, data=data, headers=requestHeaders)
            #response = urlfetch.fetch(url=self.url, method=getattr(urlfetch, method), headers=requestHeaders, deadline=30)
            #connection.request(
                #method, getFullPath(pieces, params), data, requestHeaders)
            #response = connection.getresponse()
        except Exception, e:
            #logging.error(self.url, repr(e))
            #connection.close()
            #self.status, self.reason = e.args
            raise e
예제 #13
0
    def inspiration_search(self, origin, destination=None, departure_date=None, duration=None, direct=None,
                           max_price=None, aggregation_mode="DAY"):
        """ Amadeus Inspiration Search """
        uri = "http://api.sandbox.amadeus.com/v1.2/flights/inspiration-search?"
        uri_dict = {}
        if origin is None:
            return False
        else:
            uri_dict["origin"] = origin

        uri_dict["destination"] = destination
        uri_dict["departure_date"] = departure_date
        uri_dict["duration"] = duration
        uri_dict["direct"] = direct
        uri_dict["max_price"] = max_price
        uri_dict["aggregation_mode"] = aggregation_mode
        uri_dict["apikey"] = self.api_key

        query = ""
        for parm in uri_dict:
            if uri_dict[parm] is not None:
                query += parm + "=" + str(uri_dict[parm]) + "&"

        url = uri + query[:-1]

        try:
            urlfetch.set_default_fetch_deadline(60)
            return json.load(urllib2.urlopen(url))
        except Exception:
            return False
예제 #14
0
    def get(self):
        if self.session.get('is_valid') == True:
            urlfetch.set_default_fetch_deadline(60)

            # Retrieve existing user aspirations
            aspirations_html = ""
            try:
                mylist = app_datastore.get_aspirations(self.session['student_id']).aspirations
                for item in mylist:
                    aspirations_html += "<li>" + item + "</li>"
            except Exception:
                pass

            # Retrieve a list of all aspirations from the datastore
            all_aspirations_html = ''
            try:
                all_aspirations_html = app_datastore.get_all_asp()
            except Exception:
                pass
            print all_aspirations_html

            # Prepare template values and template
            template_values = {
                'student_name': self.session.get('student_name'),
                'student_email': self.session.get('student_email'),
                'existing_aspirations': aspirations_html,
                'all_aspirations': all_aspirations_html
            }
            template = jinja_environment.get_template('aspirations.html')

            # Write page
            self.response.out.write(template.render(template_values))
        else:	
            self.redirect(app_domain)
예제 #15
0
def parse_rcsb_search_entries(search_words):
    url = 'http://www.rcsb.org/pdb/search/navbarsearch.do?'
    url += urllib.urlencode({'q': search_words})
    urlfetch.set_default_fetch_deadline(45)
    response = urlfetch.fetch(url)

    soup = BeautifulSoup(response.content)

    entries = []
    for query in soup.find_all(class_='query'):

        entry = {}

        entry['pdb_id'] = query.find(class_='qrb_structid').text.strip()
        entry['title'] = query.find(class_='qrb_title').text.strip()
        entry['authors'] = ' '.join([e.text for e in query.find_all(class_='seAuthors')])

        for key_val in query.find_all(class_='se_key'):
            key = key_val.text.strip()
            val = None
            if key_val.next_sibling and key_val.next_sibling.next_sibling:
                val = ' '.join(key_val.next_sibling.next_sibling.stripped_strings)
            if key == 'Residue Count':
                entry['n_residue'] = val
            elif key == 'Release:':
                entry['year'] = val[:4]
            elif key == 'Experiment:':
                entry['experiment'] = ' '.join(val.split())
        entries.append(entry)

    return entries
예제 #16
0
 def GetGeonamesResponse(self, query):
     if query in self.name_mappings:
         query = self.name_mappings[query].decode('utf-8')
     url = 'http://api.geonames.org/searchJSON?q=%s&maxRows=1&username=anis&featureClass=A&featureClass=P&style=SHORT' % str(urllib.quote((query.encode('utf8'))))
     urlfetch.set_default_fetch_deadline(60)
     response = urlfetch.fetch(url) # response = urllib2.urlopen(url) 
     return json.loads(response.content) #data = json.load(response)
예제 #17
0
파일: fi.py 프로젝트: paultgibbons/uiucfipy
def request_json(url, gae=True):
    '''
    Abstraction of making the call to the url
    '''
    data = None
    try:
        from google.appengine.api import urlfetch
        urlfetch.set_default_fetch_deadline(30)
        response = urlfetch.fetch(url)
        if response.status_code >= 500:
            raise Exception('Error on yahoo server', response.msg)
        if response.status_code >= 400:
            raise Exception('Error in call formatting', response.msg)
        response_string = response.content
        data = json.loads(response_string)
    except:
        import urllib2
        response = urllib2.urlopen(url)
        if response.getcode() >= 500:
            raise Exception('Error on yahoo server', response.msg)
        if response.getcode() >= 400:
            raise Exception('Error in call formatting', response.msg)
        response_string = response.read()
        data = json.loads(response_string)
    return data
예제 #18
0
 def get(self):
     #Delete all the old items in the index
     SGPostal.delete_all_in_index()
 
     #10 json files contains the address info
     for i in range(1):
         jsonUrl = "https://s3-ap-southeast-1.amazonaws.com/clt-friso/%dpostal.json" % i
         logging.debug("Downloading json file %d" %i)
         urlfetch.set_default_fetch_deadline(40)
         result = urlfetch.fetch(jsonUrl)
         if result.status_code == 200:
             #logging.debug("Download complete")
             #logging.debug("Loading json file")
             
             myData = json.loads(result.content)    
             logging.debug("File loaded, total %d items" % len(myData))
             chunks=[myData[x:x+250] for x in xrange(0, len(myData), 250)]
             i = 1
             for chunk in chunks:
                 logging.debug(str(len(chunk)))
                 strChunk = json.dumps(chunk)
                 taskqueue.add(url='/super_admin/parse_postal', countdown = 60, params = {'postalRows': strChunk, "item": i, "total": len(chunks), 'total_items': len(myData)}, queue_name='updatepostal')
                 i += 1
         else:
             logging.debug("File %d not found" % i)                    
예제 #19
0
    def __init__(self, host, appid, api_key, namespace):
        urlfetch.set_default_fetch_deadline(59)

        self.host = host
        self.appid = appid
        self.api_key = api_key
        self.namespace = namespace
예제 #20
0
파일: main.py 프로젝트: pedrofraca/tourapp
def stage_detail_parse(stage_number,url):
    data={}
    urlfetch.set_default_fetch_deadline(45)
    images_json=[]
    data_order=["day","month","avg-speed","cat","start-finish"]
    page = urllib2.urlopen(url)
    soup = BeautifulSoup(page, "html.parser")
    tabulka = soup.find("h3", {"class" : "section"})
    div = tabulka.parent
    images = soup.findAll('img')
    for image in images:
        if "Stage" in image["src"]:
            images_json.append(image["src"])
        if "Final_GC" in image["src"]:
            images_json.append(image["src"])
        if "site-icons" in image["src"]:
            data['stage-icon']=image["src"]
        cont=0

        data['stage-images']=images_json

        for element in tabulka.parent:
            if(cont<len(data_order)):
                if element.name is None and "\n" not in element.string and element.string !=" " and  "Tag for network 919" not in element.string:
                    #The interesting information doesn't have a tag
                    data[data_order[cont]]=element.string
                    cont+=1
    print stage_number
    stage=Stage.get_by_id(int(stage_number))
    stage_data=json.loads(stage.data)
    stage_data.update(data)
    stage.data=json.dumps(stage_data)
    stage.put()
예제 #21
0
def reallyinitEarthEngineService():
    util.positional_parameters_enforcement = util.POSITIONAL_IGNORE   # avoid the WARNING [util.py:129] new_request() takes at most 1 positional argument (4 given)
    OAuthInfo.SCOPE += ' https://www.googleapis.com/auth/fusiontables.readonly'
    print  OAuthInfo.SCOPE
    try:
        if os.environ['SERVER_SOFTWARE'].startswith('Development'): 
            logging.info("Initialising Earth Engine authenticated connection from devserver")
            try:  
                acct = os.environ['MY_SERVICE_ACCOUNT']
                key = os.environ['MY_PRIVATE_KEY_FILE']
            except KeyError: 
                logging.warning("Environment variable MY_SERVICE_ACCOUNT and MY_PRIVATE_KEY_FILE not set. Using settings.py")
                logging.debug (os.environ)
                acct = settings.MY_LOCAL_SERVICE_ACCOUNT
                key = settings.MY_LOCAL_PRIVATE_KEY_FILE
            
            EE_CREDENTIALS = ee.ServiceAccountCredentials(acct, key)
        else:
            logging.info("Initialising Earth Engine authenticated connection from App Engine")
            EE_CREDENTIALS = AppAssertionCredentials(OAuthInfo.SCOPE)
        ee.Initialize(EE_CREDENTIALS) 
        #print EE_CREDENTIALS
        # permit long wait for response from earth engine
        urlfetch.set_default_fetch_deadline(60)
        return EE_CREDENTIALS
    except Exception, e:
        logging.error("Failed to connect to Earth Engine Google API. Exception: %s", e)
        return False
예제 #22
0
    def get(self):
        urlfetch.set_default_fetch_deadline(45)
        template_values = {}

        # Default search-string value
        bg_list = self.request.get('bg-search-string',
                                          "")

        template_values["search_term"] = bg_list        

        if bg_list:
            # Grab a list of the boardgames that match your search string
            params = {"query" : bg_list, "type" : "boardgame"}
            print params
            url = API_BASE_PATH + "xmlapi2/search/" + "?" + urllib.urlencode(params)
            print url
            q_result = urllib.urlopen(url)
            print "good1"
            
            # Make it parseable
            dom = xml.dom.minidom.parseString(q_result.read())
            print "good2"
            
            # Handle all items. This includes the nested query for more information on each boardgame. 
            # In the end we want a list with:
            # name, thumbnail, description, players, time
            template_values["games"] = self.handleItems(dom)

        else:
            pass

        template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render(template_values))
예제 #23
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        message = body['message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']

        if not text:
            logging.info('no text')
            return

        def reply(msg=None, img=None):
            if msg:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'text': msg.encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'reply_to_message_id': str(message_id),
                })).read()
            elif img:
                resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                    ('chat_id', str(chat_id)),
                    ('reply_to_message_id', str(message_id)),
                ], [
                    ('photo', 'image.jpg', img),
                ])
            elif not msg:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'text': "error".encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'reply_to_message_id': str(message_id),
                })).read()
                logging.info("Empty message warning")
            else:
                logging.error('no msg or img specified')
                resp = None

            logging.info('send response:')
            logging.info(resp)

        conclusion = formResponse(text)

        # return values: 0 requires posting a message, 1 posting one as a reply, -1 to do nothing
        if conclusion[0] == 0:
            reply(conclusion[1])
        elif conclusion[0] == 1:
            reply(conclusion[1])
        else:
            # no response needed
            pass
예제 #24
0
    def get(self):

        params = {
            'decimalLatitude': self.request.get('decimalLatitude'),
            'decimalLongitude': self.request.get('decimalLongitude'),
            'countryCode': self.request.get('countryCode'),
            'scientificName': self.request.get('scientificName')
        }

        data = urlencode(params)

        urlfetch.set_default_fetch_deadline(URLFETCH_DEADLINE)

        url = "http://"+modules.get_hostname(module=MODULE)+"/geospatial/singlerecord"
        result = urlfetch.fetch(
            url=url,
            payload=data,
            method=urlfetch.POST,
            headers={"Content-Type":"application/x-www-form-urlencoded"}
        )

        fl = ast.literal_eval(result.content)
        params['flags'] = fl

        self.response.headers["Access-Control-Allow-Origin"] = "*"
        self.response.headers["Access-Control-Allow-Headers"] = "content-type"
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(json.dumps(params))
        return
    def post(self):
        urlfetch.set_default_fetch_deadline(60)

        logging.debug('Request: %s' % self.request.body)
        body = json.loads(self.request.body)
        update = telegram.Update.de_json(body)

        chat_id = update.message.chat.id
        message = update.message.text

        bot = telegram.Bot('TOKEN')

        if '/start' in message or '/help' in message:
            bot.sendMessage(chat_id=chat_id,
                            text='Hello! I am DevOpsReactions Bot, I can serve the latest DevOps Reactions post or a random one. What would you like?\n\nYou can control me by sending these commands:\n\n/latest - Latest DevOps Reactions post\n/random - Random DevOps Reactions post')

        image = None
        if '/latest' in message:
            image = DevOpsReactions.latest()
        if '/random' in message:
            image = DevOpsReactions.random()

        if image:
            bot.sendMessage(chat_id=chat_id,
                            text=image['title'])
            bot.sendChatAction(chat_id=chat_id,
                               action=telegram.ChatAction.UPLOAD_PHOTO)
            bot.sendDocument(chat_id=chat_id,
                             document=image['image_url'])

        self.response.write(json.dumps(body))
예제 #26
0
 def post(self):
     if not self.user:
         self.redirect("/")
     title = self.request.get('title')
     if title:
         set_fetch_timeout(60000)
         urlfetch.set_default_fetch_deadline(60000)
         search = check_cache(title)
         if search:
             rating = search[0][1]
             blurb = search[1]
             film = Film(parent = film_key(),
                         title = title,
                         rating = rating,
                         blurb = blurb)
             film.put()
             top_films(True)
             self.redirect('/')
         else:
             error = 'not found'
             self.render("newmyfilm.html", title = title,
                                             error = error)
     else:
         error = "darf"
         self.render("newmyfilm.html", title = title,
                                       error = error)
예제 #27
0
  def get(self):
    """Request handler for GET operations."""
    urlfetch.set_default_fetch_deadline(URLFETCH_TIMEOUT)
    config = explorer_config.ExplorerConfigModel.Get()
    client = DataHandlerUtil.GetDataClient(self.env)
    client.project_id = config.default_project

    query = product_labels.ProductLabelsQuery(
        data_client=client,
        dataset_name=config.default_dataset)
    filters = http_util.GetJsonParam(self.request, 'filters')

    start_date = None
    if 'start_date' in filters and filters['start_date']:
      start_date = filters['start_date']

    end_date = None
    if 'end_date' in filters and filters['end_date']:
      end_date = filters['end_date']

    response = query.Execute(
        start_date=start_date,
        end_date=end_date,
        product_name=filters['product_name'],
        test=filters['test'],
        metric=filters['metric'])

    self.RenderJson({'labels': response['labels']})
예제 #28
0
    def get(self):
        
        if self.request.cookies.get("username"):
            
            try:  

                #Hacemos petición al servicio web de geonames y mandamos el json al template
                idUsername = self.request.cookies.get("idUsername")
                populationAreas = []
                
                coordenadas = model.DatosRecibidos.query(model.DatosRecibidos.idDatos == idUsername).get()
                
                lat = coordenadas.latitud
                lng = coordenadas.longitud

                #Deadline error http, poner default fetch
                urlfetch.set_default_fetch_deadline(45)
                
                url = 'http://api.geonames.org/findNearbyJSON?username=juanfranrv&country=US&lat=' + str(lat) + '&lng=' + str(lng) + '&radius=300&formatted=true&featureCode=PPL&featureClass=P'
                
                r = urllib2.urlopen(url)
        
                result = json.load(r)              
                populationAreas = result["geonames"]
 
                self.response.write(json.dumps(populationAreas))
                
            except (ValueError, KeyError, DeadlineExceededError) as e:
                
                error = 'Geonames web service is temporarily unavailable.'
                self.response.write(json.dumps(error))
예제 #29
0
 def get(self):
     urlfetch.set_default_fetch_deadline(60)
     url = self.request.get("url")
     if url:
         self.response.write(
             json.dumps(json.load(urllib2.urlopen(BASE_URL + "setWebhook", urllib.urlencode({"url": url}))))
         )
예제 #30
0
def getNearest(category, latlang):
    getNearestUrl = 'http://api.wikimapia.org/?key=example&function=place.getnearest'+str(latlang)+'&format=json&pack=&language=en&count=50&category='+str(category)
    urlfetch.set_default_fetch_deadline(60)
    result = json.load(urllib2.urlopen(getNearestUrl))
    logging.info(result)
    logging.info('getNearest done')
    return result
예제 #31
0
    def post(self):

        logging.info("[Sender] got new job")

        job_request = json.loads(self.request.get("job"))

        email = job_request["email"]

        file_name = job_request["file"]
        file_name = file_name.encode("utf-8")

        # somehow when send email on dev env, we need to encode the file name first
        # prod env seems doing this automatically
        # if DEV_ENV:
        #     file_name = str(Header(file_name, "utf-8"))

        links = job_request["links"]

        logging.info("[Sender] raw file name: %s", file_name)
        logging.info("[Sender] encoded file name: %s", file_name)
        logging.info("[Sender] download links: %s", links)

        urlfetch.set_default_fetch_deadline(599)
        sg = sendgrid.SendGridAPIClient(apikey=_SENDGRID_API_KEY)

        for link in links:

            try:

                logging.info("[Sender] trying link %s", link)

                request = urllib2.Request(link)
                response = urllib2.urlopen(request)

                from_email = Email(APP_CONFIG["EMAIL"]["SENDER"])
                to_email = Email(email)
                subject = file_name
                content = Content("text/plain", file_name)
                mail = Mail(from_email, subject, to_email, content)

                if self.txt_file(file_name):
                    logging.info(
                        "[Sender] resource is a txt file, encode to utf-8 before send"
                    )
                    filecontent = self.to_unicode(response.read())
                    filecontent = base64.b64encode(filecontent)

                    attachment = Attachment()
                    attachment.content = filecontent
                    attachment.type = 'application/text'
                    attachment.filename = file_name
                    attachment.disposition = 'attachment'
                    attachment.content_id = 'request'
                    mail.add_attachment(attachment)
                else:
                    logging.info(
                        "[Sender] unknown resource format, send straight away")
                    filecontent = base64.b64encode(response.read())

                    attachment = Attachment()
                    attachment.content = filecontent
                    attachment.type = 'application/octet-stream'
                    attachment.filename = file_name
                    attachment.disposition = 'attachment'
                    attachment.content_id = 'request'
                    mail.add_attachment(attachment)

                response = sg.client.mail.send.post(request_body=mail.get())
                logging.info("Sending email response status: %s",
                             response.status_code)
                logging.info("Sending email response body: %s", response.body)
                logging.info("Sending email response headers: %s",
                             response.headers)
                return

            except urllib2.HTTPError as e:
                print(e.read())
            except Exception:
                logging.error(traceback.format_exc())
예제 #32
0
 def __init__(self):
     logging.info('PirateBay: init: started')
     urlfetch.set_default_fetch_deadline(60)
예제 #33
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:' + str(body))

        self.response.write(json.dumps(body))

        update_id = body['update_id']

        # Return an inline keyboard for a poll
        def get_poll_inline_keyboard(poll, share_button=False):
            keys = '[]'
            if poll['answers']:
                keys = '['

                # iterate over answers
                for i in range(len(poll['answers'])):

                    answer = poll['answers'][i]
                    data = str(poll['owner']) + ';' + str(
                        poll['id']) + ';' + str(i)

                    # Count how often answer at index i was voted for
                    voted = 0
                    for user_answer in poll['answered']:
                        if user_answer['chosen_answers'] >> i & 1:
                            voted += 1

                    # here hide the amount of the answers, to avoid interfere
                    keys += '[{"text": "' + answer + '", "callback_data": "' + data + '"}],'

                if share_button:
                    keys += '[{"text": "share", "switch_inline_query": "' + poll.get(
                        'id') + '"}],'

                # 实名显示投票结果按钮
                show_poll_results_data_display_name = \
                    str(poll['owner']) + ';' + str(poll['id']) + ';' + 'show_poll_results_display_name'
                keys += u'[{"text": "实名显示结果", "callback_data": "' + show_poll_results_data_display_name + '"}],'

                # 匿名显示投票结果按钮
                show_poll_results_data_hide_name = \
                    str(poll['owner']) + ';' + str(poll['id']) + ';' + 'show_poll_results_hide_name'
                keys += u'[{"text": "匿名显示结果", "callback_data": "' + show_poll_results_data_hide_name + '"}],'

                keys = keys[:-1] + ']'  # removes the last comma
            return '{"inline_keyboard": ' + keys + '}'

        # can know who voted
        def get_poll_status(poll, keyboard_status):

            user_names = []
            for i in range(len(poll['answers'])):

                # Count how often answer at index i was voted for
                for user_answer in poll['answered']:
                    if user_answer['chosen_answers'] >> i & 1:

                        user_name = User.get_username(user_answer['user_id'])
                        if user_name:
                            user_names.append(user_name)

                        logging.info('user_names: ' + str(user_names))

                        # avoid name appear is related to vote select
                        user_names.sort()

            poll_results = '\n' + '\n' + u'已投票的大笨蛋: ' + str(
                len(user_names)) + '\n' + '\n' + ', '.join(user_names) + '\n'

            # 显示实名的投票结果
            if keyboard_status == 'display_name':

                poll_results += u'\n- 投票结果 -\n'

                for i in range(len(poll['answers'])):
                    names = []
                    for user_answer in poll['answered']:
                        # append user name if he has voted for this answer
                        if user_answer['chosen_answers'] >> i & 1:
                            u = User.get(id=user_answer['user_id'])
                            if u:
                                names.append(u.get_name())

                    poll_results += '\n' + poll['answers'][
                        i] + '\n' + '(' + str(
                            len(names)) + '): ' + ','.join(names)

            # 显示匿名的投票结果
            if keyboard_status == 'hide_name':

                poll_results += u'\n- 投票结果 -\n'

                # count bits for each answer
                for i in range(len(poll['answers'])):
                    count = 0
                    for user_answer in poll['answered']:
                        if user_answer['chosen_answers'] >> i & 1:
                            count += 1

                    poll_results += '\n(' + str(
                        count) + ') ' + poll['answers'][i]

            return poll_results

        def telegram_method(name, keyvalues):

            # encode strings
            encoded = {}
            for key in keyvalues:
                encoded[key] = keyvalues[key].encode('utf-8')

            try:
                logging.info(BASE_URL + str(name) +
                             str(urllib.urlencode(encoded)))

                resp = urllib2.urlopen(BASE_URL + name,
                                       urllib.urlencode(encoded)).read()

                logging.info(name + ' response:' + resp)

            except Exception, e:
                logging.warn(e)
예제 #34
0
 def get(self):
     urlfetch.set_default_fetch_deadline(60)
     url = self.request.get('url')
     if url:
         self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'setWebhook', urllib.urlencode({'url': url})))))
예제 #35
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        #Função que verifica se o forca_bot foi excluido ou se ele existe no BD
        def verifyBot(left_chat_participant = None):
            if left_chat_participant:
                first_name = left_chat_participant['first_name'].encode('utf-8')
                if first_name == 'The Hangman':
                    bds.delChat(chat_id)
                    return
            return

        #Dados que recebemos do telegram
        update_id = body['update_id']
        message = body['message']
        message_id = str(message.get('message_id')).encode('utf-8')
        left_chat_participant = message.get('left_chat_participant')
        new_chat_participant = message.get('new_chat_participant')
        group_chat_created = message.get('group_chat_created')
        date = message.get('date')
        text = message.get('text').encode('utf-8') if message.get('text') else message.get('text')
        if text:
            if not text.startswith('/admin'):
                text = text.lower()
        fr = message.get('from')
        chat = message['chat']
        chat_id = str(chat['id'])
        user_id = message['from']
        u_id = str(user_id.get('id')).encode('utf-8')
        u_name = user_id.get('first_name').encode('utf-8')
        bds.checkChat(chat_id)

        #'Chama' a verificação.
        if left_chat_participant:
            verifyBot(left_chat_participant = left_chat_participant)

        if not text:
            logging.info('no text')
            return

        #Função que envia o dict para o Telegram
        def reply(dict = None):
            if dict:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode(dict)).read()
            else:
                logging.error('no msg or img specified')
                resp = None
            logging.info('send response:')
            logging.info(resp)

        #Lê as configurações
        def getLanguage(chat_id):
            s = bds.getSettings(chat_id) #Classe settings
            if s:
                if s.language == 'ptBR':
                    import ptBR as l
                    return l
                elif s.language == 'enUS':
                    import enUS as l
                    return l
                elif s.language == 'hbIL':
                    import hbIL as l
                    return l
            else:
                bds.checkChat(chat_id)
                s = bds.getSettings(chat_id) #Classe settings
                if s.language == 'ptBR':
                    import ptBR as l
                    return l
                elif s.language == 'enUS':
                    import enUS as l
                    return l
                elif s.language == 'hbIL':
                    import hbIL as l
                    return l
                return


        #Aqui começa a lógica principal
        l = getLanguage(chat_id)
        s = bds.getSettings(chat_id)
        ab = bds.getArriscarBlock(chat_id)
        first = bds.getFirstWelcome(chat_id)[0]
        rpl = [c.toDict(chat_id, l.sorry_msg)]

        text = '/start' if text == l.ligar.lower() else text #Tratamento para o caso do /start
        text = l.ajuda.lower() if text.startswith('/help') else text
        text = l.desligar.lower() if text.startswith('/stop') else text

        if text.startswith('@PlayHangmanBot'):
            text = text[15:]
        if (u_id in creators) and (text.startswith('/admin')): #Funções especiais dos criadores do bot
            if text.startswith('/admindelchat'):
                chat = text[14:]
                if len(chat) > 0:
                    if bds.delChat(chat):
                        rpl = [c.toDict(chat_id, 'Chat '+chat+' deletado')]
                    else:
                        rpl = [c.toDict(chat_id, 'Chat '+chat+' não existe')]
            elif text.startswith('/adminshout'):
                text = text[12:]
                chats = bds.getChats()
                rpl = []
                for i in range(len(chats)):
                    time.sleep(0.1)
                    rpl.append(c.toDict(chats[i], text))
            elif text.startswith('/admingetdadoschat'):
                chat = text[19:]
                if len(chat) > 0:
                    dados = bds.getDadosChat(chat)
                    jogos_dia = bds.getJogosDia(chat, date)
                    if dados:
                        rpl = [c.toDict(chat_id, 'Chat '+str(chat)+'\nJogos: '+ str(dados.games)+'\nJogadores: '+str(len(dados.players))+'\nJogos por dia: '+str(jogos_dia)+'\nTop player: '+dados.topPlayer.u_name.encode('utf-8')+'\n\tScore: '+str(dados.topPlayer.u_score)+'\n\tId: '+str(dados.topPlayer.u_id))]
                    else:
                        rpl = [c.toDict(chat_id, 'Chat '+chat+' não existe')]
            elif text.startswith('/admingetdadosglobais'):
                urlfetch.set_default_fetch_deadline(300)
                resp = bds.getDadosGlobais(date)
                rpl = [c.toDict(chat_id, 'Chats: '+str(resp[0])+'\nJogadores: '+str(resp[1])+'\nJogos por dia: '+str(resp[2])+'\nJogos: '+str(resp[3]))]
        elif (not s.waiting) or first:
            #comandos que indiferem do estado atual de jogo
            if '/start' in text:
                rpl = c.start(chat_id, u_id, first)
            elif bds.getEnabled(chat_id):
                if ('/kb' in text) or (l.att_kb in text):
                    rpl = c.kb(chat_id, u_id, message_id, s.waiting)
                elif l.desligar.lower() in text:
                    rpl = c.stop(chat_id)
                elif l.ajuda.lower() in text:
                    rpl = c.ajuda(chat_id)
                elif l.rank.lower() in text:
                    rpl = c.rank(chat_id)
                elif l.config.lower() in text:
                    rpl = c.config(chat_id, message_id)
                elif l.sobre.lower() in text:
                    rpl = c.sobre(chat_id)
                elif l.voltar.lower() in text:
                    rpl = c.voltar(chat_id, l.voltar_msg, message_id, u_id)
                elif l.comandos.lower() in text:
                    rpl = c.comandos(chat_id, message_id, u_id)
                #comandos inGame
                elif bds.getInGame(chat_id):
                    check = bds.checkUid(chat_id, u_id)
                    if l.cancelar_jogo.lower() in text:
                        rpl = g.cancelarJogo(chat_id, u_id)
                    elif check == True:
                        if bds.getArriscarBlock(chat_id):
                            rpl = g.arriscarPalavra2(chat_id, u_id, u_name, message_id, text)
                        elif l.arriscar.lower() in text:
                            rpl = g.arriscarPalavra1(chat_id, u_id, message_id)
                        elif (len(text) == 1) or (text.startswith('@PlayHangmanBot')):
                            if text.startswith('@PlayHangmanBot'):
                                text = text[10:]
                            rpl = g.chutarLetra(chat_id, u_id, u_name, message_id, text)
                    elif check == 'rnd':
                        rpl = [c.toDict(chat_id, l.round_errado_msg)]
                    elif check == 'out':
                        rpl = [c.toDict(chat_id, l.fora_msg)]

                #comandos preGame
                elif bds.getPreGame(chat_id):
                    if s.categorias and bds.checkAdm(chat_id, u_id):
                        rpl = p.setCategorias(chat_id, text, message_id,u_id)
                    elif l.entrar.lower() in text:
                        rpl = p.entrar(chat_id, u_id, u_name, message_id)
                    elif l.categorias_btn.lower() in text:
                        rpl = p.categorias(chat_id, u_id, message_id)
                    elif l.sair.lower() in text:
                        rpl = p.sair(chat_id, u_id, u_name, message_id)
                    elif l.fechar_jogo.lower() in text:
                        rpl = p.fecharJogo(chat_id, u_id, message_id, date)
                    elif l.cancelar_jogo.lower() in text:
                        rpl = p.cancelarJogo(chat_id, u_id)
                #se preGame e inGame == False (vide flowchart)
                elif (not bds.getPreGame(chat_id)) and (not bds.getInGame(chat_id)):
                    if l.novojogo.lower() in text:
                        rpl = c.novojogo(chat_id, u_id, u_name, message_id)
        else:
            if l.ajuda.lower() in text:
                rpl = c.ajuda(chat_id)
            elif '/kb' in text:
                rpl = c.kb(chat_id, u_id, message_id, s.waiting)
            else:
                rpl = c.changeLanguage(chat_id, text, message_id, u_id)

        error = False
        for i in range(len(rpl)):
                time.sleep(1.5)
                try:
                    reply(rpl[i])
                except Exception, e:
                    print e
                    if (str(e) == "HTTP Error 429: Unknown") and (not error):
                        error = True
                        time.sleep(2)
                        i = i - 1
                    else:
                        try:
                            reply(c.toDict(chat_id, l.error_msg))
                        except Exception, e:
                            print e
                            try:
                                reply(c.toDict(chat_id, 'Fatal error, contact @cristoferoswald or @bcesarg6.'))
                            except Exception, e:
                                print e
예제 #36
0
    def post(self):
        #Envia o texto de resposta para o chat
        def reply(message=None, img=None):
            comandos.reply(BASE_URL, message, img)

        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        # recupera informações da mensagem
        try:
            message = body['message']
        except:
            message = body['edited_message']

        msg = {}
        msg['text'], msg['message_id'], msg['chat_id'], msg[
            'user_id'] = comandos.extrai_texto(message)

        if not msg['text']:
            msg['text'], msg['reply_msg_txt'], msg['sticker_id'], msg[
                'emoji'] = comandos.extrai_reply(message)

        #inicializa algumas variáveis globais
        comandos.inicializa(BASE_URL, msg['chat_id'])
        #verifica a necessidade de criar nova chamada
        comandos.verifica_chamada(BASE_URL, msg['chat_id'])

        if not msg['text']:
            logging.info('no text')
            return

        comandos.send_action('typing')

        logging.info('text is: ' + msg['text'])
        if msg['text'].startswith('/'):
            msg['text'] = msg['text'].split('/')[1]
            #remove sufixo do bot do telegram "@NOMEDOBOT"
            #extrai apenas o comando
            command = comandos.get_comando(msg['text'].lower().split("@")[0])

            #COMANDOS
            #Liga e desliga o bot
            if command == 'start':
                reply('Acordei')
                setEnabled(msg['chat_id'], True)
            elif command == 'stop':
                reply('Dormi')
                setEnabled(msg['chat_id'], False)
            #Adiciona nova pessoa à lista de chamada e cria arquivo data_PESSOA
            elif command == 'add_pessoa':
                reply(comandos.add_pessoa(msg['text']))
            #Adiciona nova frase para uma pessoa
            elif command == 'add_frase':
                reply(comandos.add_frase(**msg))
            elif command == 'del_frase':
                reply(comandos.del_frase(msg['text']))
            #Adiciona o sticker enviado como repsta ao "add_frase sticker"
            elif command == 'add_sticker':
                reply(comandos.add_sticker(**msg))
            #Envia todas as frases de uma pessoa
            elif command == 'vomit':
                reply(comandos.get_vomit(msg['text']))
            #Envia uma frase específica
            elif command == 'get_frase_numero':
                reply(comandos.get_frase_numero(msg['text']))
            #Envia uma frase aleatória
            elif command == 'random':
                reply(comandos.get_frase_random(msg['text']))
            elif command == 'hype':
                reply(comandos.get_hype(msg['text']))
            #Envia a lista de chamada
            elif command == 'chamada':
                reply(comandos.get_vomit('chamada'))
            else:
                reply('comando nao reconhecido')
예제 #37
0
    def process(self, authid):

        servicetype = 'Unknown'

        try:

            if authid is None or authid == '':
                logging.info('No authid in query')
                self.response.headers['X-Reason'] = 'No authid in query'
                self.response.set_status(400, 'No authid in query')
                return

            if authid.find(':') <= 0:
                logging.info('Invalid authid in query')
                self.response.headers['X-Reason'] = 'Invalid authid in query'
                self.response.set_status(400, 'Invalid authid in query')
                return

            keyid = authid[:authid.index(':')]
            password = authid[authid.index(':') + 1:]

            if settings.WORKER_OFFLOAD_RATIO > random.random():
                workers = memcache.get('worker-urls')
                # logging.info('workers: %s', workers)
                if workers is not None and len(workers) > 0:
                    newloc = random.choice(workers)
                    logging.info('Redirecting request for id %s to %s', keyid,
                                 newloc)
                    self.response.headers['Location'] = newloc
                    self.response.set_status(302, 'Found')
                    return

            if keyid == 'v2':
                self.handle_v2(password)
                return

            if settings.RATE_LIMIT > 0:

                ratelimiturl = '/ratelimit?id=' + keyid + '&adr=' + self.request.remote_addr
                ratelimit = memcache.get(ratelimiturl)

                if ratelimit is None:
                    memcache.add(key=ratelimiturl, value=1, time=60 * 60)
                elif ratelimit > settings.RATE_LIMIT:
                    logging.info('Rate limit response to: %s', keyid)
                    self.response.headers[
                        'X-Reason'] = 'Too many request for this key, wait 60 minutes'
                    self.response.set_status(
                        503, 'Too many request for this key, wait 60 minutes')
                    return
                else:
                    memcache.incr(ratelimiturl)

            cacheurl = '/refresh?id=' + keyid + '&h=' + hashlib.sha256(
                password).hexdigest()

            cached_res = memcache.get(cacheurl)
            if cached_res is not None and type(cached_res) != type(''):
                exp_secs = (int)((cached_res['expires'] -
                                  datetime.datetime.utcnow()).total_seconds())

                if exp_secs > 30:
                    logging.info(
                        'Serving cached response to: %s, expires in %s secs',
                        keyid, exp_secs)
                    self.response.write(
                        json.dumps({
                            'access_token': cached_res['access_token'],
                            'expires': exp_secs,
                            'type': cached_res['type']
                        }))
                    return
                else:
                    logging.info(
                        'Cached response to: %s is invalid because it expires in %s',
                        keyid, exp_secs)

            # Find the entry
            entry = dbmodel.AuthToken.get_by_key_name(keyid)
            if entry is None:
                self.response.headers['X-Reason'] = 'No such key'
                self.response.set_status(404, 'No such key')
                return

            servicetype = entry.service

            # Decode
            data = base64.b64decode(entry.blob)
            resp = None

            # Decrypt
            try:
                resp = json.loads(
                    simplecrypt.decrypt(password, data).decode('utf8'))
            except:
                logging.exception('decrypt error')
                self.response.headers['X-Reason'] = 'Invalid authid password'
                self.response.set_status(400, 'Invalid authid password')
                return

            service = find_service(entry.service)

            # Issue a refresh request
            url = service['auth-url']
            data = urllib.urlencode({
                'client_id': service['client-id'],
                'redirect_uri': service['redirect-uri'],
                'client_secret': service['client-secret'],
                'grant_type': 'refresh_token',
                'refresh_token': resp['refresh_token']
            })

            urlfetch.set_default_fetch_deadline(20)

            req = urllib2.Request(
                url, data,
                {'Content-Type': 'application/x-www-form-urlencoded'})
            f = urllib2.urlopen(req)
            content = f.read()
            f.close()

            # Store the old refresh_token as some servers do not send it again
            rt = resp['refresh_token']

            # Read the server response
            resp = json.loads(content)
            exp_secs = int(resp["expires_in"])

            # Set the refresh_token if it was missing
            if not resp.has_key('refresh_token'):
                resp['refresh_token'] = rt

            # Encrypt the updated response
            cipher = simplecrypt.encrypt(password, json.dumps(resp))
            entry.expires = datetime.datetime.utcnow() + datetime.timedelta(
                seconds=exp_secs)
            entry.blob = base64.b64encode(cipher)
            entry.put()

            cached_res = {
                'access_token': resp['access_token'],
                'expires': entry.expires,
                'type': servicetype
            }

            memcache.set(key=cacheurl, value=cached_res, time=exp_secs - 10)
            logging.info('Caching response to: %s for %s secs, service: %s',
                         keyid, exp_secs - 10, servicetype)

            # Write the result back to the client
            self.response.write(
                json.dumps({
                    'access_token': resp['access_token'],
                    'expires': exp_secs,
                    'type': servicetype,
                    'v2_authid': 'v2:' + entry.service + ':' + rt
                }))

        except:
            logging.exception('handler error for ' + servicetype)
            self.response.headers['X-Reason'] = 'Server error'
            self.response.set_status(500, 'Server error')
예제 #38
0
    def get(self, service=None):
        display = 'Unknown'
        try:
            # Grab state and code from request
            state = self.request.get('state')
            code = self.request.get('code')

            if settings.TESTING:
                logging.info('Log-in with code %s, and state %s', code, state)

            if state is None or code is None:
                raise Exception('Response is missing state or code')

            statetoken = dbmodel.StateToken.get_by_key_name(state)
            if statetoken is None:
                raise Exception('No such state found')

            if statetoken.expires < datetime.datetime.utcnow():
                raise Exception('State token has expired')

            provider, service = find_provider_and_service(statetoken.service)

            display = provider['display']

            redir_uri = service['redirect-uri']
            if self.request.get('token') is not None:
                redir_uri += self.request.get('token')

            if settings.TESTING:
                logging.info('Got log-in with url %s', redir_uri)
                logging.info('Sending to %s', service['auth-url'])

            # Some services are slow...
            urlfetch.set_default_fetch_deadline(20)

            # With the returned code, request a refresh and access token
            url = service['auth-url']

            request_params = {
                'client_id': service['client-id'],
                'redirect_uri': redir_uri,
                'client_secret': service['client-secret'],
                'state': state,
                'code': code,
                'grant_type': 'authorization_code'
            }

            # Some services do not allow the state to be passed
            if service.has_key('no-state-for-token-request'
                               ) and service['no-state-for-token-request']:
                del request_params['state']

            data = urllib.urlencode(request_params)

            headers = {'Content-Type': 'application/x-www-form-urlencoded'}

            # Alternative method for sending auth, according to HubiC API
            # if service == 'hc':
            #     logging.info('Adding header ' + v['client-id'] + ':' + v['client-secret'])
            #     headers['Authorization'] = "Basic " + base64.b64encode(v['client-id'] + ':' + v['client-secret'])

            req = urllib2.Request(url, data, headers)
            f = urllib2.urlopen(req)
            content = f.read()
            f.close()

            if settings.TESTING:
                logging.info('RESP RAW:' + content)

            # OAuth response is JSON
            resp = json.loads(content)

            # If this is a service that does not use refresh tokens,
            # we just return the access token to the caller
            if service.has_key(
                    'no-refresh-tokens') and service['no-refresh-tokens']:
                dbmodel.update_fetch_token(statetoken.fetchtoken,
                                           resp['access_token'])

                # Report results to the user
                template_values = {
                    'service': display,
                    'appname': settings.APP_NAME,
                    'longappname': settings.SERVICE_DISPLAYNAME,
                    'authid': resp['access_token'],
                    'fetchtoken': statetoken.fetchtoken
                }

                template = JINJA_ENVIRONMENT.get_template('logged-in.html')
                self.response.write(template.render(template_values))
                statetoken.delete()

                logging.info('Returned access token for service %s',
                             provider['id'])
                return

            # This happens in some cases with Google's OAuth
            if not resp.has_key('refresh_token'):

                if provider.has_key('deauthlink'):
                    template_values = {
                        'service':
                        display,
                        'authid':
                        'Server error, you must de-authorize ' +
                        settings.APP_NAME,
                        'showdeauthlink':
                        'true',
                        'deauthlink':
                        provider['deauthlink'],
                        'fetchtoken':
                        ''
                    }

                    template = JINJA_ENVIRONMENT.get_template('logged-in.html')
                    self.response.write(template.render(template_values))
                    statetoken.delete()
                    return

                else:
                    raise Exception(
                        'No refresh token found, try to de-authorize the application with the provider'
                    )

            # v2 tokens are just the provider name and the refresh token
            # and they have no stored state on the server
            if statetoken.version == 2:
                authid = 'v2:' + statetoken.service + ':' + resp[
                    'refresh_token']
                dbmodel.update_fetch_token(statetoken.fetchtoken, authid)

                # Report results to the user
                template_values = {
                    'service': display,
                    'appname': settings.APP_NAME,
                    'longappname': settings.SERVICE_DISPLAYNAME,
                    'authid': authid,
                    'fetchtoken': statetoken.fetchtoken
                }

                template = JINJA_ENVIRONMENT.get_template('logged-in.html')
                self.response.write(template.render(template_values))
                statetoken.delete()

                logging.info('Returned refresh token for service %s',
                             provider['id'])
                return

            # We store the ID if we get it back
            if resp.has_key("user_id"):
                user_id = resp["user_id"]
            else:
                user_id = "N/A"

            exp_secs = 1800  # 30 min guess
            try:
                exp_secs = int(resp["expires_in"])
            except:
                pass

            # Create a random password and encrypt the response
            # This ensures that a hostile takeover will not get access
            #  to stored access and refresh tokens
            password = password_generator.generate_pass()
            cipher = simplecrypt.encrypt(password, json.dumps(resp))

            # Convert to text and prepare for storage
            b64_cipher = base64.b64encode(cipher)
            expires = datetime.datetime.utcnow() + datetime.timedelta(
                seconds=exp_secs)
            fetchtoken = statetoken.fetchtoken

            entry = None
            keyid = None

            # Find a random un-used user ID, and store the encrypted data
            while entry is None:
                keyid = '%030x' % random.randrange(16**32)
                entry = dbmodel.insert_new_authtoken(keyid, user_id,
                                                     b64_cipher, expires,
                                                     provider['id'])

            # Return the id and password to the user
            authid = keyid + ':' + password

            # If this was part of a polling request, signal completion
            dbmodel.update_fetch_token(fetchtoken, authid)

            # Report results to the user
            template_values = {
                'service': display,
                'appname': settings.APP_NAME,
                'longappname': settings.SERVICE_DISPLAYNAME,
                'authid': authid,
                'fetchtoken': fetchtoken
            }

            template = JINJA_ENVIRONMENT.get_template('logged-in.html')
            self.response.write(template.render(template_values))
            statetoken.delete()

            logging.info('Created new authid %s for service %s', keyid,
                         provider['id'])

        except:
            logging.exception('handler error for ' + display)

            template_values = {
                'service': display,
                'appname': settings.APP_NAME,
                'longappname': settings.SERVICE_DISPLAYNAME,
                'authid': 'Server error, close window and try again',
                'fetchtoken': ''
            }

            template = JINJA_ENVIRONMENT.get_template('logged-in.html')
            self.response.write(template.render(template_values))
예제 #39
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        message = body['message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']

        if not text:
            logging.info('no text')
            return

        def reply(msg=None, img=None):
            if msg:
                resp = urllib2.urlopen(
                    BASE_URL + 'sendMessage',
                    urllib.urlencode({
                        'chat_id': str(chat_id),
                        'text': msg.encode('utf-8'),
                        'disable_web_page_preview': 'true',
                        #'reply_to_message_id': str(message_id),
                    })).read()
            elif img:
                resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                    ('chat_id', str(chat_id)),
                    ('reply_to_message_id', str(message_id)),
                ], [
                    ('photo', 'image.jpg', img),
                ])
            else:
                logging.error('no msg or img specified')
                resp = None

            logging.info('send response:')
            logging.info(resp)

        #I CUSTOMISED FROM HERE!!!

        if '/tv' in text:
            purl = urllib2.Request(
                url=
                'http://plex_server_address_or_IP_address_here:32400/library/sections/1/newest?X-Plex-Token=YOUR_PLEX_TOKEN_HERE'
            )
            f = urllib2.urlopen(purl)
            tree = ElementTree()
            tree.parse(f)
            root = tree.getroot()
            shows = root.findall('./Video')
            for s in shows[:10]:
                title = s.attrib['grandparentTitle']
                season = s.attrib['title']
                #added = s.attrib['addedAt'] wanted to add the date and time but not sure how to convert Epoch time format.
                reply(title + ' - ' + season)

        if '/movies' in text:
            purl = urllib2.Request(
                url=
                'http://plex_server_address_or_IP_address_here:32400/library/sections/2/recentlyAdded?X-Plex-Token=YOUR_PLEX_TOKEN_HERE'
            )
            f = urllib2.urlopen(purl)
            tree = ElementTree()
            tree.parse(f)
            root = tree.getroot()
            shows = root.findall('./Video')
            for s in shows[:5]:
                title = s.attrib['title']
                summary = s.attrib['summary']
                reply(title + ' - ' + summary)

        if '/anime' in text:
            purl = urllib2.Request(
                url=
                'http://plex_server_address_or_IP_address_here:32400/library/sections/6/newest?X-Plex-Token=YOUR_PLEX_TOKEN_HERE'
            )
            f = urllib2.urlopen(purl)
            tree = ElementTree()
            tree.parse(f)
            root = tree.getroot()
            shows = root.findall('./Video')
            for s in shows[:5]:
                title = s.attrib['grandparentTitle']
                season = s.attrib['title']
                reply(title + ' - ' + season)
예제 #40
0
파일: main.py 프로젝트: kercos/LingoBot
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        # update_id = body['update_id']
        if 'message' not in body:
            return
        message = body['message']
        # message_id = message.get('message_id')
        # date = message.get('date')
        if "chat" not in message:
            return
        # fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']
        if "first_name" not in chat:
            return
        text = message.get('text').encode('utf-8') if "text" in message else ''
        name = chat["first_name"].encode('utf-8')
        last_name = chat["last_name"].encode(
            'utf-8') if "last_name" in chat else None
        username = chat["username"] if "username" in chat else None
        location = message["location"] if "location" in message else None
        contact = message["contact"] if "contact" in message else None

        # u'contact': {u'phone_number': u'393496521697', u'first_name': u'Federico', u'last_name': u'Sangati',
        #             u'user_id': 130870321}
        # logging.debug('location: ' + str(location))

        def reply(msg=None,
                  kb=None,
                  markdown=False,
                  inlineKeyboardMarkup=False):
            tell(chat_id, msg, kb, markdown, inlineKeyboardMarkup)

        p = person.getPersonByChatId(chat_id)

        if p is None:
            # new user
            logging.info("Text: " + text)
            if text == '/help':
                reply(ISTRUZIONI)
            elif text.startswith("/start"):
                tell_masters("New user: "******"Ciao {0}, welcome in LingoGameBot!".format(name))
                restart(p)
            else:
                reply("Press on /start if you want to begin. "
                      "If you encounter any problem, please contact @kercos")
        else:
            # known user
            p.updateUsername(username)
            if text == '/state':
                if p.state in STATES:
                    reply("You are in state " + str(p.state) + ": " +
                          STATES[p.state])
                else:
                    reply("You are in state " + str(p.state))
            elif text.startswith("/start"):
                reply("Hi {0}, welcome back in LingoGameBot!".format(name))
                p.setEnabled(True, put=False)
                restart(p)
            elif WORK_IN_PROGRESS and p.chat_id != key.FEDE_CHAT_ID:
                reply(UNDER_CONSTRUCTION +
                      " The system is under maintanance, try again later.")
            else:
                logging.debug("Sending {0} to state {1}. Input: '{2}'".format(
                    p.getName(), str(p.state), text))
                repeatState(p, input=text, contact=contact)
예제 #41
0
import numpy as np
import ee
import datetime as dt
from numpy import polyfit
import math
from calendar import monthrange
import urllib
from collections import defaultdict
import json
import datetime

from google.appengine.api import urlfetch
urlfetch.set_default_fetch_deadline(45)


def lst_map(img):
    return img.multiply(0.02).subtract(273.15).copyProperties(
        img, ['system:time_start', 'syst em:time_end'])


def Options(request):

    options = {}
    options["region"] = json.loads(request.POST.get(
        "region")) if request.POST.get("region") is not None else None
    options["series_start"] = request.POST.get('series_start')
    options["series_end"] = request.POST.get('series_end')
    options["satelite"] = request.POST.get('satelite')
    options["indices"] = request.POST.get('indices')

    # TODO logic checking
예제 #42
0
 def get(self):
     urlfetch.set_default_fetch_deadline(60)
     update_pin()
     self.response.write("pin updated")
예제 #43
0
    def handle_v2(self, inputfragment):
        servicetype = 'Unknown'
        try:
            if inputfragment.find(':') <= 0:
                self.response.headers['X-Reason'] = 'Invalid authid in query'
                self.response.set_status(400, 'Invalid authid in query')
                return

            servicetype = inputfragment[:inputfragment.index(':')]
            refresh_token = inputfragment[inputfragment.index(':') + 1:]

            service = find_service(servicetype)
            if service is None:
                raise Exception('No such service')

            if refresh_token is None or len(refresh_token.strip()) == 0:
                raise Exception('No token provided')

            tokenhash = hashlib.md5(refresh_token).hexdigest()

            if settings.RATE_LIMIT > 0:

                ratelimiturl = '/ratelimit?id=' + tokenhash + '&adr=' + self.request.remote_addr
                ratelimit = memcache.get(ratelimiturl)

                if ratelimit is None:
                    memcache.add(key=ratelimiturl, value=1, time=60 * 60)
                elif ratelimit > settings.RATE_LIMIT:
                    logging.info('Rate limit response to: %s', tokenhash)
                    self.response.headers[
                        'X-Reason'] = 'Too many request for this key, wait 60 minutes'
                    self.response.set_status(
                        503, 'Too many request for this key, wait 60 minutes')
                    return
                else:
                    memcache.incr(ratelimiturl)

            cacheurl = '/v2/refresh?id=' + tokenhash

            cached_res = memcache.get(cacheurl)
            if cached_res is not None and type(cached_res) != type(''):
                exp_secs = (int)((cached_res['expires'] -
                                  datetime.datetime.utcnow()).total_seconds())

                if exp_secs > 30:
                    logging.info(
                        'Serving cached response to: %s, expires in %s secs',
                        tokenhash, exp_secs)
                    self.response.write(
                        json.dumps({
                            'access_token': cached_res['access_token'],
                            'expires': exp_secs,
                            'type': cached_res['type']
                        }))
                    return
                else:
                    logging.info(
                        'Cached response to: %s is invalid because it expires in %s',
                        tokenhash, exp_secs)

            url = service['auth-url']
            data = urllib.urlencode({
                'client_id': service['client-id'],
                'redirect_uri': service['redirect-uri'],
                'client_secret': service['client-secret'],
                'grant_type': 'refresh_token',
                'refresh_token': refresh_token
            })

            urlfetch.set_default_fetch_deadline(20)

            req = urllib2.Request(
                url, data,
                {'Content-Type': 'application/x-www-form-urlencoded'})
            f = urllib2.urlopen(req)
            content = f.read()
            f.close()

            resp = json.loads(content)
            exp_secs = int(resp["expires_in"])
            expires = datetime.datetime.utcnow() + datetime.timedelta(
                seconds=exp_secs)

            cached_res = {
                'access_token': resp['access_token'],
                'expires': expires,
                'type': servicetype
            }

            memcache.set(key=cacheurl, value=cached_res, time=exp_secs - 10)
            logging.info('Caching response to: %s for %s secs, service: %s',
                         tokenhash, exp_secs - 10, servicetype)

            # Write the result back to the client
            self.response.write(
                json.dumps({
                    'access_token': resp['access_token'],
                    'expires': exp_secs,
                    'type': servicetype
                }))

        except:
            logging.exception('handler error for ' + servicetype)
            self.response.headers['X-Reason'] = 'Server error'
            self.response.set_status(500, 'Server error')
예제 #44
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        # update_id = body['update_id']
        message = body['message']
        #message_id = message.get('message_id')
        # date = message.get('date')
        if "chat" not in message:
            return
        # fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']
        if "first_name" not in chat:
            return
        text = message.get('text').encode('utf-8') if "text" in message else ""
        name = chat["first_name"].encode('utf-8')
        last_name = chat["last_name"].encode(
            'utf-8') if "last_name" in chat else "-"
        username = chat["username"] if "username" in chat else "-"

        #location = message["location"] if "location" in message else None
        #logging.debug('location: ' + str(location))

        def reply(msg=None, kb=None, hideKb=True, markdown=False):
            tell(chat_id, msg, kb, hideKb, markdown)

        p = ndb.Key(Person, str(chat_id)).get()

        if p is None:
            # new user
            logging.info("Text: " + text)
            if text == '/help':
                reply(INSTRUCTIONS)
            elif text.startswith("/start"):
                tell_masters("New user: "******"Hi " + name + ", " +
                      "welcome to the Parseme Shared Task annotation!")
                restart(p)
            else:
                reply("Something didn't work... please contact @kercos")
        else:
            # known user
            person.updateUsername(p, username)
            if text == '/state':
                if p.state in STATES:
                    reply("You are in state " + str(p.state) + ": " +
                          STATES[p.state])
                else:
                    reply("You are in state " + str(p.state))
            elif text.startswith("/start"):
                reply("Hi " + name + ", " +
                      "welcome back to the Parseme Shared Task annotation!")
                person.reInitializePerson(p, chat_id, name, last_name,
                                          username)
                restart(p)
            #-------------
            # SETTINGS
            #-------------
            elif p.state == -1:
                # settings
                if text == BACK_BUTTON:
                    restart(p)
                elif text == HIGHTLIGHTING_MODE_BUTTON:
                    goToSettingsHighlightingMode(p)
                    # state -11
                elif text == BUTTONS_DENSITY_BUTTON:
                    goToSettingsButtonsDensity(p)
                    # state -12
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            elif p.state == -11:
                # settings -> hightlighting mode
                if text == BACK_BUTTON:
                    goToSetting(p)
                elif text in person.MARKDOWN_OPTIONS.keys():
                    person.setHighlightingMode(p, text)
                    reply("Successfully set highlighting mode to " +
                          person.getHighlightedString(p, text),
                          markdown=True)
                    goToSetting(p)
                    #state -1
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            elif p.state == -12:
                # settings -> buttons density
                if text == BACK_BUTTON:
                    goToSetting(p)
                elif util.RepresentsInt(text):
                    charPerLine = int(text)
                    tot_sentences = sentAnno.totalSentPersonCurrentLang(p)
                    if charPerLine >= MIN_CHARS_PER_LINE and charPerLine <= MAX_CHARS_PER_LINE:
                        person.setMaxCharsPerLine(p, charPerLine)
                        goToSettingsButtonsDensity(p)
                        # state -1
                    else:
                        reply(
                            "Sorry, not a valid index. Please enter an index between "
                            + person.getHighlightedString(
                                p, str(MIN_CHARS_PER_LINE)) + " and " +
                            person.getHighlightedString(
                                p, str(MAX_CHARS_PER_LINE)) + ".")
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            # -------------
            # INITIAL STATE
            # -------------
            elif p.state == 0:
                # INITIAL STATE
                if text in ['/help', HELP_BUTTON]:
                    reply(INSTRUCTIONS)
                elif text == SETTINGS_BUTTON:
                    goToSetting(p)
                    #state -1
                elif text == INFO_BUTTON:
                    textMsg = "Number of annotated sentences per language:\n"
                    for lang, flag in sentAnno.LANGUAGE_FLAGS.iteritems():
                        progress = str(person.getLanguageProgress(p, lang))
                        total = str(sentAnno.totalSentLang(lang))
                        textMsg += flag + ' ' + progress + '/' + total + '\n'
                    reply(textMsg)
                elif text == ANNOTATE_BUTTON_EN:
                    person.setLanguage(p, 'ENGLISH')
                    person.importSelectedMwe(p)
                    displayCurrentSentence(p)
                    #state 1
                elif text == ANNOTATE_BUTTON_IR:
                    person.setLanguage(p, 'FARSI')
                    person.importSelectedMwe(p)
                    displayCurrentSentence(p)
                    # state 1
                elif chat_id in key.MASTER_CHAT_ID:
                    if text == '/test':
                        logging.debug('test')
                        #description = object.getObjectsById(4866725958909952)
                        #logging.debug('description: ' + description)
                        #logging.debug('type: ' + str(type(description)))
                        #reply(description)
                        #reply("Méthode de Français")
                        #deferred.defer(tell_fede, "Hello, world!")
                    elif text == '/testMarkdown':
                        reply(
                            "aaaa*xxxx*zzzzz\naaaa_xxxx_zzzz\naaaa`xxxx`zzzz",
                            markdown=True)
                        #reply("fdsadfa [dfsafd] dsfasdf a", markdown=True)
                        #reply("fdasfd *df\\*as* fa fsa", markdown=True)
                        #text = "*" + util.escapeMarkdown("`[8]") + "*"
                        #reply(text, markdown=True)
                        #logging.debug("reply text: " + text)
                    elif text == '/testBitMap':
                        #bitmaptest.testBitMap(100, 20)
                        bitmaptest.testBitMap(5000, 500)
                        reply('Successfully finished test (see log).')
                    elif text.startswith('/broadcast ') and len(text) > 11:
                        msg = text[11:]  #.encode('utf-8')
                        deferred.defer(broadcast, msg)
                    elif text == '/restartAllUsers':
                        deferred.defer(
                            restartAllUsers,
                            "ParsemeBot v.2 is out! " + GRINNING_FACE)
                    elif text == '/resetAllUsers':
                        c = resetAllUsers()
                        reply("Resetted users: " + str(c))
                    else:
                        reply(FROWNING_FACE + " Sorry, I can't understand...")
                    #setLanguage(d.language)
                else:
                    reply(FROWNING_FACE +
                          "Sorry I didn't understand you ...\n" +
                          "Press the HELP button to get more info.")
            # --------------------------
            # DISPLAY CURRENT SENTENCE
            # --------------------------
            elif p.state == 1:
                # display current sentence
                if text == BACK_BUTTON:
                    restart(p)
                elif text == '/progress':
                    reply("Annotated sentence(s) for " + p.language + ': ' +
                          str(person.getLanguageProgress(p)) + '/' +
                          str(sentAnno.totalSentPersonCurrentLang(p)))
                elif text == '/nextna':
                    index = person.getNextNonAnnSentIndex(p)
                    if index:
                        jumpToSentence(p, index + 1)
                    else:
                        reply(EXCLAMATION + " No next non-annotated sentence")
                elif text == '/prevna':
                    index = person.getPrevNonAnnSentIndex(p)
                    if index:
                        jumpToSentence(p, index + 1)
                    else:
                        reply(EXCLAMATION +
                              " No previous non-annotated sentence")
                elif text == ADD_MWE_BUTTON:
                    prepareToSelectTokens(p)
                    #state 30
                elif text == NO_MWE_BUTTON and p.selectedMwes == None:
                    reply(
                        CHECK +
                        " The current sentence has been marked without MWEs.")
                    sentAnno.setEmptyMwes(p)  #mirroring on person
                    #person.setEmptyMwes(p)
                    person.setCurrentSentenceAnnotated(p)
                    displayCurrentSentence(p)
                    # state 1
                elif text == MOD_MWE_BUTTON and p.selectedMwes != None:
                    # reply(UNDER_CONSTRUCTION + " currently working on this.")
                    askWhichAnnotationToModify(p)
                    # state 50
                elif text == REMOVE_MWE_BUTTON and p.selectedMwes != None:
                    askWhichAnnotationToRemove(p)
                    # state 60
                elif text == NEXT_BUTTON:
                    if person.hasNextSentece(p):
                        person.goToNextSentence(p)
                        person.importSelectedMwe(p)
                        displayCurrentSentence(p)
                        #state 1
                    else:
                        displayCurrentSentence(p)
                        reply(EXCLAMATION + " You reached the last sentence")
                        # state 1
                    # state 1
                elif text == PREV_BUTTON:
                    if person.hasPreviousSentece(p):
                        person.goToPrevSentence(p)
                        person.importSelectedMwe(p)
                        displayCurrentSentence(p)
                        #state 1
                    else:
                        displayCurrentSentence(p)
                        reply(EXCLAMATION + " You reached the first sentence")
                        # state 1
                    # state 1
                elif text == GO_TO_BUTTON:
                    tot_sentences = sentAnno.totalSentPersonCurrentLang(p)
                    reply(
                        "Please insert the index of the sentence you want to jumpt to, between "
                        + person.getHighlightedString(p, str(1)) + " and " +
                        person.getHighlightedString(p, str(tot_sentences)) +
                        " (you can also enter the number without pressing the "
                        + JUMP_ARROW + " button).",
                        markdown=True)
                elif text.startswith('/') and util.RepresentsInt(text[1:]):
                    mwe_number = int(text[1:])
                    jumpToModifySingleMwe(p, mwe_number)
                    #state 40
                elif util.RepresentsInt(text):
                    index = int(text)
                    tot_sentences = sentAnno.totalSentPersonCurrentLang(p)
                    if index > 0 and index <= tot_sentences:
                        jumpToSentence(p, index)
                        # state 1
                    else:
                        reply(
                            EXCLAMATION +
                            "Not a valid index. If you want to jump to a specific sentence, "
                            "please enter an index between " +
                            person.getHighlightedString(p, str(1)) +
                            " and " + person.getHighlightedString(
                                p, str(tot_sentences)) + '.',
                            markdown=True)
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            elif p.state == 30:
                # add MWE -> select tokens
                if text == BACK_BUTTON:
                    displayCurrentSentence(p)
                    # state 1
                elif text.endswith(']'):
                    token_number_str = text[text.rindex('[') + 1:-1]
                    #logging.debug('number_str: ' + token_number_str)
                    if util.RepresentsInt(token_number_str):
                        token_number = int(token_number_str)
                        if token_number > 0 and token_number <= len(
                                p.selectedTokens):
                            dealWithTokensSelection(p, token_number - 1)
                            #state 30
                        else:
                            reply("Not a valid token.")
                    else:
                        reply("Not a valid token.")
                elif text == CHOOSE_CAT_BUTTON:
                    currentMwes = p.selectedMwes
                    if currentMwes and person.getSelectedTokensIndexTuple(
                            p) in currentMwes:
                        reply(EXCLAMATION + ' MWE has been already inserted!')
                    else:
                        askAnnotationLabel(p)
                        # state 31
                elif text == CONFIRM_CHANNGES:
                    dealWithChangeMwe(p)
                    # state 1
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            elif p.state == 31:
                # add MWE -> select category
                if text == BACK_BUTTON:
                    if p.last_state == 40:
                        displayCurrentSentence(p)
                        # state 1
                    else:
                        dealWithTokensSelection(p)
                        # state 30
                elif text in categories.ANNOTATION_CATS:
                    indexes = person.getSelectedTokensIndexTuple(p)
                    mwe_flat = renderMwe(p, indexes, False, False)
                    reply(CHECK + " The MWE " + mwe_flat +
                          " has been marked with category " +
                          person.getHighlightedString(p, text) + '\n',
                          markdown=True)
                    sentAnno.appendMwe(p, text)  #mirroring on person
                    #person.appendMwe(p, text)
                    person.setCurrentSentenceAnnotated(p)
                    displayCurrentSentence(p)
                    # state 1
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            elif p.state == 40:
                # user has selected a single MWE via the /number option
                if text == BACK_BUTTON:
                    displayCurrentSentence(p)
                    # state 1
                elif text == CHANGE_CAT_BUTTON:
                    askAnnotationLabel(p)
                    # state 31
                elif text == REMOVE_BUTTON:
                    mwe_to_remove = person.getSelectedTokensIndexTuple(p)
                    mwe_flat = renderMwe(p, mwe_to_remove, True, False)
                    #logging.debug("after REMOVE_MWE_BUTTON: " + str(mwe_to_remove))
                    #logging.debug("mwe_to_remove: " + str(mwe_to_remove))
                    #logging.debug("current mwes: " + str(p.selectedMwes))
                    sentAnno.removeMwe(p, mwe_to_remove)  #mirroring on person
                    #person.removeMwe(p, mwe_to_remove)
                    tell(p.chat_id,
                         CHECK + " The MWE " + mwe_flat + " has been removed!",
                         markdown=True)
                    displayCurrentSentence(p)
                    # state 1
                elif text == CHANGE_MWE_ELEMENTS:
                    p.currentMweTmp = person.getSelectedTokensIndexTuple(p)
                    dealWithTokensSelection(p)
                    # state 30
                elif text == CHANGE_MWE_CONFIDENCE:
                    askConfidenceLevel(p)
                    # state 70
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            elif p.state == 50:
                # modify MWE
                if text == BACK_BUTTON:
                    displayCurrentSentence(p)
                    # state 1
                elif util.RepresentsInt(text):
                    mwe_number = int(text)
                    jumpToModifySingleMwe(p, mwe_number)
                    # state 40
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            elif p.state == 60:
                # remove MWE
                if text == BACK_BUTTON:
                    displayCurrentSentence(p)
                    # state 1
                elif util.RepresentsInt(text):
                    mwe_number = int(text)
                    dealWithRemoveMwe(p, mwe_number)
                    # state 1
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            elif p.state == 70:
                # change confidence level MWE
                if text == BACK_BUTTON:
                    displayCurrentSentence(p)
                    # state 1
                elif util.RepresentsInt(text):
                    newConf = int(text)
                    dealWithChangeConfidenceLevel(p, newConf)
                    # state 1
                elif text in NUMBERS_0_10[1:]:
                    newConf = NUMBERS_0_10.index(text)
                    dealWithChangeConfidenceLevel(p, newConf)
                    # state 1
                else:
                    reply(FROWNING_FACE + " Sorry, I can't understand...")
            else:
                reply("There is a problem (" + str(p.state).encode('utf-8') +
                      "). Report this to @kercos" + '\n')
                restart(p)
예제 #45
0
 def post(self):
     urlfetch.set_default_fetch_deadline(60)
     body = json.loads(self.request.body)
     self.response.write(json.dumps(body))
     process_cmds(body['message'])
예제 #46
0
def request_scrapy(api_url):
    urlfetch.set_default_fetch_deadline(60)
    content = urlfetch.fetch(api_url).content
    json_content = json.loads(content)
    return json_content['items']
예제 #47
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        # logging.info('request body:')
        logging.info('request body:' + str(body))
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        try:
            message = body['message']
        except:
            message = body['edited_message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        if text is None:
            text = ""
        text = text.encode('utf-8')
        fr = message.get('from')
        username = fr.get('username')
        userid = fr.get('id')
        chat = message['chat']
        chat_type = chat.get('type')
        chat_id = chat['id']
        chat_user = chat.get('username')
        logging.info('the message id was {} and the text was {}'.format(
            message_id, text))
        message_user_id = message['from']['id']

        if not text:
            logging.info('no text')
            return

        def reply_with_keyboard(msg=None, *args):
            listlist = [[arg] for arg in args]
            # self.create_file('/telegram-events-bot.appspot.com/test_file')
            # self.response.write('\n\n')
            resp = urllib2.urlopen(
                BASE_URL + 'sendMessage',
                urllib.urlencode({
                    'chat_id':
                    str(chat_id),
                    'text':
                    msg.encode('utf-8'),
                    'disable_web_page_preview':
                    'true',
                    # 'reply_to_message_id': str(message_id),
                    'reply_markup':
                    json.dumps({
                        'keyboard': listlist,
                        'one_time_keyboard': True,
                        'selective': True
                    })
                })).read()

        # def update_pin():
        #     pin_msg = SuperMessage.get_or_insert('SuperMessage')
        #     pin_chat_id = pin_msg.chat_id
        #     pinned_message_id = pin_msg.pinned_message_id
        #     pin_string = getPinStr()
        #     logging.info('in update pin the chat id is {} and the pinned message to update is {}'.format(pin_chat_id,pinned_message_id))
        #     resp = urllib2.urlopen(BASE_URL + 'editMessageText', urllib.urlencode({
        #         'chat_id': str(pin_chat_id),
        #         'message_id': str(pinned_message_id),
        #         'text': pin_string + "@",
        #         'disable_web_page_preview': 'true'
        #     })).read()
        #
        #     resp = urllib2.urlopen(BASE_URL + 'editMessageText', urllib.urlencode({
        #         'chat_id': str(pin_chat_id),
        #         'message_id': str(pinned_message_id),
        #         'text': pin_string,
        #         'disable_web_page_preview': 'true'
        #     })).read()
        #     logging.info('edit pin send response:')
        #     logging.info(resp)
        #     return resp

        def reply(msg, force_reply=False):
            if force_reply:
                resp = urllib2.urlopen(
                    BASE_URL + 'sendMessage',
                    urllib.urlencode({
                        'chat_id':
                        str(chat_id),
                        'text':
                        msg.encode('utf-8'),
                        'disable_web_page_preview':
                        'true',
                        # 'reply_to_message_id': str(message_id),
                        'reply_markup':
                        json.dumps({
                            'force_reply': True,
                            'selective': True
                        })
                    })).read()
            else:
                resp = urllib2.urlopen(
                    BASE_URL + 'sendMessage',
                    urllib.urlencode({
                        'chat_id': str(chat_id),
                        'text': msg.encode('utf-8'),
                        'disable_web_page_preview': 'true',
                    })).read()

            # logging.info('reply send response:')
            logging.info('reply send response:' + str(resp))
            return resp

        if chat_type == "supergroup":
            if text == '/pinevents':

                # pin_string = getPinStr()
                # pinned_msg_info = json.loads(reply(pin_string))
                #also need to do admin only
                # if chat_id == AUSTIN_CHAT_ID:
                #this is for a specified admin id to pin events
                if message_user_id == 103787344:
                    pin_string = getPinStr()
                    pinned_msg_info = json.loads(reply(pin_string))
                    reply("Please pin this Event Message")
                    pinned_store = SuperMessage.get_or_insert("SuperMessage")
                    pinned_store.pinned_message_id = int(
                        pinned_msg_info['result']['message_id'])
                    pinned_store.chat_id = str(
                        pinned_msg_info['result']['chat']['id'])
                    pinned_store.put()
                else:
                    reply("Please have an admin pin events")
            elif 'events' in text:
                pin_string = getPinStr()
                reply(pin_string)
                # reply('Please message @GroupEventsBot in a private chat for event commands')
            elif 'createevent' in text:
                reply(
                    'Please message @GroupEventsBot in a private chat to create events'
                )
            elif text.startswith('/'):
                sd = text.split()
                if len(sd) > 1:
                    reply("{} turned {} into a {}".format(
                        username,
                        text.split(' ', 1)[1], sd[0].replace("/", "")))
                else:
                    reply("{} is now a {}".format(username,
                                                  sd[0].replace("/", "")))

        elif chat_type == 'private':
            if "test" in text:
                reply("Hey! this is a test!")
            if text.startswith('/'):
                if text == '/start':
                    reply('Bot enabled')
                    setEnabled(chat_id, True)
                elif text == '/stop':
                    reply('Bot disabled')
                    setEnabled(chat_id, False)
                elif text == '/events':
                    reply(getFullPinStr())
                elif text == "/updatePin":
                    #update_pin()
                    pass
                elif text == '/createevent':
                    reply(
                        "Hey there! What would you like the name of the Event to be?\nplease do not use emojis, or special characters for text input"
                    )
                    SESSION_USERS[userid] = "EventName"
                else:
                    reply(
                        'use /createevent to create your event\ncoming soon: edit your own events + reminders\nIf you need to edit your event after its been created, for now message an admin for the group'
                    )

            elif userid in SESSION_USERS:
                if SESSION_USERS[userid] == "EventName":
                    if '\n' in text:
                        reply("please use one line for the event name")
                    else:
                        reply(
                            'what day is the event happening on?\n(Events will only show up within a week)\nex: tomorrow, next saturday, June 23, 6/3/2016'
                        )
                        USER_GROUP_NAME[userid] = text
                        SESSION_USERS[userid] = "GetDate"

                elif SESSION_USERS[userid] == "GetDate":
                    reply('when does the event start?\nex: 4 pm, 3:25 am')
                    USER_DATE[userid] = cal.parse(text)
                    SESSION_USERS[userid] = "GetStartTime"

                elif SESSION_USERS[userid] == "GetStartTime":
                    reply(
                        'what is the event description? (under 400 characters)'
                    )
                    USER_START_TIME[userid] = cal.parse(text)
                    SESSION_USERS[userid] = "GetDescription"

                elif SESSION_USERS[userid] == "GetDescription":
                    USER_EVENT_DESCRIPTION[userid] = text
                    if len(text) > 400:
                        reply(
                            "description is too long, please keep it under 400 characters, length: "
                            + str(len(text)))
                        reply(
                            "what is the event description? (under 400 characters)"
                        )
                        SESSION_USERS[userid] = "GetDescription"
                    else:
                        usertime = time.strftime("%B %d", USER_DATE[userid][0])
                        starttime = time.strftime(
                            "%I %p", USER_START_TIME[userid][0]).lower()
                        reply("{}:\n{}\n{}\nstarts at: {}\norganizer: @{}".
                              format(usertime, USER_GROUP_NAME[userid], text,
                                     starttime, chat_user),
                              force_reply=False)
                        reply_with_keyboard('does this sound right?', 'yes',
                                            'no')
                        SESSION_USERS[userid] = "GetConfirmation"

                elif SESSION_USERS[userid] == "GetConfirmation":
                    if "yes" in text:
                        reply("Cool! I'll add it to the events schedule",
                              force_reply=False)
                        SESSION_USERS[userid] = "Done"
                        e = Event(organizer=chat_user,
                                  organizer_id=chat_id,
                                  eventname=USER_GROUP_NAME[userid],
                                  day=datetime.datetime.fromtimestamp(
                                      time.mktime(USER_DATE[userid][0])),
                                  starttime=cst_to_utc(
                                      datetime.time(
                                          USER_START_TIME[userid][0].tm_hour,
                                          USER_START_TIME[userid][0].tm_min,
                                          tzinfo=CST1())),
                                  description=USER_EVENT_DESCRIPTION[userid])
                        r = e.put()
                        update_pin()
                        event_to_notify = getfmtevent(e, tmz_info=UTC())
                        event_to_notify = "NEW EVENT!!:\n{}".format(
                            event_to_notify)
                        pin_msg = SuperMessage.get_or_insert('SuperMessage')
                        pin_chat_id = pin_msg.chat_id
                        post_new_event(msg=event_to_notify,
                                       chat_id=pin_chat_id)
                    else:
                        reply(
                            "What would you like the name of the Event to be?")
                        SESSION_USERS[userid] = "EventName"

                        #need to save event and add it to schedule

            elif 'who are you' in text:
                reply('I make the events :D')
            elif 'what time' in text:
                reply('look at the corner of your screen!')
            else:
                if getEnabled(chat_id):
                    reply(
                        'use /createevent to create your own event!\ncoming soon: edit your own events + reminders\nIf you need to edit your event after its been created, for now message an admin for the group'
                    )
                else:
                    logging.info('not enabled for chat_id {}'.format(chat_id))
예제 #48
0
### Global Variables Begin ###
master_url = "http://www.codechef.com/problems/";
sub_urls = ["easy", "medium", "hard", "challenge", "school"] #extcontest is not scraped bcoz it contains a LARGE amount of problems 1492
user_url = "http://www.codechef.com/users/"
username_pattern = re.compile('^[a-z]{1}[a-z0-9_]{3,13}$')	

JINJA_ENVIRONMENT = jinja2.Environment(
		loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
		extensions=['jinja2.ext.autoescape'],
		autoescape=True)

tag_set = set(['2-sat', 'bit', 'adhoc', 'aho-corasick', 'articulation-point', 'backtracking', 'bellman-ford', 'bfs', 'biconnected-comp', 'big-integer', 'binary-numbers', 'binary-search', 'binary-tree', 'binomial', 'bipartite', 'bitmasking', 'bitwise', 'bridges', 'brute-force', 'burnside', 'chinese-remainder', 'combinatorics', 'complete-search', 'convex-hull', 'data-structure', 'decomposition', 'deque', 'dfs', 'dijkstra', 'discretization', 'disjoint-set', 'divide-and-conquer', 'dp', 'enumeration', 'euler-tour', 'expectation', 'exponentiation', 'factorial', 'factorization', 'fft', 'fibonacci', 'floyd-warshall', 'game-theory', 'gauss-elim', 'gcd', 'geometry', 'grammar-parsing', 'graphs', 'greedy', 'hashing', 'heaps', 'heavy-light', 'heuristic', 'hungarian', 'impartial-game', 'implementation', 'inclusion-exclusion', 'inversions', 'kd-tree', 'kmp', 'kruskal', 'lca', 'lcp', 'line-sweep', 'linear-programming', 'link-cut-tree', 'matching', 'maths', 'matrix-expo', 'max-independent-set', 'maxflow', 'meet-in-middle', 'memoization', 'miller-rabin', 'min-cost-flow', 'mincut', 'modulo', 'mst', 'newton-raphson', 'number-theory', 'offline-query', 'order-statistic', 'palindrome', 'permutation', 'persistence', 'pigeonhole', 'polygons', 'precision', 'prefix-sum', 'preprocessing', 'prim', 'prime', 'probability', 'queue', 'rabin-karp', 'recurrence', 'recursion', 'regex', 'scc', 'segment-tree', 'shortest-path', 'sieve', 'sliding-window', 'sorting', 'sparse-tables', 'splay-tree', 'sprague-grundy', 'sqrt-decomposition', 'stable-marriage', 'stack', 'string', 'suffix-array', 'suffix-auto', 'suffix-trees', 'ternary-search', 'topological-sort', 'treap', 'tree-dp', 'trees', 'trie', 'two-pointers', 'union-find', 'vertex-cover', 'xor', 'zero-sum-game'])
### Global Variables End ###

urlfetch.set_default_fetch_deadline(60)
ereporter.register_logger()

### Models Begin ###
class Problem(ndb.Model):
	"""
	Model for Problem on Codechef. ID of this model is problem code.
	"""
	pname = ndb.StringProperty(required = True)
	submissions = ndb.IntegerProperty(required = True)
	accuracy = ndb.FloatProperty(required = True)
	author = ndb.StringProperty()
	num_fav = ndb.IntegerProperty(default = 0)
	sum_rating = ndb.IntegerProperty(indexed = False, default = 0)
	num_rating = ndb.IntegerProperty(indexed = False, default = 0)
	avg_rating = ndb.ComputedProperty(lambda self : (self.sum_rating*1.0)/(self.num_rating) if self.num_rating else None)
예제 #49
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        try:
            message = body['message']
        except:
            message = body['edited_message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']

        if not text:
            logging.info('no text')
            return

        def reply(msg=None, img=None):
            if msg:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'text': msg.encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'reply_to_message_id': str(message_id),
                })).read()
            elif img:
                resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                    ('chat_id', str(chat_id)),
                    ('reply_to_message_id', str(message_id)),
                ], [
                    ('photo', 'image.jpg', img),
                ])
            else:
                logging.error('no msg or img specified')
                resp = None

            logging.info('send response:')
            logging.info(resp)

        if text.startswith('/'):
            if text == '/start':
                reply('Bot enabled')
                setEnabled(chat_id, True)
            elif text == '/stop':
                reply('Bot disabled')
                setEnabled(chat_id, False)
            elif text == '/image':
                img = Image.new('RGB', (512, 512))
                base = random.randint(0, 16777216)
                pixels = [base+i*j for i in range(512) for j in range(512)]  # generate sample image
                img.putdata(pixels)
                output = StringIO.StringIO()
                img.save(output, 'JPEG')
                reply(img=output.getvalue())
            else:
                reply('What command?')

        # CUSTOMIZE FROM HERE
        elif '/drop' in text
            reply('this is /drop')
        elif 'drop' in text
            reply('this is just drop')

        elif 'who are you' in text:
            reply('telebot starter kit, created by yukuku: https://github.com/yukuku/telebot')
        elif 'what time' in text:
            reply('look at the corner of your screen!')
        else:
            if getEnabled(chat_id):
                reply('I got your message! (but I do not know how to answer)')
            else:
                logging.info('not enabled for chat_id {}'.format(chat_id))
예제 #50
0
 def get(self):
     urlfetch.set_default_fetch_deadline(60)
     self.response.write(
         json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))
예제 #51
0
URL_FETCH_TIMEOUT = 60

WIKI_URL = ""

# Create the Jinja templating system we use to dynamically generate HTML. See:
# http://jinja.pocoo.org/docs/dev/
JINJA2_ENVIRONMENT = jinja2.Environment(loader=jinja2.FileSystemLoader(
    os.path.dirname(__file__)),
                                        autoescape=True,
                                        extensions=['jinja2.ext.autoescape'])

ee.Initialize(config.EE_CREDENTIALS)

ee.data.setDeadline(URL_FETCH_TIMEOUT)
socket.setdefaulttimeout(URL_FETCH_TIMEOUT)
urlfetch.set_default_fetch_deadline(URL_FETCH_TIMEOUT)

# set the collection ID

IMAGE_COLLECTION_ID = 'MODIS/MYD13A1'

ref_start = '2000-01-01'
ref_end = '2008-12-31'
series_start = '2006-01-01'
series_end = '2016-12-31'

counter = 0
CountryorProvince = 0

id_list = []
value_list = []
예제 #52
0
  def post(self):
    """Request handler for POST operations."""
    try:
      start_time = time.time()
      urlfetch.set_default_fetch_deadline(URLFETCH_TIMEOUT)

      config = explorer_config.ExplorerConfigModel.Get()

      request_data = json.loads(self.request.body)

      datasource = request_data.get('datasource')
      if not datasource:
        raise KeyError('The datasource is required to run a query')

      query = datasource.get('query_exec') or datasource.get('query')

      if not query:
        raise KeyError('datasource.query must be provided.')

      if (not config.grant_query_to_public and
          not users.is_current_user_admin()):
        dashboard_id = request_data.get('dashboard_id')
        if not dashboard_id:
          raise KeyError('The dashboard id is required to run a query')

        widget_id = request_data.get('id')
        if not widget_id:
          raise KeyError('The widget id is required to run a query')

        if dashboard.Dashboard.IsQueryCustom(query, dashboard_id, widget_id):
          raise SecurityError('The user is not authorized to run custom queries')
        else:
          logging.error('Query is identical.')

      cache_duration = config.cache_duration or None

      logging.debug('Query datasource: %s', datasource)
      query_config = datasource['config']

      if datasource.get('type', 'BigQuery') == 'Cloud SQL':
        logging.debug('Using Cloud SQL backend')
        cloudsql_client_config = query_config.get('cloudsql')
        if not cloudsql_client_config:
          cloudsql_client_config = {}

        cloudsql_server_config = cloudsql_config.CloudsqlConfigModel.Get()

        client = gae_cloud_sql_client.GaeCloudSqlClient(
          instance=cloudsql_client_config.get('instance'),
          db_name=cloudsql_client_config.get('database_name'),
          db_user=cloudsql_server_config.username,
          db_password=cloudsql_server_config.password)
      else:
        logging.debug('Using BigQuery backend')
        client = DataHandlerUtil.GetDataClient(self.env)

      client.project_id = config.default_project
      response = client.Query(query, cache_duration=cache_duration)

      if query_config['results'].get('pivot'):
        pivot_config = query_config['results']['pivot_config']

        transformer = big_query_result_pivot.BigQueryPivotTransformer(
            reply=response,
            rows_name=pivot_config['row_field'],
            columns_name=pivot_config['column_field'],
            values_name=pivot_config['value_field'])
        transformer.Transform()

      response['results'] = (
          result_util.ReplyFormatter.RowsToDataTableFormat(response))

      elapsed_time = time.time() - start_time
      response['elapsedTime'] = elapsed_time
      self.RenderJson(response)

    # If 'expected' errors occur (specifically dealing with SQL problems),
    # return JSON with descriptive text so that we can give the user a
    # constructive error message.
    # TODO: Formalize error reporting/handling across the application.
    except (big_query_client.BigQueryError, big_query_result_pivot.DuplicateValueError,
            ValueError, KeyError, SecurityError) as err:
      logging.error(str(err))
      self.RenderJson({'error': str(err)})
    except MySQLdb.OperationalError as err:
      self.RenderJson({'error': 'MySQLdb error %s' % str(err)})
    except (google.appengine.runtime.DeadlineExceededError,
            apiproxy_errors.DeadlineExceededError,
            urlfetch_errors.DeadlineExceededError):
      self.RenderText(text=ERROR_TIMEOUT, status=408)
예제 #53
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        message = body['message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']

        if not text:
            logging.info('no text')
            return

        def reply(msg=None, img=None):
            if msg:
                resp = urllib2.urlopen(
                    BASE_URL + 'sendMessage',
                    urllib.urlencode({
                        'chat_id': str(chat_id),
                        'text': msg.encode('utf-8'),
                        'disable_web_page_preview': 'true',
                        'reply_to_message_id': str(message_id),
                    })).read()
            elif img:
                resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                    ('chat_id', str(chat_id)),
                    ('reply_to_message_id', str(message_id)),
                ], [
                    ('photo', 'image.jpg', img),
                ])
            else:
                logging.error('no msg or img specified')
                resp = None

            logging.info('send response:')
            logging.info(resp)

        if text.startswith('/'):
            if text == '/start':
                reply('Bot enabled')
                setEnabled(chat_id, True)
            elif text == '/stop':
                reply('Bot disabled')
                setEnabled(chat_id, False)
            elif text == '/image':
                img = Image.new('RGB', (512, 512))
                base = random.randint(0, 16777216)
                pixels = [
                    base + i * j for i in range(512) for j in range(512)
                ]  # generate sample image
                img.putdata(pixels)
                output = StringIO.StringIO()
                img.save(output, 'JPEG')
                reply(img=output.getvalue())
            else:
                reply('What command?')

        # CUSTOMIZE FROM HERE

        elif 'who are you' in text:
            reply(
                'telebot starter kit, created by yukuku: https://github.com/yukuku/telebot'
            )
        elif 'what time' in text:
            reply('look at the top-right corner of your screen!')
        else:
            if getEnabled(chat_id):
                resp1 = json.load(
                    urllib2.urlopen(
                        'http://www.simsimi.com/requestChat?lc=en&ft=1.0&req='
                        + urllib.quote_plus(text.encode('utf-8'))))
                back = resp1.get('res')
                if not back:
                    reply('okay...')
                elif 'I HAVE NO RESPONSE' in back:
                    reply('you said something with no meaning')
                else:
                    reply(back)
            else:
                logging.info('not enabled for chat_id {}'.format(chat_id))
예제 #54
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        message = body['message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']

        if not text:
            logging.info('no text')
            return

        def reply(msg=None, img=None):
            if msg:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'text': msg.encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'reply_to_message_id': str(message_id),
                })).read()
            elif img:
                resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                    ('chat_id', str(chat_id)),
                    ('reply_to_message_id', str(message_id)),
                ], [
                    ('photo', 'image.jpg', img),
                ])
            else:
                logging.error('no msg or img specified')
                resp = None

            logging.info('send response:')
            logging.info(resp)

        if text.startswith('/'):
            if text == '/start':
                reply('Bot enabled')
                setEnabled(chat_id, True)
            elif text == '/stop':
                reply('Bot disabled')
                setEnabled(chat_id, False)
            elif text == '/image':
                img = Image.new('RGB', (512, 512))
                base = random.randint(0, 16777216)
                pixels = [base+i*j for i in range(512) for j in range(512)]  # generate sample image
                img.putdata(pixels)
                output = StringIO.StringIO()
                img.save(output, 'JPEG')
                reply(img=output.getvalue())
            elif text == '/ping':
                reply('Pong')
            elif text == '/help':
                reply('Until now I can do nothing')
            elif text == '/botinfo':
                reply('name: slavebot \nbotname: @ultraslavebot \nadmin: @jhusser \nprivacy: disabled\n groups: enabled')
            elif text == '/hodor':

                    reply('HODOR!')
            elif text == '/test':

                    reply('/hodor')
            elif text == '/shutdown':

                    reply('Attention! Bot is going to sleep.')


            elif text == '/whoareyou':
                    reply("I'm a very usefull bot")

         elif text == '/rolldice':
                DICENUMBER = random.randint(1,6)
                reply(DICENUMBER)

        elif text == '/newmsg':
                reply("test")




        # CUSTOMIZE FROM HERE


        elif 'what time' in text:
            reply('look at the top-right corner of your screen!')
        elif text == 'H':
            reply('O')
            Htester = 'true'
        elif text == 'O':
            reply('D')
        elif text == 'D':
            reply('O')
        elif text == '0':
            reply('R')
        elif text == 'R':
            reply('Htester: ' + Htester)
            Htester = 'false'
            reply('Htester: ' + Htester)


        else:
            if getEnabled(chat_id):
                try:
                    resp1 = json.load(urllib2.urlopen('http://www.simsimi.com/requestChat?lc=en&ft=1.0&req=' + urllib.quote_plus(text.encode('utf-8'))))
                    back = resp1.get('res').get('msg')
                except urllib2.HTTPError, err:
                    logging.error(err)
                    back = str(err)
                if not back:
                    reply('okay...')
                elif 'I HAVE NO RESPONSE' in back:
                    reply('you said something with no meaning')
                else:
                    reply(back)
            else:
예제 #55
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        try:
        	message = body['message']
        except:
        	message = body['edited_message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']
        privacy = chat['type']

        if not text:
            logging.info('no text')
            return

        def reply(msg=None, img=None, stk=None, audio=None, doc=None, fw=None, chat=None, chat1=None, chat2=None, chat3=None, chat4=None, chat5=None):
            if msg:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'text': msg,
                    'disable_web_page_preview': 'true',
                    'parse_mode': 'HTML',
                })).read()
            elif img:
                resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                    ('chat_id', str(chat_id)),
                ], [
                    ('photo', 'image.jpg', img),
                ])
            elif stk:
                resp = urllib2.urlopen(BASE_URL + 'sendSticker', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'sticker': stk,
                })).read()
            elif audio:
                resp = urllib2.urlopen(BASE_URL + 'sendAudio', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'audio': audio,
                })).read()
            elif doc:
                resp = urllib2.urlopen(BASE_URL + 'sendDocument', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'document': doc,
                })).read()
            elif fw:
                resp = urllib2.urlopen(BASE_URL + 'forwardMessage', urllib.urlencode({
                    'chat_id': fw,
                    'from_chat_id': str(chat_id),
                    'message_id': str(message_id),
                    'parse_mode': 'HTML',
                })).read()
            elif chat:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': '-12787170',
                    'text': chat.replace("=PBZZ=", "").encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'parse_mode': 'HTML',
                })).read()
            elif chat1:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': '-36729866',
                    'text': chat1.replace("=STUDENTIPER=", "").encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'parse_mode': 'HTML',
                })).read()
            elif chat2:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': '-12604594',
                    'text': chat2.replace("=LAPA=", "").encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'parse_mode': 'HTML',
                })).read()
            elif chat3:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': '-7284886',
                    'text': chat3.replace("=3LZ=", "").encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'parse_mode': 'HTML',
                })).read()
            elif chat4:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': '-23982686',
                    'text': chat4.replace("=1AK=", "").encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'parse_mode': 'HTML',
                })).read()
            elif chat5:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': '-18336711',
                    'text': chat5.replace("=1LZ=", "").encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'parse_mode': 'HTML',
                })).read()
            else:
                logging.error('no msg or img specified')
                resp = None

            logging.info('send response:')
            logging.info(resp)

        if text.startswith('/'):
            if text.lower() == '/start' or text.lower() == '/start@sarcinellobot':
                reply('<b>Sarcinello</b> attivato.\nInvia /comandi per elencare le funzioni disponibili.\nPer contattare mio padre: @nicoserafino')
                setEnabled(chat_id, True)
            elif text.lower() == '/stop' or text.lower() == '/stop@sarcinellobot':
                reply('<b>Sarcinello</b> disattivato.\nInvia /start per attivarlo.')
                setEnabled(chat_id, False)
        if text.lower() == '/comandi' or text.lower() == '/comandi@sarcinellobot':
            reply('Ciao! sono il <b>Sarcinello</b>. Per contattare mio padre: @nicoserafino\n\n/start per attivare il Sarcinello\n/stop per disattivarlo\n\nsuggerimento - Suggeriscimi una funzione\n\nsegreteria\n\naula studio - per gli orari delle aule studio\n\nbiblioteca - per gli orari della biblioteca\n\nmappa del policlinico\n\nade, pezzolla, carratù\n\ntasse, iscrizioni, simulatore\n\nscandeza rate\n\nisee, iseu\n\nadisu, bando\n\nfrequenze, badge\n\npropedeuticità\n\nmodulistica\n\norari, calendario\n\nprogrammi\n\n appelli\n\n programma di [disciplina] - ad esempio "programma di chimica"\n\nnodbis, laurea\n\nprogress test')

        # CUSTOMIZE FROM HERE 
		# Testo			reply('testo')
        # Immagini		reply(img=urllib2.urlopen('https://telegram.org/img/t_logo.png').read())
		# Sticker		reply(stk='file_id')
		# Audio			reply(audio='file_id')
		# Documenti		reply(doc='file_id')

		#   elif privacy == 'private':
		#		reply(fw='43591247')

		# TODO
		# if 'appell' in text.lower() and 'capuano' in text.lower():
		# 	reply(URL_ESAMI + '1267')

        elif text.lower() != 'suggerimento' and 'suggerimento' in text.lower():
			reply(fw='43591247')
			reply('Grazie per il tuo suggerimento!')
        elif text.startswith('=PBZZ='):
			reply(chat=text)
        elif text.startswith('=STUDENTIPER='):
			reply(chat1=text)
        elif text.startswith('=LAPA='):
			reply(chat2=text)
        elif text.startswith('=3LZ='):
			reply(chat3=text)
        elif text.startswith('=1AK='):
			reply(chat4=text)
        elif text.startswith('=1LZ='):
			reply(chat5=text)
        else:
            if getEnabled(chat_id):
				#if 'elezion' in text.lower() or 'voto' in text.lower() or 'vota' in text.lower() or 'votazione' in text.lower() or 'votare' in text.lower() or 'candidat' in text.lower() or 'rappresentan' in text.lower() or 'lista' in text.lower() or 'liste' in text.lower() or '18' in text.lower() or '19' in text.lower() or 'maggio' in text.lower() or 'persempre' in text.lower() or 'per sempre' in text.lower() or 'studentiper' in text.lower() or 'UP' in text.lower() or 'seggi' in text.lower() or 'scheda' in text.lower() or 'elettorale' in text.lower():
				#	reply('Il 18 e il 19 maggio vota StudentiPer (UP ai centrali).\n\nLe nostre conquiste..')
				#	reply(img=urllib2.urlopen('http://creop.altervista.org/Media/Foto/elezioni1.jpg').read())
				#	reply('E quello che continueremo a fare..#PerSempre')
				#	reply(img=urllib2.urlopen('http://creop.altervista.org/Media/Foto/elezioni2.jpg').read())
				if ('nico' in text.lower().split() or 'quaranta' in text.lower().split() or '40' in text.split()) and not 'la nico' in text.lower():
					reply(stk='BQADBAADGwADTyaZAjUU-thrRuh9Ag')
				if 'pita' in text.lower():
					reply('Pita è sfigata ' + u'\U0001F62D'.encode('utf-8'))
				if 'gestione' in text.lower() or 'gestitevi' in text.lower():
					reply(stk='BQADBAADHQADTyaZAiyn9ScUMAh6Ag')
				if 'parisi' in text.lower() or ( 'st' in text.lower() and 'bene' in text.lower() ):
					reply('Parisi sta bene!')
				if 'lucilla' in text.lower():
					reply('Lucilla ti guarda.')
				if 'convenzioni' in text.lower():
					reply('Tutti i vantaggi che offre la StudentiPer Card. Richiedila in auletta!\n http://app.studentiper.it/convenzioni.html')
				if 'auguri' in text.lower():
					reply('Auguri!')
				if text.lower() == 'suggerimento':
					reply('Per suggerire scrivi qualcosa dopo la parola \'suggerimento\'')
				if 'mappa' in text.lower() or ('mappa' in text.lower() and 'policlinico' in text.lower()):
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/mappa.jpg').read())
				if 'erasmus' in text.lower():
					reply('Tutte le info riguardanti il programma Erasmus per gli studenti Uniba sono disponibili sul sito web http://uniba.llpmanager.it/studenti/')
				if 'segreteria' in text.lower():
					reply('La segreteria è aperta dal Lunedi al Venerdi mattina dalle 10 alle 12. Martedi e Giovedi pomeriggio dalle 15 alle 17.')
				if 'rata' in text.lower() or 'rate' in text.lower() or ('scade' in text.lower() and 'rat' in text.lower()):
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/rate.jpg').read())
				if ('mail' in text.lower() and 'prof' in text.lower()) or 'ricevimento' in text.lower():
					reply('Questo sito riporta indirizzi e-mail e curricula (cliccando sul nome) dei professori.\n http://www.medicina.uniba.it/personale/index.jsp \nSolitamente nel curriculum è riportato l\'orario di ricevimento abituale.')
				if 'ade' in (text.lower()).split() or 'ade?' in (text.lower()).split() or 'pezzolla' in text.lower() or 'carrat' in text.lower():
					reply('Le ADE sono attività didattiche elettive, cioè seminari e convegni che si tengono al policlinico in orari che non interferiscano con l\'attività didattica ordinaria. Con la partecipazione a questi eventi si ottengono attestati e CFU (spesso 0.3CFU a sessione). Raggiunto il numero di CFU in ADE previsti dal piano di studio per l\'anno in corso, gli attestati vanno consegnati alle Prof. Carratù (AK) o Pezzolla (LZ) come segue.\n\nPER GLI AK\nLa prof.ssa Carratù riceve lunedì e mercoledì dalle 9.30 alle 12.30 primo piano degli istituti biologici.\n\nPER GLI LZ\nLa prof.ssa Pezzolla riceve dal 13 settembre settembre il martedi dalle 10 alle 12 al quarto piano del padiglione Asclepios (ingresso Pronto Soccorso).\n\nIl modulo ADE va consegnato alle professoresse entro il 30 settembre con gli attestati originali in allegato. Successivamente il solo modulo, privo degli attestati, va ritirato (AK il giovedì, LZ il martedì della settimana successiva con lo stesso orario)e consegnato in segreteria entro il 31 ottobre.')
				if 'moduli' in text.lower() or 'modulo' in text.lower():
					reply('Segui questo link per tutta la modulistica http://goo.gl/FnFD0C')
				if 'cus' in text.lower().split():
					reply('Il CUS, Centro Universitario Sportivo, offre numerosi vantaggi per gli studenti dell\'Uniba. L\'iscrizione è di 20€ (15€ se è la tua prima iscrizione). Queste le convenzioni:')
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/cus.jpg').read())
				if 'aula' in text.lower() and 'studio' in text.lower() or ('sal' in text.lower() and 'lettur' in text.lower()):
					reply('Gli orari di apertura dell\'aula studio del polinfunzionale sono quelli indicati nell\'immagine seguente.\nL\'aula studio del dipartimento di Biochimica è aperta dalle 8.15 alle 18.00.\nPer la biblioteca centrale digita \'biblioteca\'')
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/polifunzionale.jpg').read())
				if 'biblioteca' in text.lower():
					reply('Orari biblioteca centrale: http://goo.gl/948hLn\nPer l\'aula studio digita \'aula studio\'')
				if 'propedeuticit' in text.lower() or 'propedeutic' in text.lower():
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/propedeut.jpg').read())
				if 'nodbis' in text.lower() or 'laurea' in text.lower():
					reply('Leggi qui il regolamento nodbis http://goo.gl/bkCtjD')
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/nodbis.jpg').read())
				if 'progress' in text.lower() and 'test' in text.lower():
					reply('Il progress test è una prova nazionale che consiste in due quiz a risposta multipla, ognuno da 150 domande (150 di area pre-clinica e 150 di area clinica).\n\nLa frequenza della giornata è assegnata purché lo studente consegni le risposte dopo almeno 45 minuti dall\'inizio di ogni prova. Se lo studente supera il punteggio medio del proprio anno, valutato sulla base dei test svolti negli anni precedenti, sarà assegnato un punto aggiuntivo nella media dei voti per il calcolo del voto di presentazione alla seduta di Laurea.\n(*nessun voto sarà formalmente modificato)')
				if 'adisu' in text.lower() or ('borsa' in text.lower() and 'studio' in text.lower()) or 'bando' in (text.lower()).split():
					reply('BANDO ADISU 2015/16: http://goo.gl/Py2HYR \nSCADENZE: http://goo.gl/nTikvg \n\nL\'ufficio in Via Giustino Fortunato è aperto dal lunedì al venerdì dalle 9 alle 12. Il martedì e il giovedì anche il pomeriggio dalle 15 alle 16.')
				if 'tasse' in text.lower().split() or 'immatricolazion' in text.lower():
					reply('PAGA PIAGA!\nSe cerchi il simulatore tasse del sito UNIBA segui questo link: https://csi.ict.uniba.it/simulatoretasse')
					reply('II rata prorogata al 20 ottobre.\nIII rata rateizzata 50% entro il 20 novembre e 50% entro il 28 febbraio.\nI rata 2016/17 entro il 20 dicembre.')
				if 'iscrizion' in text.lower() or 'sbarrament' in text.lower():
					reply('Per iscriversi ad anni successivi occorrono i seguenti requisiti entro Febbraio 2017')
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/sbarramenti.png').read())
				if 'simulatore' in text.lower():
					reply('Per il simulatore tasse by StudentiPer segui questo link: http://goo.gl/g6IRLp')
				if 'isee' in text.lower() or 'iseu' in text.lower():
					reply('L\'ISEE per prestazioni universitarie va richiesto al CAF (dovrai ripassare 15 giorni dopo per ricevere copia cartacea). Successivamente l\'uniba lo recupera telematicamente, quindi non c\'è alcuna documentazione da consegnare in segreteria. Per i servizi ADISU, invece, occorre copia cartacea.\nScrivi "iscrizione" per info sull\'iscrizione ad anni successivi.')
				if 'frequenz' in text.lower() or 'intermedi' in text.lower() or 'badge' in text.lower():
					reply('Il badge non sarà utilizzato nemmeno quest\'anno (ascolta l\'audio).\nLe frequenze delle prove intermedie non sono richieste per sostenere l\'esame. L\'ex primo anno ha solo quelle di citologia, ma per un errore della segreteria.')
				if 'calendario' in text.lower() or ('orari' in text.lower() and not 'segreteria' in text.lower() and not 'ricevimento' in text.lower() and not 'biblioteca' in text.lower() and not 'aula' in text.lower()):
#					reply('Gli orari del secondo semestre A.A. 2016-2017 non sono ancora disponibili')
					reply('Per quale anno? Ora disponibili:\n /I_AK \n /I_LZ \n /II_AK \n /II_LZ \n /III_AK \n /III_LAPA \n /III_PBZZ \n /IV_AK \n /IV_LZ \n /V_AK \n /V_LZ')
				if 'materiale' in text.lower() and 'didattico' in text.lower():
					reply('Segui questo link per l\'archivio su Drive (secondo anno LZ) https://goo.gl/xGceWn')
				if text == '/I_AK' or text == '/I_AK@sarcinellobot':
					reply('Orari I_AK: https://goo.gl/PX1DFN')
				if text == '/I_LZ' or text == '/I_LZ@sarcinellobot':
					reply('Orari I_LZ: https://goo.gl/3Ug8Wc')
				if text == '/II_AK' or text == '/II_AK@sarcinellobot':
					reply('Orari II_AK: https://goo.gl/8hhgls')
				if text == '/II_LZ' or text == '/II_LZ@sarcinellobot':
					reply('Orari II_LZ: https://goo.gl/eTQ3z2')
				if text == '/III_AK-A' or text == '/III_AK-A@sarcinellobot':
					reply('Orari III_AK-A: https://goo.gl/JTTauS')
				if text == '/III_AK-B' or text == '/III_AK-B@sarcinellobot':
					reply('Orari III_AK-B: https://goo.gl/sdPumR')
				if text == '/III_LAPA' or text == '/III_LAPA@sarcinellobot':
					reply('Orari III_LAPA: https://goo.gl/TMyCTV')
				if text == '/III_PBZZ' or text == '/III_PBZZ@sarcinellobot':
					reply('Orari III_PBZZ: https://goo.gl/pI58BE')
				if text == '/IV_AK' or text == '/IV_AK@sarcinellobot':
					reply('Orari IV_AK: https://goo.gl/vVsZSG')
				if text == '/IV_LZ' or text == '/IV_LZ@sarcinellobot':
					reply('Orari IV_LZ: https://goo.gl/YFAazr')
				if text == '/V_AK' or text == '/V_AK@sarcinellobot':
					reply('Orari V_AK: https://goo.gl/PkJJ2e')
				if text == '/V_LZ' or text == '/V_LZ@sarcinellobot':
					reply('Orari V_LZ: https://goo.gl/zFo4vk')
				if text == '/VI_AK' or text == '/VI_AK@sarcinellobot':
					reply('Orari VI_AK: https://goo.gl/bHYCr1')
				if text == '/VI_LZ' or text == '/VI_LZ@sarcinellobot':
					reply('Orari VI_LZ: https://goo.gl/AhfW47')
				if text.lower() == 'anatomia' or ('anatomia' in text.lower() and 'appell' in text.lower()):
					reply('Incubo!')
				if 'appelli' in text.lower() or 'appello' in text.lower():
					reply('Cerchi gli appelli per quale anno? Ora disponibili:\n /esami_I \n /esami_II \n /esami_III')
				if text == '/esami_I' or text == '/esami_I@sarcinellobot':
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/esami1.jpg').read())
				if text == '/esami_II' or text == '/esami_II@sarcinellobot':
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/esami2.jpg').read())
				if text == '/esami_III' or text == '/esami_III@sarcinellobot':
					reply(img=urllib2.urlopen('http://creop.altervista.org/Sarcinellobot/Media/Foto/esami3.jpg').read())
				if 'programmi' in text.lower():
					reply('Segui questo link per tutti i programmi http://goo.gl/v69OQt')
				if 'programm' in text.lower() and 'umane' in text.lower():
					reply('Segui questo link per il programma di Scienze Umane http://goo.gl/6ZUh5W')
				if 'programm' in text.lower() and ( 'biologia' in text.lower() or 'genetica' in text.lower() ) and 'micro' not in text.lower():
					reply('Segui questo link per il programma di Biologia e Genetica http://goo.gl/h8TWlc')
				if 'programm' in text.lower() and ( 'fisica' in text.lower() or 'informatica' in text.lower() ):
					reply('Segui questo link per il programma di Fisica e Informatica http://goo.gl/VV8PSG')
				if 'programm' in text.lower() and 'chimica' in text.lower().split():
					reply('Segui questo link per il programma di Chimica http://goo.gl/pVAp0o')
				if 'programm' in text.lower() and ( 'biochimica' in text.lower() or 'molecolare' in text.lower() ):
					reply('Ecco il programma di Biochimica parte1, parte2 e Biologia Molecolare http://goo.gl/ESNxAJ')
				if 'programm' in text.lower() and ( 'isto' in text.lower() or 'embrio' in text.lower() or 'citologia' in text.lower() ):
					reply('Segui questo link per il programma di Citologia, Istologia ed Embriologia http://goo.gl/NGG8XD')
				if 'programm' in text.lower() and ( 'metodologi' in text.lower() or 'semeiotica' in text.lower() ):
					reply('Segui questo link per il programma di Metodologie I anno http://goo.gl/wZ8Bqu e di Metodologia e Semeiotica Medico-Chirurgica III anno http://goo.gl/RnC35a')
				if 'programm' in text.lower() and 'micro' in text.lower():
					reply('Ecco il programma di microbiologia by Alfonso: http://goo.gl/4sJLdp')
				if 'programm' in text.lower() and 'anatomia' in text.lower() and not 'patologica' in text.lower():
					reply('Segui questo link per il programma di Anatomia http://goo.gl/CQgtfm')
				if 'sito' in text.lower() and ('saccia' in text.lower() or 'anatomia' in text.lower()):
					reply('Ecco il sito del Prof. Saccia:\nhttp://www.matsac.it/anatomia_1_med/')
				if 'programm' in text.lower() and 'fisiologia' in text.lower():
					reply('Segui questo link per il programma di Fisiologia http://goo.gl/JuOsvy')
				if 'programm' in text.lower() and ('patologia' in text.lower() or 'immunologia' in text.lower()):
					reply('Segui questo link per il programma di Patologia e Immunologia http://goo.gl/b9XbvI')
				if 'programm' in text.lower() and 'inglese' in text.lower():
					reply('Segui questo link per il programma di Inglese Scientifico http://goo.gl/FOH54g')
				if 'programm' in text.lower() and 'patologica' in text.lower():
					reply('Segui questo link per il programma di Anatomia Patologica I http://goo.gl/aIwm5L e Anatomia Patologica II http://goo.gl/JC8H7j')
				if 'programm' in text.lower() and 'laboratorio' in text.lower():
					reply('Segui questo link per il programma di Medicina di Laboratorio http://goo.gl/OqHaqK')
				if 'programm' in text.lower() and 'statistica' in text.lower():
					reply('Segui questo link per il programma di Statistica http://goo.gl/lvOfXO')
				if 'programm' in text.lower() and 'specialit' in text.lower():
					reply('Segui questo link per il programma di \n Specialità 1 http://goo.gl/n9ubqV \n Specialità 2 http://goo.gl/Y4GllY \n Specialità 3 http://goo.gl/y3Vus9 \n Specialità 4 http://goo.gl/TvZkPR \n Specialità 5 http://goo.gl/AQxrw6')
				if 'programm' in text.lower() and ('clinica' in text.lower() or 'organi' in text.lower() or 'senso' in text.lower()):
					reply('Segui questo link per il programma di Clinica Medico-Chirurgica degli Organi di Senso http://goo.gl/0EFv5k')
				if privacy == 'private':
					reply(fw='43591247')
            else:
                logging.info('not enabled for chat_id {}'.format(chat_id))
예제 #56
0
 def __init__(self, *args, **kwargs):
     super(BaseHandler, self).__init__(*args, **kwargs)
     # The default deadline of 5 seconds is too aggressive of a target for GCS
     # directory listing operations.
     urlfetch.set_default_fetch_deadline(60)
예제 #57
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        message = body['message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']

        if not text:
            logging.info('no text')
            return

        def reply(msg=None, img=None):
            if msg:
                resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                    'chat_id': str(chat_id),
                    'text': msg.encode('utf-8'),
                    'disable_web_page_preview': 'true',
                    'reply_to_message_id': str(message_id),
                })).read()
            elif img:
                resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                    ('chat_id', str(chat_id)),
                    ('reply_to_message_id', str(message_id)),
                ], [
                    ('photo', 'image.jpg', img),
                ])
            else:
                logging.error('no msg or img specified')
                resp = None

            logging.info('send response:')
            logging.info(resp)
        tmptext = text.lower()
        if text.startswith('/'):
            if text == '/start':
                reply('Bot enabled')
                setEnabled(chat_id, True)
            elif text == '/help':
                reply('Type to find out ip info => ip XX.XX.XX.XX/XX')
            elif text == '/stop':
                reply('Bot disabled')
                setEnabled(chat_id, False)
            elif text == '/image':
                img = Image.new('RGB', (512, 512))
                base = random.randint(0, 16777216)
                pixels = [base+i*j for i in range(512) for j in range(512)]  # generate sample image
                img.putdata(pixels)
                output = StringIO.StringIO()
                img.save(output, 'JPEG')
                reply(img=output.getvalue())
            elif tmptext.startswith('/dns',0,4):
                dns = tmptext.split(" ")
                try:
                    resource_url = 'http://api.statdns.com/'+dns[1]+'/a'
                    resp1 = json.loads(urllib2.urlopen(resource_url).read())
                    reply(resp1['answer'][1]['rdata'])
                except urllib2.HTTPError, err:
                    logging.error(err)
                    back = str(err)
            else:
                reply('What command?')
예제 #58
0
    def post(self):
        """Request handler for POST operations."""
        try:
            start_time = time.time()
            urlfetch.set_default_fetch_deadline(URLFETCH_TIMEOUT)

            config = explorer_config.ExplorerConfigModel.Get()
            client = DataHandlerUtil.GetDataClient(self.env)
            client.project_id = config.default_project

            request_data = json.loads(self.request.body)

            dashboard_id = request_data.get('dashboard_id')
            if not dashboard_id:
                raise KeyError('The dashboard id is required to run a query')

            widget_id = request_data.get('id')
            if not widget_id:
                raise KeyError('The widget id is required to run a query')

            datasource = request_data.get('datasource')
            if not datasource:
                raise KeyError('The datasource is required to run a query')

            query = datasource.get('query_exec') or datasource.get('query')

            if not query:
                raise KeyError('datasource.query must be provided.')

            if (not config.grant_query_to_public
                    and not users.is_current_user_admin()
                    and dashboard.Dashboard.IsQueryCustom(
                        query, dashboard_id, widget_id)):
                raise SecurityError(
                    'The user is not authorized to run custom queries')

            query_config = request_data['datasource']['config']

            cache_duration = config.cache_duration or None

            response = client.Query(query, cache_duration=cache_duration)

            if query_config['results'].get('pivot'):
                pivot_config = query_config['results']['pivot_config']

                transformer = big_query_result_pivot.BigQueryPivotTransformer(
                    reply=response,
                    rows_name=pivot_config['row_field'],
                    columns_name=pivot_config['column_field'],
                    values_name=pivot_config['value_field'])
                transformer.Transform()

            response['results'] = (
                result_util.ReplyFormatter.RowsToDataTableFormat(response))

            elapsed_time = time.time() - start_time
            response['elapsedTime'] = elapsed_time
            self.RenderJson(response)

        # If 'expected' errors occur (specifically dealing with SQL problems),
        # return JSON with descriptive text so that we can give the user a
        # constructive error message.
        # TODO: Formalize error reporting/handling across the application.
        except (big_query_client.BigQueryError, ValueError, KeyError,
                SecurityError) as err:
            self.RenderJson({'error': err.message})
 def get(self):
     urlfetch.set_default_fetch_deadline(20)
     botname = self.request.get('botname').lower()
     api_url = key.DIALECT_API_URL if botname == 'dialectbot' else key.DIALETTI_API_URL
     json_response = requests.get(api_url + 'getMe').json()
     self.response.write(json.dumps(json_response))
예제 #60
0
파일: main.py 프로젝트: SalamiArmy/InfoBoet
 def get(self):
     urlfetch.set_default_fetch_deadline(10)
     self.response.write(TelegramWebhookHandler().commandCascade)
     return self.response