-
Notifications
You must be signed in to change notification settings - Fork 1
Convert datetimes to UTC #21
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
eefc478
Convert Query#start_time and #end_time to UTC
roback 7179610
Remove duplicated code
roback e6fac64
Include timezone in Post#published and #indexed
roback 3251010
Always use #_parse_time helper function in Post
roback 484add6
Set timezone in examples
roback 868a5c8
Assume UTC if no timezone is set
roback 145a902
Fix typo in readme
roback 415c6bf
No need to duplicate requirements
roback 98bcf5e
Add tests with time parsed by dateutil
roback cdecac5
Add example that uses local timezone
roback 2596d04
Merge branch 'master' into utc
dentarg 13bef57
No need to repeat what's in requirements.txt
dentarg 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,59 @@ | ||
| import twingly_search | ||
| import datetime | ||
| import dateutil.parser | ||
| from dateutil.tz import * | ||
|
|
||
| """ | ||
| Simple cli that lets you search in Twingly Search API using your local timezone | ||
|
|
||
| Example run: | ||
| python examples/search_using_local_timezone.py | ||
| > What do you want to search for? cupcakes | ||
| > Start time (in CET): 2016-02-01 00:00:00 | ||
| > End time (in CET): 2016-02-10 00:00:00 | ||
| Search results --------------------- | ||
| Published (in CET) - Post URL | ||
| 2016-02-09 21:38:09 - http://www.heydonna.com/2016/02/smores-cupcakes | ||
| 2016-02-09 20:21:36 - http://www.attwentynine.com/2016/02/09/chocolate-cake-cupcake | ||
| ... | ||
| """ | ||
| class SimpleSearchCli(object): | ||
| CURRENT_TIMEZONE = tzlocal() | ||
| CURRENT_TIMEZONE_NAME = datetime.datetime.now(CURRENT_TIMEZONE).tzname() | ||
|
|
||
| def __init__(self): | ||
| self.client = twingly_search.Client() | ||
|
|
||
| def start(self): | ||
| query = self.client.query() | ||
|
|
||
| # See https://developer.twingly.com/resources/search-language/ | ||
| query.pattern = raw_input("What do you want to search for? ") | ||
| query.start_time = self._read_time_from_stdin("Start time") | ||
| query.end_time = self._read_time_from_stdin("End time") | ||
|
|
||
| results = query.execute() | ||
|
|
||
| self._print_results(results) | ||
|
|
||
| def _read_time_from_stdin(self, time_label): | ||
| prompt = "%s (in %s): " % (time_label, self.CURRENT_TIMEZONE_NAME) | ||
|
|
||
| parsed_time = dateutil.parser.parse(raw_input(prompt)) | ||
| # Sets your local timezone on the parsed time object. | ||
| # If no timezone is set, twingly_search assumes UTC. | ||
| return parsed_time.replace(tzinfo=self.CURRENT_TIMEZONE) | ||
|
|
||
| def _print_results(self, result): | ||
| print "Search results ---------------------" | ||
| print "Published (in %s) - Post URL" % self.CURRENT_TIMEZONE_NAME | ||
|
|
||
| for post in result.posts: | ||
| # The time returned from the API is always in UTC, | ||
| # convert it to your local timezone before displaying it. | ||
| local_datetime = post.published.astimezone(self.CURRENT_TIMEZONE) | ||
| published_date_string = local_datetime.strftime("%Y-%m-%d %H:%M:%S") | ||
|
|
||
| print "%s - %s" % (published_date_string, post.url) | ||
|
|
||
| SimpleSearchCli().start() |
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| future | ||
| requests | ||
| pytz | ||
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| future | ||
| requests | ||
| -r requirements.txt | ||
|
|
||
| nose | ||
| betamax | ||
| python-dateutil |
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
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.
we also list requirements in the README, but don't I think we need to do that when we have this file
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.
Yes, I think that requests should be removed from the readme instead :)
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.
Done in 13bef57
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.
👍