Example #1
0
def send(arg):
		base_send = APIBASE + "send"

		letters = None
		numbers = 0
		timestamp = 0
		message = arg[0]
		
		if len(arg) > 1:
			if len(arg[1]) == 5:
				letters, numbers = get_location(arg[1])
				if len(arg) == 3:
					timestamp = int(arg[2])
			else:
				timestamp = int(arg[1])
				if len(arg) == 3:
					letters, numbers = get_location(arg[2])
					
		payload = {'message': message, 'location_letters': letters if letters != None else "", 'location_numbers': numbers, 'timestamp': timestamp}
		response = requests.post(base_send, data=payload)
		
		if response.status_code != 200:
			request_error(response.status_code, response.text)
			return
			
		print "Sent OK!" 
 def get(self):
     # Scan through members, updating each one.  Each request picks up
     # where the last one left off, and processes the next 100 members.
     last_key = db.Key(self.request.get('last_key', MIN_KEY))
     batch = model.Member.all().filter('__key__ >', last_key).fetch(100)
     now = datetime.datetime.utcnow()
     for member in batch:
         member = member.clean(now)
         if member.tags:
             loc = utils.get_location(member)
             l = model.Location(location=loc, user=member.user,
                 location_time=now, tags=member.tags)
             l.put()
             member.set_location(utils.get_location(member), now)
             logging.info('updated member: ' + member.user.email())
     if batch:
         # Schedule a task to continue processing the next batch.
         taskqueue.add(method='GET', url='/_update_members',
                       params={'last_key': str(batch[-1].key())})
Example #3
0
def read_dataset_eartquakes(
        fname='greek-earthquakes-1964-2000-with-times.csv'):
    data_dir = os.path.join(get_location(), 'data')
    print(data_dir)
    DATASET_FILE = os.path.join(data_dir, fname)
    data = pd.read_csv(DATASET_FILE, delimiter='\t', parse_dates=["DATETIME"])
    # avoid datetime less 1970 year
    data['DATETIME'] = data['DATETIME'].apply(
        lambda x: x + pd.DateOffset(years=10))
    data['TIMESTAMP'] = data['DATETIME'].values.astype(np.int64) // 10**9
    from datetime import datetime
    print(datetime.utcfromtimestamp(data['TIMESTAMP'].values[0]))
    return data
Example #4
0
 def is_going_to_set_location(self, event):
     text = event.message.text
     location = get_location()
     for i in range(len(location)):
         print("Location[",i,"] is ",location[i])
         if location[i].find(text) != -1:
             global default_ID
             default_ID = i
             print("default_ID = ",default_ID)
             global loc
             loc = location[i]
             return True
     return False
Example #5
0
 def obtain_data_set(self, key):
     self.locations = utils.get_location(key)
     raw_pattern = self.dao.get_last_value(key)
     patterns = utils.get_pattern(raw_pattern)
     data_sets = self.dao.get_data_by_pattern(key, *patterns)
     data_period_set = self.dao.get_data_by_range(key, [-6, -2])
     get_period_value = partial(utils.get_value, self.locations)
     data_period_set_modify = map(get_period_value, data_period_set)
     data_sets.append(data_period_set_modify)
     self.data_sets = data_sets
     self.raw_value = raw_pattern
     if utils.check_list_null(self.data_sets):
         logging.error('please check key:' + key)
     return self
Example #6
0
 def get_data_by_pattern(self, raw_key, *patterns):
     '''
     返回指定key符合指定pattern的值
     :param key:
     :param pattern:
     :return:
     '''
     data_set = self.get_all(raw_key)
     location = utils.get_location(raw_key)
     result = []
     for date_pattern in patterns:
         data_set_accord_date_pattern = utils.filte_by_pattern(date_pattern, data_set)
         get_value_accord_location = partial(utils.get_value, location)
         tmp = map(get_value_accord_location, data_set_accord_date_pattern)
         result.append(tmp)
     return result
Example #7
0
 def is_going_to_location(self, event):
     text = event.message.text
     if text.lower() == 'y':
         global using_default
         using_default = 1        
         return True
     else:
         text = text.replace("台","臺")
         text = text[0:2]
         location = get_location()
         for i in range(len(location)):
             print("Location[",i,"] is ",location[i])
             if location[i].find(text) != -1:
                 global ID
                 ID = i
                 print("ID = ",ID)
                 return True
         return False
