This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_new_commit.py
72 lines (64 loc) · 2.92 KB
/
tweet_new_commit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import sys
import datetime
import tweepy
from git import Repo
from django.core.management.base import BaseCommand
# Git repository path
git_path = '/path/to/your/repository/'
# Twitter oauth file with ACCESS_KEY, ACCESS_SECRET
# file located in the same path as this script
file = 'TwitterOAuth.txt'
# OAuth Settings
CONSUMER_KEY = "#######################################" # Your CONSUMER_KEY
CONSUMER_SECRET = "####################################" # Your CONSUMER_SECRET
class Command(BaseCommand):
def handle(self, *args, **options):
repo = Repo(git_path) # Connect to your repository
assert repo.bare == False
repo.config_reader() # Get a config reader for read-only access
last_commit = repo.head.commit.message # Get the last commit
print "Your last commit is '{0}'".format(last_commit.rstrip())
self.twitter_authorize()
tweet = last_commit.capitalize() # Capitalize first letter. We are serious gentlemen. Yeah :)
# Update status on Twitter
try:
self.api.update_status(tweet)
print "Commit successfully tweeted."
except tweepy.error.TweepError, e:
print e, "You have this commit in the Twitter timeline."
def twitter_authorize(self):
config = self.get_credentials()
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
# If script execution is firstly and there is no file with ACCESS_KEY and ACCESS_SECRET we are getting
# them from Twitter and writing to the file. Next time we'll use them and skip authorization on Twitter.
if config['no file'] or config['no data']:
auth_url = auth.get_authorization_url()
print '\nPlease authorize: ' + auth_url
print '\nType bellow the PIN from Twitter site!'
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
with open(os.path.join(os.path.dirname(__file__),file), 'w') as f:
f.write("ACCESS_KEY = %s\n" % auth.access_token.key)
f.write("ACCESS_SECRET = %s" % auth.access_token.secret)
f.close()
config = self.get_credentials()
auth.set_access_token(config['ACCESS_KEY'], config['ACCESS_SECRET'])
self.api = tweepy.API(auth)
def get_credentials(self):
result = {'no data': False}
try:
with open(os.path.join(os.path.dirname(__file__),file)) as f:
read_data = f.read()
f.close()
for line in read_data.split('\n'):
if "ACCESS_KEY" in line:
result['ACCESS_KEY'] = line.split(" = ")[1]
elif "ACCESS_SECRET" in line:
result['ACCESS_SECRET'] = line.split(" = ")[1]
else:
result['no data'] = True
result['no file'] = False
except IOError:
result['no file'] = True
return result