-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrespondence.py
More file actions
153 lines (124 loc) · 5.07 KB
/
respondence.py
File metadata and controls
153 lines (124 loc) · 5.07 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''Social Graph building via Timeline filters'''
__author__ = 'colgur@gmail.com'
__version__ = '0.1-devel'
import time
import logging
import urllib
import urllib2
import simplejson as json
from types import *
import twitter
import igraph as ig
import socialgraph as sg
class RespondenceError(Exception):
@property
def message(self):
'''Return first argument used to construct this error'''
return self.args[0]
class TwitterUser(object):
def __init__(self):
self._sg = sg.SocialSubGraph(self.lookup)
@property
def socialgraph(self):
'''Get Social Subgraph for current User'''
return self._sg
def authenticate(self, username, password):
'''Twitter User must be authenticated to view other Timelines'''
try:
self._api = twitter.Api(username=username, password=password)
self._user = self._api.GetUser(username)
except twitter.TwitterError, twerr:
raise RespondenceError(twerr.message)
def populate_from_list(self, slug):
'''Helper populates ssg from Twitter List'''
try:
list_members = self._api.GetListMembers(slug)
except twitter.TwitterError, twerr:
raise RespondenceError("List Members Retrieval error with '%s'" % twerr.message)
except AttributeError, attrerr:
raise RepsondenceError("Check python-twitter version: %s" % attrerr.args[0])
list_users = list_members['users']
[self._sg.add_vertex(sg.SocialVertex(each_user.id))
for each_user in list_users if not each_user.protected]
def lookup(self, relationship, svids):
'''Low-level access to Twitter Social Graph'''
sviditer = iter([svids])
if type(svids) is ListType:
sviditer = iter(svids)
known_bots = ['MrTweet', 'wefollow', 'TweetStats', 'grader']
filtered_svids = []
for each_sv in sviditer:
user = self._api.GetUser(each_sv)
if user.friends_count > 300:
logging.info("'%s' has %d friends...",
user.screen_name, user.friends_count)
if user.screen_name not in known_bots:
filtered_svids.extend([each_sv])
try:
for each_socialvertex in socg.twitter_lookup(relationship, filtered_svids):
yield each_socialvertex
except:
raise
def rate_limit_status():
rate_limit_status_url = 'http://twitter.com/account/rate_limit_status.json'
result = urllib2.urlopen(rate_limit_status_url)
rate_limit_dict = json.load(result)
current_time = time.time()
reset_time = rate_limit_dict['reset_time_in_seconds']
remaining_hits = rate_limit_dict['remaining_hits']
print "Remaining Hits: %d" % remaining_hits
print "\tLocal time: %s" % time.strftime("%a %b %d %H:%M:%S %Z %Y",
time.localtime(current_time))
print "\tReset time: %s" % time.strftime("%a %b %d %H:%M:%S %Z %Y",
time.localtime(reset_time))
def test():
import respondence_test
def parse_options():
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--username", dest="username",
help="Screen Name of User", metavar="USER")
parser.add_option("-p", "--password", dest="password",
help="Password required for Authentication",
metavar="PASS")
(options, args) = parser.parse_args()
if options.username == None or options.password == None:
parser.print_help()
sys.exit()
return (options.username, options.password)
def main():
'''Entry point of typical use scenario'''
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s : %(message)s',
datefmt='%H:%M:%S')
(username, password) = parse_options()
current_user = TwitterUser()
try:
current_user.authenticate(username, password)
except RespondenceError, resperr:
logging.error("Authentication failed with '%s'", resperr.message())
sys.exit()
try:
current_user.populate_from_list("community1")
except RespondenceError, resperr:
logging.error("Populate failed with '%s'", resperr.message())
sys.exit()
# Free to operate on SocialGraph now
# e.g. current_sg.dlsiter(<some igraph.Vertex>, <some depth>)
current_sg = current_user.socialgraph
if __name__ == '__main__':
# test()
main()