Esempio n. 1
0
    def _sendBoxcar(self, msg, title, email, subscribe=False):
        msg = msg.strip()
        curUrl = API_URL
        data = urllib.urlencode({
                'email': email,
                'notification[from_screen_name]': title,
                'notification[message]': msg.encode('utf-8'),
                'notification[from_remote_service_id]': int(time.time())
                })
        if subscribe: # subscription notification
            data = urllib.urlencode({'email': email})
            curUrl = curUrl + "/subscribe"

        req = urllib2.Request(curUrl)
        try:
            handle = urllib2.urlopen(req, data)
            handle.close()
        except urllib2.URLError, e:
            if not hasattr(e, 'code'):
                logger.error("Boxcar notification failed. {0}".format(ex(e)))
                return False
            else:
                logger.warning("Boxcar notification failed. Error code: {0}".format(str(e.code)))

            if e.code == 404: #HTTP status 404 if the provided email address isn't a Boxcar user.
                logger.warning("Username is wrong/not a boxcar email. Boxcar will send an email to it")
                return False
            elif e.code == 401: #For HTTP status code 401's, it is because you are passing in either an invalid token, or the user has not added your service.
                if subscribe: #If the user has already added your service, we'll return an HTTP status code of 401.
                    logger.error("Already subscribed to service")
                    # i dont know if this is true or false ... its neither but i also dont know how we got here in the first place
                    return False
                else: #HTTP status 401 if the user doesn't have the service added
                    subscribeNote = self._sendBoxcar(msg, title, email, True)
                    if subscribeNote:
                        logger.debug("Subscription send")
                        return True
                    else:
                        logger.error("Subscription could not be send")
                        return False
            elif e.code == 400: #If you receive an HTTP status code of 400, it is because you failed to send the proper parameters
                logger.error("Wrong data send to boxcar")
                return False
Esempio n. 2
0
	def action(self, query, args=None):
		with db_lock:
	
			if query == None:
				return
	
			sqlResult = None
			attempt = 0
	
			while attempt < 5:
				try:
					if args == None:
						logger.debug("{0}: {1}".format(self.filename, query))
						#print query
						sqlResult = self.connection.execute(query)
					else:
						logger.debug("{0}: {1} with args {2}".format(self.filename, query, args))
						#print query, args
						sqlResult = self.connection.execute(query, args)
					self.connection.commit()
					# get out of the connection attempt loop since we were successful
					break
				except sqlite3.OperationalError, e:
					if "unable to open database file" in e.message or "database is locked" in e.message:
						logger.warning(u"DB error: ".format(ex(e)))
						#print "error(e)"
						attempt += 1
						time.sleep(1)
					else:
						logger.error(u"DB error: ".format(ex(e)))
						#print "error(e)"
						raise
				except sqlite3.DatabaseError, e:
					logger.error(u"Fatal error executing query: ".format(ex(e)))
					#print "error(e)"
					raise