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

« back to all changes in this revision

Viewing changes to scripts/tweet.py

  • Committer: Janos Gyerik
  • Date: 2012-04-04 13:22:47 UTC
  • Revision ID: burlyman@titan2x.com-20120404132247-2s7liq2i4e7nn8ul
minor improvement to create_user_profile signal

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# To access twitter with OAuth, you need to first obtain:
 
4
#    consumer key, consumer secret, access token, access token secret
 
5
#
 
6
# 1. Login to Twitter
 
7
# 2. Go to https://dev.twitter.com/apps/new and fill the form
 
8
#    - Application Type should be Client
 
9
#    - Default Access Type must be Read and Write
 
10
# 3. Consumer key and secret are here: https://dev.twitter.com/apps
 
11
# 4. Access token and secret are in the My Access Token menu
 
12
#
 
13
 
 
14
''' hack to setup django environment START '''
 
15
import sys
 
16
sys.path.append('..')
 
17
import settings
 
18
from django.core.management import setup_environ
 
19
setup_environ(settings)
 
20
''' hack to setup django environment END '''
 
21
 
 
22
from oneliners.models import OneLiner
 
23
from oneliners.views import tweet
 
24
 
 
25
import optparse
 
26
 
 
27
try:
 
28
    consumer_key = settings.TWITTER.get('consumer_key')
 
29
    consumer_secret = settings.TWITTER.get('consumer_secret')
 
30
    access_token = settings.TWITTER.get('access_token')
 
31
    access_token_secret = settings.TWITTER.get('access_token_secret')
 
32
except:
 
33
    consumer_key = None
 
34
    consumer_secret = None
 
35
    access_token = None
 
36
    access_token_secret = None
 
37
 
 
38
if __name__ == '__main__':
 
39
    parser = optparse.OptionParser()
 
40
    parser.set_usage('%prog [options]')
 
41
    parser.set_description('Tweet specified one-liners by @bashoneliners')
 
42
 
 
43
    parser.add_option('--pk', '--id', help='The pk/id of the OneLiner to tweet', type=int, action='append')
 
44
    parser.add_option('--recent', help='List recent OneLiners, do not tweet', action='store_true', default=False)
 
45
    parser.add_option('--send', help='Send tweets', action='store_true', default=False)
 
46
 
 
47
    (options, args) = parser.parse_args()
 
48
 
 
49
    if not (consumer_key and consumer_secret and access_token and access_token_secret):
 
50
        if not consumer_key:
 
51
            print 'Error: Consumer Key is required!'
 
52
        if not consumer_secret:
 
53
            print 'Error: Consumer Secret is required!'
 
54
        if not access_token:
 
55
            print 'Error: Access Token is required!'
 
56
        if not access_token_secret:
 
57
            print 'Error: Access Token Secret is required!'
 
58
        parser.print_help()
 
59
        parser.exit()
 
60
 
 
61
    if options.recent:
 
62
        for oneliner in OneLiner.recent()[:10]:
 
63
            print oneliner.pk,
 
64
            print oneliner.summary
 
65
            print oneliner.line
 
66
            print
 
67
        parser.exit()
 
68
 
 
69
    if options.pk:
 
70
        for pk in options.pk:
 
71
            oneliner = OneLiner.objects.get(pk=pk)
 
72
 
 
73
            if options.send:
 
74
                print tweet(oneliner, force=True)
 
75
            else:
 
76
                print tweet(oneliner, force=True, test=True)
 
77
 
 
78
# eof