google-code-export / django-syncr

Automatically exported from code.google.com/p/django-syncr
0 stars 0 forks source link

del.icio.us links not in correct time zone #8

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When I use DeliciousSyncr.syncRecent() the objects that are created are not
using the correct time. I believe the issue is because Del.icio.us returns
links in GMT, but my local timezone is not.

Original issue reported on code.google.com by brian.wa...@gmail.com on 11 May 2008 at 3:59

GoogleCodeExporter commented 9 years ago
Hey Brian, Yes we did observe that the synced objects bear the timestamp of 
their respective server. In order to change the timestamp to your localtime you 
do this.

in app/delicious.py

def _syncPost(self, post_elem):
        post_hash = post_elem.attrib['hash']
        time_lst = time.strptime(post_elem.attrib['time'], "%Y-%m-%dT%H:%M:%SZ")
        time_obj = datetime.datetime(*time_lst[0:7])
        time_final = time_obj - datetime.timedelta(hours=7)
        tags = self.clean_tags(post_elem.attrib['tag'])

        try:
            extended = post_elem.attrib['extended']
        except KeyError:
            extended = ''
        default_dict = {'description': post_elem.attrib['description'],
                        'tag_list': tags,
                        'url': post_elem.attrib['href'],
                        # Is post_hash attrib unique to the post/URL or post/username ?!
                        'post_hash': post_hash,
                        'saved_date': time_final,
                        'extended_info': extended,
                        }
        obj, created = Bookmark.objects.get_or_create(post_hash = post_hash,
                                                      defaults = default_dict)
        return obj

You can change the hours value to your timezone difference. I hope this works, 
this worked with twitter for me. Make sure you change the 'saved_date' to 
time_final in 
default dict. Try out and let me know

Original comment by yash...@gmail.com on 24 Jun 2008 at 5:09

GoogleCodeExporter commented 9 years ago
Could you please show how you would fix this issue with twitter? Thanks! :D

Original comment by misstri...@gmail.com on 15 Jul 2008 at 2:54

GoogleCodeExporter commented 9 years ago
I'm still not entirely clear on this issue... can someone post example code 
demonstrating the problem? Or steps 
for recreating it w/TwitterSyncr or DeliciousSyncr?

Original comment by jesse.l...@gmail.com on 15 Jul 2008 at 12:31

GoogleCodeExporter commented 9 years ago
I think it is the same thing as in comment 1, I just don't know the exact lines 
of code to be added to 
twitter.py.

Basically it is what Yash said... the timestamp is coming from the twitter (or 
del.icio.us) server and is not in 
the same time zone as my app. So, if I actually posted a tweet at 6, when its 
imported into my app, it shows 
that it was posted at 10 instead.

For example; twitter says I posted this tweet 14 hours ago: 
http://twitter.com/misstricky/statuses/858454083

My app says I posted the same tweet about 10 hours ago.

Original comment by misstri...@gmail.com on 15 Jul 2008 at 12:42

GoogleCodeExporter commented 9 years ago
Hey here is the twitter part of correcting the timezone,

def _syncTwitterStatus(self, status):
    user = self._syncTwitterUser(status.user)
        tweettime = datetime.datetime.strptime(status.created_at, "%a %b %d %H:%M:%S +0000 %Y")
        pub_time = tweettime - datetime.timedelta(hours=7)
        default_dict = {'pub_time': pub_time,
                        'twitter_id': status.id,
                        'text': smart_unicode(status.text),
                        'user': user}
        obj, created = Tweet.objects.get_or_create(twitter_id = status.id,
                                                   defaults = default_dict)
        return obj

Just change the value of hours in timedelta function to your timezone. 
Everything works fine.

Original comment by yash...@gmail.com on 15 Jul 2008 at 5:17

GoogleCodeExporter commented 9 years ago
thanks for the line of code! works perfectly. :)

- liz

Original comment by misstri...@gmail.com on 15 Jul 2008 at 7:13

GoogleCodeExporter commented 9 years ago
Hey again,

That worked perfectly for my local set up... however I keep getting an error 
when I try to run it on my host. I 
uploaded the exact same copy of syncr that I have working here.. I get the 
error:

AttributeError: type object 'datetime.datetime' has no attribute 'strptime'

The only differences I know of with my host is it is on python 2.4 instead of 
2.5, and is on a svn of Django 
from today.

Thanks again! :)
- Liz

Original comment by misstri...@gmail.com on 18 Jul 2008 at 10:13

GoogleCodeExporter commented 9 years ago
Hey liz,
  I checked it on my host and it worked perfectly. I am using python 2.5. Python2.4
datetime module do not have strptime attribute. I tried importing datetime in 
both
python2.4 and python2.5, strptime is causing an attribute error in python2.4. 

Original comment by yash...@gmail.com on 18 Jul 2008 at 11:32

