summaryrefslogtreecommitdiffstats
path: root/modules/twitter
blob: fa4d366dfc9cacb0916f13f820ebab7e59c3b2d8 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
#encode=utf8

import sys, twitter

sys.stderr = sys.stdout

def shorten(text):
	
	text = text.replace("about ", "~")
	text = text.replace("an", "1")
	text = text.replace(" minutes", "m")
	text = text.replace(" minute", "m")
	text = text.replace(" hours", "h")
	text = text.replace(" hour", "h")
	text = text.replace(" seconds", "s")
	text = text.replace(" second", "s")
	text = text.replace(" days", "d")
	text = text.replace(" day", "d")
	text = text.replace(" weeks", "w")
	text = text.replace(" week", "w")
	text = text.replace(" years", "y")
	text = text.replace(" year", "y")
	text = text.replace(" ago", "")
	return text
	

def parse(text):
	text = text.replace("\n","")
	t = text.split(" ")
	new = ""
	for piece in t:
		escape = ""
		if piece.startswith("@"):
			escape = ""
			if piece[1:].lower() == "shackspam" or piece[1:].lower() == "shackspace":
				escape = ""
				
		elif piece.startswith("#"):
			escape = ""
		elif piece.startswith("http://"):
			escape = ""
		if escape:
			new += escape + piece + " "
		else:
			new += piece + " "

	return new

if len(sys.argv) > 1 and sys.argv[1] == "--help":
	print "read or post to the shackspam twitter timeline"
	if len(sys.argv) == 3 and sys.argv[2] == "--verbose":
		print "call without parameters to read"
		print "use /twitter [your tweet] to tweet something to the shackspam twitter timeline"
	exit()	




api = twitter.Api(username="shackspam", password="sh4ckit")

#api.SetXTwitterHeaders("Shackspace UTTERANCE","","0.00.0.01.42")

if len(sys.argv) > 1:
	
	
	tweet = u""
	
	for t in sys.argv[1:]:
		tweet += t.decode("utf8") + u" "
	tweet = tweet[:-1]

	print u"Length: " + str(len(tweet)) + u""

	if len(tweet) > 140:
		print u"Your Tweet must be shorter than 140 characters.\n"
		exit()
	
	status = api.PostUpdate(tweet)
	
	
	

else:	
	"""print "\tshackspace tweets:"
	for i in api.GetUserTimeline():
		i.text = parse(i.text)
		print ("" + i.user.screen_name + " " + i.GetRelativeCreatedAt() + " " + i.text).encode("utf8").replace("\n"," ")
	
	print "\n\tfriends tweets"
	"""
	a = api.GetFriendsTimeline()
	a.reverse()
	for i in a:
		text = i.text
		if i.user.screen_name.lower() == "shackspace" or i.user.screen_name.lower() == "shackspam":
			info = "" + i.user.screen_name + " " + shorten(i.GetRelativeCreatedAt()) + "" 
		else:
			info = "" + i.user.screen_name + " " + shorten(i.GetRelativeCreatedAt()) + "" 
		text = info + " " + parse(text)
		
		t = text.split(" ")
		tmp = ""
		for n in t:
			if ((len(tmp) + len(n)) > 80):
				print parse(tmp).encode("utf8")
				tmp = " " *( len(info)-15)
			
			tmp += " " + n
		if len(tmp) > (len(info)-15+1):	
			print parse(tmp).encode("utf8")