def authorize_oauth_token(self, acc_id, pin): self.log.debug('Authorizating OAuth token for %s' % acc_id) try: account = self.accman.get(acc_id, False) if account.logged_in == LoginStatus.DONE: return Response(code=808) else: return Response(account.authorize_oauth_token(pin)) except Exception, exc: return self.__handle_exception(exc)
def broadcast_status(self, acc_array, text): responses = [] for acc_id in acc_array: try: account = self.accman.get(acc_id) resp = Response(account.update_status(text), account_id=acc_id) responses.append(resp) except Exception, exc: resp = self.__handle_exception(exc) resp.account_id = acc_id responses.append(resp)
def auth(self, acc_id): try: account = self.accman.get(acc_id, False) if account.logged_in == LoginStatus.DONE: return Response(code=808) else: self.accman.login_status(acc_id, LoginStatus.DONE) return Response(account.auth()) except Exception, exc: self.accman.login_status(acc_id, LoginStatus.NONE) return self.__handle_exception(exc)
def login(self, acc_id): self.log.debug('Starting login sequence with %s' % acc_id) try: account = self.accman.get(acc_id, False) if account.logged_in == LoginStatus.DONE: #add columns return Response(code=808) else: self.accman.login_status(acc_id, LoginStatus.IN_PROGRESS) return Response(account.start_login(acc_id)) except Exception, exc: return self.__handle_exception(exc)
def short_url(self, url): service = self.config.read('Services', 'shorten-url') try: urlshorter = URL_SERVICES[service].do_service(url) return Response(urlshorter.response) except Exception, exc: return self.__handle_exception(exc)
def get_column_statuses(self, acc_id, col_id, count=STATUSPP, since_id=None): try: account = self.accman.get(acc_id) if col_id == ColumnType.TIMELINE: rtn = self.__apply_filters( account.get_timeline(count, since_id)) elif col_id == ColumnType.REPLIES: rtn = account.get_replies(count, since_id) elif col_id == ColumnType.DIRECTS: rtn = account.get_directs(count, since_id) elif col_id == ColumnType.SENT: rtn = account.get_sent(count, since_id) elif col_id == ColumnType.FAVORITES: rtn = account.get_favorites(count) elif col_id == ColumnType.PUBLIC: rtn = account.get_public_timeline(count, since_id) else: list_id = account.get_list_id(col_id) if list_id is None: raise IndexError rtn = account.get_list_statuses(list_id, count, since_id) print len(rtn), rtn return Response(rtn) except Exception, exc: return self.__handle_exception(exc)
def get_user_profile(self, acc_id, user): try: account = self.accman.get(acc_id) profile = account.get_profile(user) profile.muted = self.is_muted(profile.username) return Response(profile) except Exception, exc: return self.__handle_exception(exc)
def update_profile(self, acc_id, args): try: account = self.accman.get(acc_id) new_profile = account.update_profile(args) account.set_profile(new_profile) return Response(new_profile) except Exception, exc: return self.__handle_exception(exc)
def get_profile_image(self, acc_id, user): try: account = self.accman.get(acc_id) img_content = account.get_profile_image(user) img_path = os.path.join(account.config.imgdir, user) fd = open(img_path, 'w') fd.write(img_content) fd.close() return Response(img_path) except Exception, exc: return self.__handle_exception(exc)
def get_all_friends_list(self): friends = [] try: for account in self.accman.get_all(): print account for profile in account.get_following(): if profile not in friends: friends.append(profile) self.config.save_friends([f.username for f in friends]) return Response(friends) except Exception, exc: return self.__handle_exception(exc)
def autoshort_url(self, message): service = self.config.read('Services', 'shorten-url') try: all_urls = get_urls(message) if len(all_urls) == 0: raise NoURLException # TODO: Validate already shorten URLs for url in all_urls: urlshorter = URL_SERVICES[service].do_service(url) message = message.replace(url, urlshorter.response) return Response(message) except Exception, exc: return self.__handle_exception(exc)
def unmute(self, user): try: self.config.remove_filter('@%s' % user) return Response(user) except Exception, exc: return self.__handle_exception(exc)
def get_following(self, acc_id, only_id=False): try: account = self.accman.get(acc_id) return Response(account.get_following(only_id)) except Exception, exc: return self.__handle_exception(exc)
def report_spam(self, acc_id, user): try: account = self.accman.get(acc_id) return Response(account.report_spam(user)) except Exception, exc: return self.__handle_exception(exc)
def is_friend(self, acc_id, user): try: account = self.accman.get(acc_id) return Response(account.is_friend(user)) except Exception, exc: return self.__handle_exception(exc)
def __handle_exception(self, exc, extra_info=''): self.__print_traceback() _type = type(exc) print "Exception type: %s" % (str(_type)) response = None if _type == urllib2.URLError: response = Response(code=801) elif _type == IndexError: return Response(code=808) elif _type == KeyError: response = Response(code=807) elif _type == NotImplementedError: response = Response(code=900) elif _type == ZeroDivisionError: response = Response(code=809) elif _type == urllib2.HTTPError: if exc.code in ERROR_CODES: response = Response(code=exc.code) elif (exc.code == 400): self.log.debug("Error HTTP 400 detected: %s" % exc) response = Response(code=100) response.errmsg = "Sorry, server is limiting your API calls" elif (exc.code == 403): msg = '' errmsg = exc.read() self.log.debug("Error HTTP 403 detected: %s" % errmsg) if type(errmsg) == str: msg = errmsg elif type(errmsg) == dict: if errmsg.has_key('error'): msg = errmsg['error'] else: msg = errmsg if msg.find("Status is a duplicate.") > 0: response = Response(code=802) elif msg.find("is already on your list.") > 0: response = Response(code=802) elif msg.find("already requested to follow") > 0: response = Response(code=802) elif msg.find( "cannot send messages to users who are not following you" ) > 0: response = Response(code=813) elif msg.find("text of your tweet is too long") > 0: response = Response(code=814) else: response = Response(code=100) response.errmsg = msg elif _type == ValueError: response = Response(code=404) elif _type == ssl.SSLError: response = Response(code=810) elif _type == URLShortenError: response = Response(code=811) elif _type == NoURLException: response = Response(code=812) else: response = Response(code=999) self.log.debug(response.errmsg) return response
def unblock(self, acc_id, user): try: account = self.accman.get(acc_id) return Response(account.unblock(user)) except Exception, exc: return self.__handle_exception(exc)
def search(self, acc_id, query): try: account = self.accman.get(acc_id, False) return Response(account.search(query)) except Exception, exc: return self.__handle_exception(exc)
def unrepeat_status(self, acc_id, status_id): try: account = self.accman.get(acc_id) return Response(account.unrepeat(status_id)) except Exception, exc: return self.__handle_exception(exc)
def destroy_direct(self, acc_id, status_id): try: account = self.accman.get(acc_id) return Response(account.destroy_direct(status_id)) except Exception, exc: return self.__handle_exception(exc)
def unmark_favorite(self, acc_id, status_id): try: account = self.accman.get(acc_id) return Response(account.unmark_favorite(status_id)) except Exception, exc: return self.__handle_exception(exc)
def send_direct(self, acc_id, username, message): try: account = self.accman.get(acc_id) return Response(account.send_direct(username, message)) except Exception, exc: return self.__handle_exception(exc)
def get_own_profile(self, acc_id): try: account = self.accman.get(acc_id) return Response(account.profile) except Exception, exc: return self.__handle_exception(exc)
def get_conversation(self, acc_id, status_id): try: account = self.accman.get(acc_id) return Response(account.get_conversation(status_id)) except Exception, exc: return self.__handle_exception(exc)
def get_public_timeline(self, acc_id, count=STATUSPP, since_id=None): try: account = self.accman.get(acc_id, False) return Response(account.get_public_timeline(count, since_id)) except Exception, exc: return self.__handle_exception(exc)
def trends(self, acc_id): try: account = self.accman.get(acc_id, False) return Response(account.trends()) except Exception, exc: return self.__handle_exception(exc)
def unfollow(self, acc_id, username): try: account = self.accman.get(acc_id) return Response(account.unfollow(username)) except Exception, exc: return self.__handle_exception(exc)
def update_status(self, acc_id, text, in_reply_id=None): try: account = self.accman.get(acc_id) return Response(account.update_status(text, in_reply_id)) except Exception, exc: return self.__handle_exception(exc)
def __handle_exception(self, exc, extra_info=''): self.__print_traceback() _type = type(exc) print "Exception type: %s" % (str(_type)) response = None if _type == urllib2.URLError: response = Response(code=801) elif _type == IndexError: return Response(code=808) elif _type == KeyError: response = Response(code=807) elif _type == NotImplementedError: response = Response(code=900) elif _type == ZeroDivisionError: response = Response(code=809) elif _type == urllib2.HTTPError: if exc.code in ERROR_CODES: response = Response(code=exc.code) elif (exc.code == 400): self.log.debug("Error HTTP 400 detected: %s" % exc) response = Response(code=100) response.errmsg = "Sorry, server is limiting your API calls" elif (exc.code == 403): msg = '' errmsg = exc.read() self.log.debug("Error HTTP 403 detected: %s" % errmsg) if type(errmsg) == str: msg = errmsg elif type(errmsg) == dict: if 'error' in errmsg: msg = errmsg['error'] else: msg = errmsg if msg.find("Status is a duplicate.") > 0: response = Response(code=802) elif msg.find("is already on your list.") > 0: response = Response(code=802) elif msg.find("already requested to follow") > 0: response = Response(code=802) elif msg.find("cannot send messages to users who are not following you") > 0: response = Response(code=813) elif msg.find("text of your tweet is too long") > 0: response = Response(code=814) else: response = Response(code=100) response.errmsg = msg elif _type == ValueError: response = Response(code=404) elif _type == ssl.SSLError: response = Response(code=810) elif _type == URLShortenError: response = Response(code=811) elif _type == NoURLException: response = Response(code=812) else: response = Response(code=999) self.log.debug(response.errmsg) return response
except Exception, exc: return self.__handle_exception(exc) def broadcast_status(self, acc_array, text): responses = [] for acc_id in acc_array: try: account = self.accman.get(acc_id) resp = Response(account.update_status(text), account_id=acc_id) responses.append(resp) except Exception, exc: resp = self.__handle_exception(exc) resp.account_id = acc_id responses.append(resp) return Response(responses) def destroy_status(self, acc_id, status_id): try: account = self.accman.get(acc_id) return Response(account.destroy_status(status_id)) except Exception, exc: return self.__handle_exception(exc) def get_single_status(self, acc_id, status_id): try: account = self.accman.get(acc_id) return Response(account.get_status(status_id)) except Exception, exc: return self.__handle_exception(exc)