Example #8
0
    def get(self, tag):
        # To allow a join action to redirect to OAuth and smoothly redirect
        # back here to finish the action, the join action is a GET request.
        if self.request.get('join'):
            # Add a tag to the member, and also update the member's location.
            self.require_member()
            self.verify_signature()
            now = datetime.datetime.utcnow()
            duration = int(self.request.get('duration'))
            stop_time = now + datetime.timedelta(0, duration)
            self.member.set_location(utils.get_location(self.member), now)
            model.Member.join(self.user, tag, stop_time)
            # Ensure that a TagStat entity exists, to be later updated.
            model.TagStat.get_or_insert(key_name=tag)
            # Schedule a task to update the stats now.
            taskqueue.add(
                method='GET', url='/_update_tagstats', params={'tag': tag})
            # Schedule a task to promptly update the stats upon expiry.
            taskqueue.add(
                method='GET', countdown=duration + 1,
                url='/_update_tagstats', params={'tag': tag})
            # Redirect to avoid adding the join action to the browser history.
            raise utils.Redirect('/' + tag)

        # Generate the tag viewing page.
        now = datetime.datetime.utcnow()
        user_id = self.user and self.user.user_id()
        join_time = ''
        members = []
        for member in model.Member.get_for_tag(tag, now):
            if member.location:
                if member.user.user_id() == user_id:
                    join_time = utils.describe_delta(
                        member.get_stop_time(tag) - now)
                else:
                    members.append(self.get_member_info(member))
        members.sort(key=lambda m: (m.get('distance', 0), m['nickname']))
        if join_time:
            members.insert(0, self.get_member_info(self.member))
            members[0]['is_self'] = 1
        if self.user:
            self.set_signature()  # to prevent XSRF
        self.render('templates/tag.html', vars=self.vars, tag=tag,
                    join_time=join_time, members=simplejson.dumps(members))
Example #9
0
    def get(self):
        self.require_user()
        next = self.request.get('next', '')
        access_token = oauth_webapp.handle_authorization_finished(
            self, latitude.LatitudeOAuthClient(utils.oauth_consumer))

        # Store a new Member object, including the user's current location.
        member = model.Member.create(self.user)
        member.nickname = self.request.get('nickname')
        member.latitude_key = access_token.key
        member.latitude_secret = access_token.secret
        member.location = utils.get_location(member)
        member.location_time = datetime.datetime.utcnow()
        if not member.location:
            raise utils.ErrorMessage(400, '''
Sorry, Google Latitude has no current location for you.
''')
        member.put()
        raise utils.Redirect(next or '/')
Example #10
0
	def messages(arg):
		def print_data(url):
			data = requests.get(url)
			if data.status_code != 200:
				request_error(data.status_code, data.text)

			data = json.loads(data.text)
			message_printer(data)
	
		additional_arguments =	{
									'timestamp': 'has_timestamp',
									'location': 'has_location',
									'trending': 'trending',
									'random': 'random'
								}
		local_args = ""
	
		base_messages = APIBASE + 'messages'
	
		if len(arg) == 0:
			print_data(base_messages)
		else:
			if len(arg[0]) == 5:
				letters, numbers = get_location(arg[0])
				
				discriminator = "/" + letters + "-" + numbers
				
				if len(arg[1:]) == 1:
					if arg[1] in additional_arguments.keys():
						local_args = "/" + additional_arguments[arg[1]]
					else:
						print "Unrecognized modifier. Available arguments: "
						print ', '.join(additional_arguments)
						return
				elif len(arg[1:]) > 1:
					print "You can't do that."
				
				
				full_url = base_messages + discriminator + local_args
				print_data(full_url)
			elif arg[0] in additional_arguments.keys():
				local_args = "/" + additional_arguments[arg[0]]
				
				full_url = base_messages + local_args
				print_data(full_url)
			
			elif arg[0] == "near":
				base_near = base_messages + "/near/"
				
				if len(arg[1]) == 5:
					letters, numbers = get_location(arg[1])
					radius = 0
					
					if len(arg) == 3:
						radius = int(arg[2])
					
					full_url = base_near + letters + "-" + numbers + ("/" + str(radius) if radius > 0 else "")
					print_data(full_url)
					
				else:
					print "You must specify a location."
				
			
			else:
				print "I'm sorry Bill, I'm afraid I can't let you do that."