GoogleCodeExporter commented 9 years ago
could you propose a different solution since my host forces me to use python 
2.4? Thanks! :)

Original comment by misstri...@gmail.com on 19 Jul 2008 at 1:01

GoogleCodeExporter commented 9 years ago
Two notes: first this is not an ideal solution to the timezone problem, but it 
will work in your cases. I hope to 
put something more permanent/flexible into the syncr code soon.

As far as your problem goes, python 2.4 datetime does not include a strptime 
method as yashh said, but you 
will be able to find an equivalent in the time module:

 from time import strptime

Try this that instead.

Original comment by jesse.l...@gmail.com on 19 Jul 2008 at 1:39

GoogleCodeExporter commented 9 years ago
Hey, thank you! That import worked. However, now I'm getting this error: 

  pub_time = tweettime-timedelta(hours=4)
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 
'datetime.timedelta'

Works perfectly on my dev machine... hehe

- Liz

Original comment by misstri...@gmail.com on 19 Jul 2008 at 2:56

GoogleCodeExporter commented 9 years ago
Any chance for solution that works with python 2.4? Thanks again. :)

Original comment by misstri...@gmail.com on 19 Jul 2008 at 7:30

GoogleCodeExporter commented 9 years ago
Ok I will suggest another solution. Install pytz (http://pytz.sourceforge.net/)

easy_install pytz

In the view 

import pytz
tz = pytz.timezone(settings.TIME_ZONE)

Make sure you set the TIME_ZONE variable in your settings.py

Now in the _syncTwitterStatus(self, status)

tweettime = datetime.datetime.strptime(status.created_at, "%a %b %d %H:%M:%S 
+0000 %Y")
pub_time = tweettime.replace(tzinfo=pytz.utc).astimezone(tz)

The remaining part is the same. I think this is a little better solution. I 
heard a bug when used with mysql DB. But just try this and let us know if any 
problem exists. 

Original comment by yash...@gmail.com on 19 Jul 2008 at 8:58

GoogleCodeExporter commented 9 years ago
Hey, I installed pytz. However I am still getting this error:

    pub_time = tweettime.replace(tzinfo=pytz.utc).astimezone(tz)
AttributeError: 'time.struct_time' object has no attribute 'replace'

Original comment by misstri...@gmail.com on 20 Jul 2008 at 3:46

GoogleCodeExporter commented 9 years ago
hey any solutions to the error? Thanks! :)

Original comment by misstri...@gmail.com on 20 Jul 2008 at 11:36

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Hmm looks like python2.4 creates some new issues. Check out the pytz 
documentation

http://pytz.sourceforge.net/

It supports python 2.3 and higher. 

Original comment by yash...@gmail.com on 20 Jul 2008 at 11:55

GoogleCodeExporter commented 9 years ago
I don't think the error is coming from pytz. I think it is coming from 2.4's 
strptime.... could you try the same 
code with something other than strptime? it does not seem to work the way it is 
intended in the code... 

Original comment by misstri...@gmail.com on 21 Jul 2008 at 12:11

GoogleCodeExporter commented 9 years ago
You need a datetime object to use the replace() method. Python 2.4 does not 
support strptime in the datetime 
module, but you can use the time module's version and manually convert that to 
a datetime object as such:

datetime.datetime(*time.strptime("20-3-2005","%d-%m-%Y")[:6])

Or in your code replace: 
tweettime = datetime.datetime.strptime(status.created_at, "%a %b %d %H:%M:%S 
+0000 %Y")
with:
tweettime = datetime.datetime(*time.strptime(status.created_at, "%a %b %d 
%H:%M:%S +0000 %Y")[:6])

Original comment by jesse.l...@gmail.com on 21 Jul 2008 at 1:04

GoogleCodeExporter commented 9 years ago
thank you! it works!! :D yay! :D :D

Original comment by misstri...@gmail.com on 21 Jul 2008 at 1:11

GoogleCodeExporter commented 9 years ago
I've just come across this thread now. FWIW, this is my implementation:

// project.util.timeconverter.py

from django.conf import settings
from dateutil import tz
import pytz

def time_to_settings(time_field):
    zone = pytz.timezone(settings.TIME_ZONE)
    newtime = time_field.replace(tzinfo=tz.tzutc()).astimezone(zone)
    return newtime

def time_to_utc(time_field):
    new_time = time_field.replace(tzinfo=pytz.utc)
    return new_time

// usage: modified the save() method of syncr.delicious.models.Bookmark
from project.util.timeconverter.py import time_to_settings

def save(self, *args, **kwargs):
  self.saved_date = time_to_settings(self.saved_date)
  super(Bookmark, self).save(*args, **kwargs)

Original comment by gmacgre...@gmail.com on 13 Jan 2009 at 4:15