-
Notifications
You must be signed in to change notification settings - Fork 1
Cityinfo #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Najipian
wants to merge
26
commits into
camelcasetechsd:master
Choose a base branch
from
Najipian:cityinfo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Cityinfo #5
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
98c4631
show logged user in home template
Najipian 7087c1b
miss spelling corrected
Najipian afe6343
Merge branch 'master' of https://github.com/camelcasetechsd/appengine…
Najipian bb19618
twice square ta=raining task
Najipian c968b87
http training task
Najipian 86eecde
correct permissions
Najipian f6f4bb2
fix reviews
Najipian 32d49ae
fix reviews
Najipian f844a4b
fix reviews
Najipian 37eb5cf
Datastore training
Najipian aafb430
fix reviews
Najipian 0a0a5a1
Merge branch 'test' into datastore-training
Najipian 59e138c
resolve conflect
Najipian 00941a6
Merge branch 'master' of https://github.com/camelcasetechsd/appengine…
Najipian 6c76bbf
create files and modules
Najipian da396b4
create pipelines
Najipian b42d97b
get city info
Najipian 19f1118
get city temp and store data
Najipian 79c4051
seperate functions
Najipian 38dd837
cityinfo view end point
Najipian e77e4d4
cityinfo view end point
Najipian f1fa563
cron job
Najipian e72b42e
city view link , deploy cron file
Najipian 7c4b827
review files
Najipian b18791f
review files
Najipian 78a04f4
cityinfo reviews
Najipian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # __init__.py needed to create "package" for this directory |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import requests | ||
| import logging | ||
| import os | ||
| from google.appengine.ext import ndb | ||
| from requests_toolbelt.adapters import appengine | ||
| from app.modules.common.kinds import CityInfo | ||
|
|
||
| # https://toolbelt.readthedocs.io/en/latest/adapters.html#appengineadapter | ||
| appengine.monkeypatch(validate_certificate=False) | ||
|
|
||
| def CityWikiInfo(lat , lon): | ||
| # wikipedia base api url | ||
| API_HOST = "https://en.wikipedia.org/w/api.php?" | ||
|
|
||
| # add given lat long to api url | ||
| apiUrl = "%sformat=json&action=query&prop=extracts&exintro=1&explaintext=1&exlimit=20&generator=geosearch&ggsradius=10000&ggslimit=100&ggscoord=%s%s%s" % (API_HOST, lat,"|", lon) | ||
|
|
||
| logging.info(apiUrl) | ||
|
|
||
| template_vars = {} | ||
|
|
||
| try: | ||
| # make the api call | ||
| request = requests.get(apiUrl) | ||
| data = request.json() | ||
|
|
||
| # check if there is data returned | ||
| if 'query' in data : | ||
|
|
||
| for idx , page in data['query']['pages'].items(): | ||
| # get the 0 indexd page (first page data) | ||
| if page['index'] == 0: | ||
| return page['extract'] | ||
|
|
||
| else: | ||
| # if there is no data returned show user | ||
| logging.error(e) | ||
| except requests.exceptions.RequestException as e: | ||
| logging.error(e) | ||
|
|
||
| return None | ||
|
|
||
| def CityWeatherTemp(lat , lon): | ||
| API_HOST = "http://api.openweathermap.org" | ||
| API_KEY = os.environ.get("HTTP_EXAMPLE_API_KEY") | ||
|
|
||
| if API_KEY: | ||
| try: | ||
| apiUrl = "%s/data/2.5/forecast?appid=%s&mode=json&units=metric&lat=%s&lon=%s" % (API_HOST, API_KEY, lat, lon) | ||
| request = requests.get(apiUrl) | ||
| data = request.json() | ||
| return data['list'][0]['main']['temp'] | ||
| except requests.exceptions.RequestException as e: | ||
| logging.error(e) | ||
|
|
||
| return None | ||
|
|
||
| def StoreCitiesInfo(cities): | ||
| for city in cities: | ||
| entity_key = ndb.Key('CityInfo', city['Location']) | ||
| entity = entity_key.get() | ||
|
|
||
| if entity is None: | ||
| entity = CityInfo( | ||
| Location=city['Location'], | ||
| Info=city['Info'], | ||
| Temp=city['Temp'] | ||
| ) | ||
| entity.key = ndb.Key('CityInfo', city['Location']) | ||
| entity.put() | ||
|
|
||
| else: | ||
| entity.Info = city['Info'] | ||
| entity.Temp = city['Temp'] | ||
| entity.put() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <html> | ||
| <body> | ||
| <h1>Cities Information</h1> | ||
| <dl> | ||
| {% for city in cities %} | ||
| <dt><p>{{ city.Location }}</p></dt> | ||
| <dd><p>Temp : {{ city.Temp }}</p></dd> | ||
| <dd><p>Last Updated : {{ city.LastUpdated }}</p></dd> | ||
| <dd><p>Info : {{ city.Info }}</p></dd> | ||
| {% endfor %} | ||
| {% if not cities %} | ||
| No cities found | ||
| {% endif %} | ||
| </dl> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import webapp2 | ||
| import logging | ||
| import os | ||
| from google.appengine.ext.webapp import template | ||
| from app.modules.common.city_info import CityInfoRootPipeline | ||
| from app.modules.common.kinds import CityInfo | ||
|
|
||
| class CityInfoBuildHandler(webapp2.RequestHandler): | ||
| def get(self): | ||
| logging.info("CityInfoBuildHandler") | ||
| CitiesUpdate = CityInfoRootPipeline() | ||
| CitiesUpdate.start() | ||
|
|
||
| class CityInfoViewHandler(webapp2.RequestHandler): | ||
| def get(self): | ||
| logging.info("CityInfoViewHandler") | ||
|
|
||
| # get cities | ||
| cities = CityInfo.query().fetch(20) | ||
|
|
||
| template_path = os.path.join(os.path.dirname(__file__), 'city_info.html') | ||
| self.response.write(template.render(template_path, { | ||
| 'cities': cities, | ||
| })) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| cities: | ||
| - | ||
| name: London | ||
| lat: 51.507222 | ||
| lon: -0.1275 | ||
| - | ||
| name: Cairo | ||
| lat: 30.044444 | ||
| lon: 31.235833 | ||
| - | ||
| name: Beijing | ||
| lat: 39.916667 | ||
| lon: 116.383333 | ||
|
|
||
| - | ||
| name: Paris | ||
| lat: 48.8588376 | ||
| lon: 2.2768489 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import webapp2 | ||
| import logging | ||
| import pipeline | ||
| import app.modules.cityinfo.city_extras as CityExtras | ||
| import app.modules.common.util as Utils | ||
|
|
||
| class CityInfoRootPipeline(pipeline.Pipeline): | ||
|
|
||
| def run(self): | ||
| logging.info("CityInfoRootPipeline") | ||
|
|
||
| # Read cityinfo.yaml file | ||
| data = Utils.ReadYamlFile('../cityinfo/cityinfo.yaml') | ||
| cities = [] | ||
|
|
||
| for city in data['cities'] : | ||
| cityInfo = yield CityInfoFetchPipeline(city) | ||
| cities.append(cityInfo) | ||
|
|
||
| yield CityInfoPersistPipeline(*cities) | ||
|
|
||
| class CityInfoFetchPipeline(pipeline.Pipeline): | ||
|
|
||
| def run(self, city): | ||
| logging.info("CityInfoFetchPipeline") | ||
|
|
||
| cityinfo = yield CityInfoInfoPipeline(city['lat'] , city['lon'] ) | ||
| citytemp = yield CityInfoWeatherPipeline(city['lat'] , city['lon']) | ||
|
|
||
| yield CityInfoReturn(city['name'], cityinfo,citytemp) | ||
|
|
||
| class CityInfoInfoPipeline(pipeline.Pipeline): | ||
|
|
||
| def run(self, lat , lon): | ||
| logging.info("CityInfoInfoPipeline") | ||
| return CityExtras.CityWikiInfo(lat , lon) | ||
|
|
||
| class CityInfoWeatherPipeline(pipeline.Pipeline): | ||
|
|
||
| def run(self , lat , lon): | ||
| logging.info("CityInfoWeatherPipeline") | ||
| return CityExtras.CityWeatherTemp(lat , lon) | ||
|
|
||
| class CityInfoPersistPipeline(pipeline.Pipeline): | ||
|
|
||
| def run(self, *args): | ||
| logging.info("CityInfoPersistPipeline") | ||
|
|
||
| CityExtras.StoreCitiesInfo(args) | ||
|
|
||
| class CityInfoReturn(pipeline.Pipeline): | ||
|
|
||
| def run(self, cityname,cityInfo , cityTemp): | ||
| logging.info("CityInfoReturn") | ||
|
|
||
| return {'Location' : cityname ,'Info' : cityInfo , 'Temp' : cityTemp } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import webapp2 | ||
| import logging | ||
| from google.appengine.ext import ndb | ||
|
|
||
| class Example(ndb.Model): | ||
| value = ndb.StringProperty() | ||
|
|
||
| class CityInfo(ndb.Model): | ||
| Location = ndb.StringProperty() | ||
| Info = ndb.TextProperty() | ||
| Temp = ndb.FloatProperty() | ||
| LastUpdated = ndb.DateTimeProperty(auto_now=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import os | ||
| import yaml | ||
|
|
||
| def ReadYamlFile(fileLocation): | ||
| data = {} | ||
| yaml_path = os.path.join(os.path.dirname(__file__), fileLocation) | ||
| with open(yaml_path, 'r') as stream: | ||
| data = yaml.load(stream) | ||
|
|
||
| return data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <html> | ||
| <body> | ||
| <h1>Datastore Training</h1> | ||
| <dl> | ||
| {% for entity in entities %} | ||
| <dt><p>{{ entity.key }}</p></dt> | ||
| <dd><p>{{ entity.value }}</p></dd> | ||
| {% endfor %} | ||
| </dl> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import webapp2 | ||
| import logging | ||
| import os | ||
| from google.appengine.ext import ndb | ||
| from google.appengine.ext.webapp import template | ||
| from app.modules.common.kinds import Example | ||
|
|
||
| class DatastoreTrainingHandler(webapp2.RequestHandler): | ||
| def get(self): | ||
| logging.info("DatastoreTrainingHandler") | ||
|
|
||
| # get entities | ||
| entities_for_example_kind = Example.query().fetch(20) | ||
|
|
||
| # make a list of key value pairs | ||
| entities = [] | ||
| for entity in entities_for_example_kind: | ||
| entities.append({'key':entity.key.string_id() , 'value':entity.value}) | ||
|
|
||
| template_path = os.path.join(os.path.dirname(__file__), 'datastore_training.html') | ||
| self.response.write(template.render(template_path, { | ||
| 'entities': entities, | ||
| })) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| cron: | ||
| - description: "City info build hourly" | ||
| url: /cityinfo/build | ||
| schedule: every 1 hours |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a condition to be displayed if we don't have data on any cities.