Skip to content

Commit

Permalink
show map animation
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzhak committed May 23, 2017
1 parent ef820a9 commit 377dd79
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
1 change: 1 addition & 0 deletions nasabot/help/help_stories.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ async def say_something(ctx):
use_aliases=True).format(**ctx['user']),
user=ctx['user']
)
logger.debug('# end of say_something')
60 changes: 45 additions & 15 deletions nasabot/query/query_stories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from botstory.ast.story_context import get_message_attachment
import emoji
import datetime
from datetime import date, timedelta
import logging
from nasabot.geo import animation, tiles
import os
from urllib.parse import urljoin
import uuid

logger = logging.getLogger(__name__)

Expand All @@ -15,7 +16,7 @@


def day_before():
return date.today() - timedelta(days=1)
return datetime.datetime.now() - datetime.timedelta(days=1)


def setup(story):
Expand Down Expand Up @@ -47,11 +48,22 @@ async def show_image(ctx, target_date, lat, long, level):
}, ],
)

async def show_animation(ctx, target_data, lat, long, level):
tile = tiles.wgs84_tile_by_coors(lat, long, level)
await story.say('Here is the last 2 weeks:',
async def show_animation(ctx, target_date, lat, long, level):
tile = tiles.mercator_tile_by_coords(lat, long, level)
await story.say('Here is the last 2 weeks...',
user=ctx['user'])
gif_filename = os.path.join(dir_path, 'tmp', 'tmp-file-name.gif')
await story.start_typing(user=ctx['user'])

gif_filename = 'animation-{}.gif'.format(uuid.uuid4())
gif_full_filename = os.path.join(os.environ.get('GENERATED_STATIC_DIR'), gif_filename)
gif_url = urljoin(os.environ.get('HOST_URL'),
os.path.join(os.environ.get('GENERATED_STATIC_PATH'), gif_filename))

logger.info('# tile')
logger.info(tile)
logger.info('# level')
logger.info(level)

await animation.pipeline(
source=animation.source.GIBSSource(
'https://gibs.earthdata.nasa.gov/wmts/{projection}/best/{layer}/default/{date}/{resolution}/{z}/{y}/{x}.jpg',
Expand All @@ -62,18 +74,35 @@ async def show_animation(ctx, target_data, lat, long, level):
**tile,
),
timeline=animation.timeline.Interval(
datetime.datetime.now() - datetime.timedelta(days=1, weeks=2),
datetime.datetime.now() - datetime.timedelta(days=1),
target_date - datetime.timedelta(weeks=2),
target_date,
),
target=animation.target.Gif(
gif_filename,
gif_full_filename,
),
)
await story.send_image(
gif_filename,
user=ctx['user'],
)
os.remove(gif_filename)
await story.say(
emoji.emojize('Processed. Now we are going to upload it :package:.'),
user=ctx['user'])
await story.start_typing(user=ctx['user'])
await story.send_image(gif_url,
user=ctx['user'])
await story.stop_typing(user=ctx['user'])

# show static image
#
# await story.send_image(
# satellite_image_epsg3857.format(
# # satellite_image_epsg4326.format(
# **tile,
# date=target_date.isoformat(),
# z=level,
# ),
# user=ctx['user'],
# )
await story.say('What is next?',
user=ctx['user'])
os.remove(gif_full_filename)

@story.on(text.EqualCaseIgnore('earth'))
def handle_random_location():
Expand Down Expand Up @@ -104,6 +133,7 @@ async def show_asia(ctx):
def handle_list_of_coords():
@story.part()
async def use_passed_coords_to_show_earth(ctx):
logger.info('# use_passed_coords_to_show_earth')
raw_text = text.get_raw_text(ctx)
values = raw_text.split(',')
if len(values) < 2 or len(values) > 4:
Expand All @@ -116,7 +146,7 @@ async def use_passed_coords_to_show_earth(ctx):
else:
zoom = 6

await show_image(ctx, day_before(), lat, long, zoom)
await show_animation(ctx, day_before(), lat, long, zoom)

@story.on(location.Any())
def handle_location():
Expand Down
16 changes: 14 additions & 2 deletions nginx/sites-enabled/bot.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
server {
listen 80;

# serve static files
location /generated_static/ {
alias /usr/src/generated_static/;
expires 30d;
}

# bot hooks
location / {
root /usr/src/generated_static;
expires 30d;
proxy_pass http://bot:8081;

# include proxy_params;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

0 comments on commit 377dd79

Please sign in to comment.