def get_single_lab(lab_slug, open_cage_api_key):
    """Gets data from a single lab from hackerspaces.org."""
    wiki = MediaWiki(hackerspaces_org_api_url)
    wiki_response = wiki.call({
        'action': 'query',
        'titles': lab_slug,
        'prop': 'revisions',
        'rvprop': 'content'
    })

    # If we don't know the pageid...
    for i in wiki_response["query"]["pages"]:
        content = wiki_response["query"]["pages"][i]["revisions"][0]["*"]

    # Transform the data into a Lab object
    current_lab = Hackerspace()

    equipment_list = []

    # Parse the Mediawiki code
    wikicode = mwparserfromhell.parse(content)
    for k in wikicode.filter_templates():
        element_name = unicode(k.name)
        if "Hackerspace" in element_name:
            for j in k.params:
                current_lab.name = lab_slug
                j_value = unicode(j.value)
                j_name = unicode(j.name)
                # Remove new line in content
                if j_value[-1:] == "\n" or j_value[:1] == "\n":
                    j_value = j_value.replace('\n', '')
                if j_name == "logo":
                    current_lab.logo = j_value
                if j_name == "founding":
                    current_lab.founding = j_value
                if j_name == "coordinate":
                    # Clean the coordinates
                    j_value = j_value.replace('"', '')
                    j_value = j_value.replace('N', '')
                    j_value = j_value.replace('S', '')
                    j_value = j_value.replace('W', '')
                    j_value = j_value.replace('E', '')
                    j_value = j_value.replace(u'°', '')
                    j_value = j_value.replace(' ', '')
                    # Get the full address with the coordinates
                    address = get_location(query=j_value,
                                           format="reverse",
                                           api_key=open_cage_api_key)
                    current_lab.city = address["city"]
                    current_lab.county = address["county"]
                    current_lab.state = address["state"]
                    current_lab.postal_code = address["postal_code"]
                    current_lab.address_1 = address["address_1"]
                    current_lab.country = address["country"]
                    current_lab.country_code = address["country_code"]
                    current_lab.continent = address["continent"]
                    current_lab.latitude = address["latitude"]
                    current_lab.longitude = address["longitude"]
                if j_name == "membercount":
                    current_lab.membercount = j_value
                if j_name == "fee":
                    current_lab.fee = j_value
                if j_name == "size":
                    current_lab.size = j_value
                if j_name == "status":
                    current_lab.status = j_value
                if j_name == "site":
                    current_lab.site = j_value
                if j_name == "wiki":
                    current_lab.wiki = j_value
                if j_name == "irc":
                    current_lab.irc = j_value
                if j_name == "jabber":
                    current_lab.jabber = j_value
                if j_name == "phone":
                    current_lab.phone = j_value
                if j_name == "youtube":
                    current_lab.youtube = j_value
                if j_name == "eventbrite":
                    current_lab.eventbrite = j_value
                if j_name == "facebook":
                    current_lab.facebook = j_value
                if j_name == "ustream":
                    current_lab.ustream = j_value
                if j_name == "flickr":
                    current_lab.flickr = j_value
                if j_name == "twitter":
                    current_lab.twitter = j_value
                if j_name == "googleplus":
                    current_lab.googleplus = j_value
                if j_name == "email":
                    current_lab.email = j_value
                if j_name == "maillist":
                    current_lab.maillist = j_value
                if j_name == "ical":
                    current_lab.ical = j_value
                if j_name == "forum":
                    current_lab.forum = j_value
        elif "Equipment" in element_name:
            for j in k.params:
                equipment_list.append(j.replace("equipment=", ""))

            current_lab.equipment = equipment_list

    # Load the free text
    freetext = ""
    for k in wikicode._nodes:
        try:
            test_value = k.name
        except AttributeError:
            freetext += unicode(k)
    current_lab.text = freetext

    return current_lab
