~chipaca/unity-lens-video/custom-user-agent

« back to all changes in this revision

Viewing changes to main/views.py

  • Committer: Janos Gyerik
  • Date: 2011-11-12 20:03:03 UTC
  • Revision ID: janos@axiom-20111112200303-122cpptng5rao9zv
added new field: OneLiner.was_tweeted

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
    return params
38
38
 
39
39
def tweet(oneliner, test=False, consumer_key=None, consumer_secret=None, access_token=None, access_token_secret=None):
40
 
    if oneliner.is_published:
41
 
        try:
42
 
            import tweepy # 3rd party lib, install with: easy_install tweepy
43
 
            import settings
44
 
            if consumer_key is None:
45
 
                consumer_key = settings.TWITTER.get('consumer_key')
46
 
            if consumer_secret is None:
47
 
                consumer_secret = settings.TWITTER.get('consumer_secret')
48
 
            if access_token is None:
49
 
                access_token = settings.TWITTER.get('access_token')
50
 
            if access_token_secret is None:
51
 
                access_token_secret = settings.TWITTER.get('access_token_secret')
52
 
 
53
 
            # set up credentials to use Twitter api.
54
 
            auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
55
 
            auth.set_access_token(access_token, access_token_secret)
56
 
            api = tweepy.API(auth)
57
 
 
58
 
            tweetmsg = 'http://bashoneliners.com/main/oneliner/%d %s: %s # posted by %s' % (
59
 
                    oneliner.pk,
60
 
                    oneliner.summary,
61
 
                    oneliner.line,
62
 
                    oneliner.user.username,
63
 
                    )
64
 
            if len(tweetmsg) > 161:
65
 
                tweetmsg = tweetmsg[:157] + ' ...'
66
 
            
67
 
            if test:
68
 
                print tweetmsg
69
 
                print
70
 
                return True
71
 
            else:
72
 
                return api.update_status(tweetmsg)
73
 
        except:
74
 
            pass
 
40
    if not oneliner.was_tweeted:
 
41
        if oneliner.is_published:
 
42
            try:
 
43
                import tweepy # 3rd party lib, install with: easy_install tweepy
 
44
                import settings
 
45
                if consumer_key is None:
 
46
                    consumer_key = settings.TWITTER.get('consumer_key')
 
47
                if consumer_secret is None:
 
48
                    consumer_secret = settings.TWITTER.get('consumer_secret')
 
49
                if access_token is None:
 
50
                    access_token = settings.TWITTER.get('access_token')
 
51
                if access_token_secret is None:
 
52
                    access_token_secret = settings.TWITTER.get('access_token_secret')
 
53
 
 
54
                # set up credentials to use Twitter api.
 
55
                auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
 
56
                auth.set_access_token(access_token, access_token_secret)
 
57
                api = tweepy.API(auth)
 
58
 
 
59
                tweetmsg = 'http://bashoneliners.com/main/oneliner/%d %s: %s # posted by %s' % (
 
60
                        oneliner.pk,
 
61
                        oneliner.summary,
 
62
                        oneliner.line,
 
63
                        oneliner.user.username,
 
64
                        )
 
65
                if len(tweetmsg) > 160:
 
66
                    tweetmsg = tweetmsg[:156] + ' ...'
 
67
                
 
68
                if test:
 
69
                    print tweetmsg
 
70
                    print
 
71
                    return True
 
72
                else:
 
73
                    oneliner.was_tweeted = True
 
74
                    oneliner.save()
 
75
                    return api.update_status(tweetmsg)
 
76
            except:
 
77
                pass
75
78
 
76
79
 
77
80
''' url handlers '''