Merge lp:~stefanor/ibid/twitter-timestamp-702815-0.1 into lp:~ibid-core/ibid/old-release-0.1-1.6

Proposed by Stefano Rivera
Status: Merged
Approved by: Stefano Rivera
Approved revision: 968
Merged at revision: 972
Proposed branch: lp:~stefanor/ibid/twitter-timestamp-702815-0.1
Merge into: lp:~ibid-core/ibid/old-release-0.1-1.6
Diff against target: 145 lines (+31/-13)
5 files modified
COPYING (+2/-2)
docs/api/ibid.utils.rst (+9/-0)
docs/conf.py (+1/-1)
ibid/plugins/social.py (+9/-9)
ibid/utils/__init__.py (+10/-1)
To merge this branch: bzr merge lp:~stefanor/ibid/twitter-timestamp-702815-0.1
Reviewer Review Type Date Requested Status
marcog (community) Approve
Max Rabkin Approve
Review via email: mp+46373@code.launchpad.net

Commit message

Add parse_timestamp function to ibid.utils, use for parsing timestamps from Twitter.

To post a comment you must log in.
967. By Stefano Rivera

Bump copyright years

Revision history for this message
Max Rabkin (max-rabkin) :
review: Approve
968. By Stefano Rivera

Clarify that parse_timestamp is for machine timestamps

Revision history for this message
marcog (marco-gallotta) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'COPYING'
2--- COPYING 2010-01-19 11:41:20 +0000
3+++ COPYING 2011-01-21 21:23:42 +0000
4@@ -22,7 +22,7 @@
5 Ibid Core Licence:
6 ==================
7
8-Copyright © 2008-2010: Jonathan Hitchcock, Michael Gorven, Stefano Rivera and
9+Copyright © 2008-2011: Jonathan Hitchcock, Michael Gorven, Stefano Rivera and
10 other contributors.
11 All rights reserved.
12
13@@ -48,7 +48,7 @@
14 Ibid Package License:
15 =====================
16
17-Copyright © 2008-2010: The Ibid developers and contributors.
18+Copyright © 2008-2011: The Ibid developers and contributors.
19
20 This program is free software: you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22
23=== modified file 'docs/api/ibid.utils.rst'
24--- docs/api/ibid.utils.rst 2011-01-12 14:16:31 +0000
25+++ docs/api/ibid.utils.rst 2011-01-21 21:23:42 +0000
26@@ -46,6 +46,15 @@
27 >>> format_date(datetime.utcnow())
28 u'2009-12-14 12:41:55 SAST'
29
30+.. function:: parse_timestamp(timestamp)
31+
32+ Parse string *timestamp*, convert it to UTC, and strip the timezone.
33+
34+ This function is good at parsing machine timestamps, but can't handle
35+ "human" times very well. (It uses :mod:`dateutil.parser`)
36+
37+ Return a naive :class:`datetime.datetime`.
38+
39 .. function:: human_join(items, [separator=u',', conjunction=u'and'])
40
41 Turn iterable *items* into a unicode list in the format ``a, b, c
42
43=== modified file 'docs/conf.py'
44--- docs/conf.py 2010-03-09 20:44:39 +0000
45+++ docs/conf.py 2011-01-21 21:23:42 +0000
46@@ -38,7 +38,7 @@
47
48 # General information about the project.
49 project = u'Ibid'
50-copyright = u'2008-2010, The Ibid Developers'
51+copyright = u'2008-2011, The Ibid Developers'
52
53 # The version info for the project you're documenting, acts as replacement for
54 # |version| and |release|, also used in various other places throughout the
55
56=== modified file 'ibid/plugins/social.py'
57--- ibid/plugins/social.py 2011-01-19 19:22:40 +0000
58+++ ibid/plugins/social.py 2011-01-21 21:23:42 +0000
59@@ -1,4 +1,4 @@
60-# Copyright (c) 2008-2010, Michael Gorven, Stefano Rivera, Jonathan Hitchcock
61+# Copyright (c) 2008-2011, Michael Gorven, Stefano Rivera, Jonathan Hitchcock
62 # Released under terms of the MIT/X/Expat Licence. See COPYING for details.
63
64 from urllib2 import HTTPError
65@@ -7,13 +7,14 @@
66 import re
67 import logging
68
69+import dateutil.parser
70 import feedparser
71
72-from ibid.compat import dt_strptime, ElementTree
73+from ibid.compat import ElementTree
74 from ibid.config import DictOption
75 from ibid.plugins import Processor, match, handler
76 from ibid.utils import ago, decode_htmlentities, generic_webservice, \
77- json_webservice
78+ json_webservice, parse_timestamp
79
80 log = logging.getLogger('plugins.social')
81 features = {}
82@@ -35,7 +36,7 @@
83 else:
84 event.addresponse(u', '.join(u'%s (%s ago)' % (
85 e.title,
86- ago(event.time - dt_strptime(e.updated, '%a, %d %b %Y %H:%M:%S +0000'), 1)
87+ ago(event.time - parse_timestamp(e.updated))
88 ) for e in songs['entries']))
89
90 features['microblog'] = {
91@@ -84,9 +85,8 @@
92 return {
93 'text': latest.findtext('{http://www.w3.org/2005/Atom}content')
94 .split(': ', 1)[1],
95- 'ago': ago(datetime.utcnow() - dt_strptime(
96- latest.findtext('{http://www.w3.org/2005/Atom}published'),
97- '%Y-%m-%dT%H:%M:%S+00:00')),
98+ 'ago': ago(datetime.utcnow() - parse_timestamp(
99+ latest.findtext('{http://www.w3.org/2005/Atom}published'))),
100 'url': [x for x
101 in latest.getiterator('{http://www.w3.org/2005/Atom}link')
102 if x.get('type') == 'text/html'
103@@ -104,8 +104,8 @@
104
105 return {
106 'text': decode_htmlentities(latest['text']),
107- 'ago': ago(datetime.utcnow() - dt_strptime(latest['created_at'],
108- '%a %b %d %H:%M:%S +0000 %Y')),
109+ 'ago': ago(datetime.utcnow()
110+ - parse_timestmap(latest['created_at'])),
111 'url': url,
112 }
113
114
115=== modified file 'ibid/utils/__init__.py'
116--- ibid/utils/__init__.py 2011-01-12 14:16:31 +0000
117+++ ibid/utils/__init__.py 2011-01-21 21:23:42 +0000
118@@ -1,4 +1,4 @@
119-# Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera
120+# Copyright (c) 2009-2011, Michael Gorven, Stefano Rivera
121 # Released under terms of the MIT/X/Expat Licence. See COPYING for details.
122
123 import codecs
124@@ -19,6 +19,7 @@
125 import zlib
126 from subprocess import Popen, PIPE
127
128+import dateutil.parser
129 from dateutil.tz import tzlocal, tzutc
130 from pkg_resources import resource_exists, resource_filename
131
132@@ -187,6 +188,14 @@
133
134 return unicode(timestamp.strftime(format.encode('utf8')), 'utf8')
135
136+def parse_timestamp(timestamp):
137+ "Parse a machine timestamp, convert to UTC, strip timezone"
138+ dt = dateutil.parser.parse(timestamp)
139+ if dt.tzinfo:
140+ dt = dt.astimezone(tzutc())
141+ dt = dt.replace(tzinfo=None)
142+ return dt
143+
144 class JSONException(Exception):
145 pass
146

Subscribers

People subscribed via source and target branches