Example #12
0
def get_labs(format, open_cage_api_key):
    """Gets DIYBio Lab data from diybio.org."""

    diybiolabs_soup = data_from_diybio_org()
    diybiolabs = {}

    rows_list = []
    continents_dict = {}
    continents_order = 0
    ranges_starting_points = []

    # Load all the DIYBio Labs
    # By first parsing the html

    # Parse table rows
    for row in diybiolabs_soup.select("table tr"):
        cells = row.find_all('td')
        rows_list.append(cells)

    # Find the continents in order to iterate over their children td
    for k, row in enumerate(rows_list):
        for col in row:
            if col.find('h3'):
                for h3 in col.findAll('h3'):
                    ranges_starting_points.append(k)
                    continents_dict[continents_order] = h3.get_text()
                    continents_order += 1

    # Find the rows of each continent
    ranges = {}
    for k, j in enumerate(reversed(ranges_starting_points)):
        if k < len(ranges_starting_points) - 1:
            ranges[k] = {
                "start": ranges_starting_points[k],
                "end": ranges_starting_points[k + 1]
            }
        else:
            # The last continent, Oceania
            ranges[k] = {
                "start": ranges_starting_points[k],
                "end": len(rows_list)
            }

    # Iterate over the range of each continent to find the Labs
    for i in ranges:
        # The +1 just avoids the H3 line
        for j in range(ranges[i]["start"] + 1, ranges[i]["end"]):
            # Avoid empty rows by measuring the lenght of the content of each cell and with a boolean check
            rules = [len(n) == 0 for n in rows_list[j]]
            if False in rules:
                current_lab = DiyBioLab()
                current_lab.city = rows_list[j][1].contents[0].encode('utf-8')
                # Data from the USA is not really well formatted
                if continents_dict[i] == "USA-EAST" or continents_dict[
                        i] == "USA-WEST":
                    current_lab.state = rows_list[j][2].contents[0].replace(
                        " ", "").encode('utf-8')
                else:
                    current_lab.country_code = rows_list[j][2].contents[
                        0].encode('utf-8')
                current_lab.url = rows_list[j][3].contents[0].attrs['href']
                # Each lab is identified by the simplified url
                slug = current_lab.url
                if "http://" in slug:
                    slug = slug.replace("http://", "")
                elif "https://" in slug:
                    slug = slug.replace("https://", "")
                if "www." in slug:
                    slug = slug.replace("www.", "")
                current_lab.name = slug
                current_lab.slug = slug

                # Data from the USA is not really well formatted
                if continents_dict[i] == "USA-EAST" or continents_dict[
                        i] == "USA-WEST":
                    current_lab.continent = "North America"
                    current_lab.country_code = "USA"
                    current_lab.country = "United States of America"
                    current_lab.state = us.states.lookup(
                        current_lab.state).name

                # Get address from city
                address = get_location(query=current_lab.city,
                                       format="direct",
                                       api_key=open_cage_api_key)
                current_lab.continent = address["continent"]
                current_lab.latitude = address["latitude"]
                current_lab.longitude = address["longitude"]
                current_lab.address_1 = address["address_1"]
                current_lab.country = address["country"]
                current_lab.country_code = address["country_code"]
                current_lab.latitude = address["latitude"]
                current_lab.longitude = address["longitude"]
                current_lab.county = address["county"]
                current_lab.postal_code = address["postal_code"]
                current_lab.state = address["state"]

                # Add the lab to the list
                diybiolabs[slug] = current_lab
                del current_lab

    # Return a dictionary / json
    if format.lower() == "dict":
        output = {}
        for j in diybiolabs:
            output[j] = diybiolabs[j].__dict__
    # Return a geojson
    elif format.lower() == "geojson" or format.lower() == "geo":
        labs_list = []
        for l in diybiolabs:
            single = diybiolabs[l].__dict__
            single_lab = Feature(type="Feature",
                                 geometry=Point((single["latitude"],
                                                 single["longitude"])),
                                 properties=single)
            labs_list.append(single_lab)
        output = dumps(FeatureCollection(labs_list))
    # Return a Pandas DataFrame
    elif format.lower() == "pandas" or format.lower() == "dataframe":
        output = {}
        for j in diybiolabs:
            output[j] = diybiolabs[j].__dict__
        # Transform the dict into a Pandas DataFrame
        output = pd.DataFrame.from_dict(output)
        # Put labs names as the index, to make it coherent with other APIs
        output = output.transpose()
    # Return an object
    elif format.lower() == "object" or format.lower() == "obj":
        output = diybiolabs
    # Default: return an oject
    else:
        output = diybiolabs
    # Return a proper json
    if format.lower() == "json":
        output = json.dumps(diybiolabs)
    return output