示例#1
0
    def _sendAndroidPN(self, title, msg, url, username, broadcast):

        # build up the URL and parameters
        msg = msg.strip()

        data = urllib.urlencode({
            'action': "send",
            'broadcast': broadcast,
            'uri': "",
            'title': title,
            'username': username,
            'message': msg.encode('utf-8'),
        })

        # send the request to pushover
        try:
            req = urllib2.Request(url)
            handle = urllib2.urlopen(req, data)
            handle.close()

        except urllib2.URLError as e:
            # FIXME: Python 2.5 hack, it wrongly reports 201 as an error
            if hasattr(e, 'code') and e.code == 201:
                logger.debug(u"ANDROIDPN: Notification successful.")
                return True

            # if we get an error back that doesn't have an error code then who knows what's really happening
            if not hasattr(e, 'code'):
                logger.error(u"ANDROIDPN: Notification failed.")
                return False
            else:
                logger.error(u"ANDROIDPN: Notification failed. Error code: " + str(e.code))

            # HTTP status 404 if the provided email address isn't a AndroidPN user.
            if e.code == 404:
                logger.warning(
                    u"ANDROIDPN: Username is wrong/not a AndroidPN email. AndroidPN will send an email to it")
                return False

            # 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.
            elif e.code == 401:

                # HTTP status 401 if the user doesn't have the service added
                subscribeNote = self._sendAndroidPN(title, msg, username)
                if subscribeNote:
                    logger.debug(u"ANDROIDPN: Subscription sent")
                    return True
                else:
                    logger.error(u"ANDROIDPN: Subscription could not be sent")
                    return False

            # If you receive an HTTP status code of 400, it is because you failed to send the proper parameters
            elif e.code == 400:
                logger.error(u"ANDROIDPN: Wrong data sent to AndroidPN")
                return False

        logger.debug(u"ANDROIDPN: Notification successful.")
        return True
示例#2
0
    def _sendAndroidPN(self, title, msg, url, username, broadcast):

        # build up the URL and parameters
        msg = msg.strip()

        data = urllib.urlencode({
            'action': "send",
            'broadcast': broadcast,
            'uri': "",
            'title': title,
            'username': username,
            'message': msg.encode('utf-8'),
        })

        # send the request to pushover
        try:
            req = urllib2.Request(url)
            handle = urllib2.urlopen(req, data)
            handle.close()

        except urllib2.URLError, e:
            # FIXME: Python 2.5 hack, it wrongly reports 201 as an error
            if hasattr(e, 'code') and e.code == 201:
                logger.debug(u"ANDROIDPN: Notification successful.")
                return True

            # if we get an error back that doesn't have an error code then who knows what's really happening
            if not hasattr(e, 'code'):
                logger.error(u"ANDROIDPN: Notification failed.")
                return False
            else:
                logger.error(u"ANDROIDPN: Notification failed. Error code: " +
                             str(e.code))

            # HTTP status 404 if the provided email address isn't a AndroidPN user.
            if e.code == 404:
                logger.warning(
                    u"ANDROIDPN: Username is wrong/not a AndroidPN email. AndroidPN will send an email to it"
                )
                return False

            # 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.
            elif e.code == 401:

                # HTTP status 401 if the user doesn't have the service added
                subscribeNote = self._sendAndroidPN(title, msg, username)
                if subscribeNote:
                    logger.debug(u"ANDROIDPN: Subscription sent")
                    return True
                else:
                    logger.error(u"ANDROIDPN: Subscription could not be sent")
                    return False

            # If you receive an HTTP status code of 400, it is because you failed to send the proper parameters
            elif e.code == 400:
                logger.error(u"ANDROIDPN: Wrong data sent to AndroidPN")
                return False
示例#3
0
 def _connect(self):
     logger.info('Connecting to %s:%s' % (self.host, self.port))
     try:
         self._socket.connect((self.host, self.port))
     except ssl.SSLError as e:
         # Note: have not verified that we actually get errno 258 for this error
         if (hasattr(ssl, 'PROTOCOL_SSLv3')
                 and (getattr(e, 'reason', None) == 'UNSUPPORTED_PROTOCOL'
                      or e.errno == 258)):
             logger.warning(
                 'Was unable to ssl handshake, trying to force SSLv3 (insecure)'
             )
             self._create_socket(ssl_version=ssl.PROTOCOL_SSLv3)
             self._socket.connect((self.host, self.port))
         else:
             raise
示例#4
0
    def find_book(self, bookid=None, queue=None):
        myDB = database.DBConnection()

        URL = 'https://www.goodreads.com/book/show/' + bookid + '?' + urllib.urlencode(self.params)

        try:
            rootxml, in_cache = get_xml_request(URL)
            if rootxml is None:
                logger.debug("Error requesting book")
                return
        except Exception as e:
            logger.error("Error finding book: %s" % str(e))
            return

        bookLanguage = rootxml.find('./book/language_code').text
        bookname = rootxml.find('./book/title').text

        if not bookLanguage:
            bookLanguage = "Unknown"
#
# PAB user has said they want this book, don't block for unwanted language, just warn
#
        valid_langs = ([valid_lang.strip() for valid_lang in lazylibrarian.IMP_PREFLANG.split(',')])
        if bookLanguage not in valid_langs:
            logger.debug('Book %s language does not match preference, %s' % (bookname, bookLanguage))

        if (rootxml.find('./book/publication_year').text is None):
            bookdate = "0000"
        else:
            bookdate = rootxml.find('./book/publication_year').text

        try:
            bookimg = rootxml.find('./book/img_url').text
            if 'assets/nocover' in bookimg:
                bookimg = 'images/nocover.png'
        except (KeyError, AttributeError):
            bookimg = 'images/nocover.png'

        authorname = rootxml.find('./book/authors/author/name').text
        bookdesc = rootxml.find('./book/description').text
        bookisbn = rootxml.find('./book/isbn').text
        bookpub = rootxml.find('./book/publisher').text
        booklink = rootxml.find('./book/link').text
        bookrate = float(rootxml.find('./book/average_rating').text)
        bookpages = rootxml.find('.book/num_pages').text

        name = authorname
        GR = GoodReads(name)
        author = GR.find_author_id()
        if author:
            AuthorID = author['authorid']
        else:
            logger.warning("No AuthorID for %s, unable to add book %s" % (authorname, bookname))
            return

        bookname = unaccented(bookname)
        bookname, booksub = split_title(authorname, bookname)
        dic = {':': '', '"': '', '\'': ''}
        bookname = replace_all(bookname, dic).strip()
        booksub = replace_all(booksub, dic).strip()
        if booksub:
            series, seriesNum = bookSeries(booksub)
        else:
            series, seriesNum = bookSeries(bookname)

        controlValueDict = {"BookID": bookid}
        newValueDict = {
            "AuthorName": authorname,
            "AuthorID": AuthorID,
            "AuthorLink": None,
            "BookName": bookname,
            "BookSub": booksub,
            "BookDesc": bookdesc,
            "BookIsbn": bookisbn,
            "BookPub": bookpub,
            "BookGenre": None,
            "BookImg": bookimg,
            "BookLink": booklink,
            "BookRate": bookrate,
            "BookPages": bookpages,
            "BookDate": bookdate,
            "BookLang": bookLanguage,
            "Status": "Wanted",
            "BookAdded": today(),
            "Series": series,
            "SeriesNum": seriesNum
        }

        myDB.upsert("books", newValueDict, controlValueDict)
        logger.debug("%s added to the books database" % bookname)

        if 'nocover' in bookimg or 'nophoto' in bookimg:
            # try to get a cover from librarything
            workcover = getBookCover(bookid)
            if workcover:
                logger.debug(u'Updated cover for %s to %s' % (bookname, workcover))
                controlValueDict = {"BookID": bookid}
                newValueDict = {"BookImg": workcover}
                myDB.upsert("books", newValueDict, controlValueDict)

        elif bookimg and bookimg.startswith('http'):
            link = cache_cover(bookid, bookimg)
            if link:
                controlValueDict = {"BookID": bookid}
                newValueDict = {"BookImg": link}
                myDB.upsert("books", newValueDict, controlValueDict)

        if seriesNum is None:
            #  try to get series info from librarything
            series, seriesNum = getWorkSeries(bookid)
            if seriesNum:
                logger.debug(u'Updated series: %s [%s]' % (series, seriesNum))
                controlValueDict = {"BookID": bookid}
                newValueDict = {
                    "Series": series,
                    "SeriesNum": seriesNum
                }
                myDB.upsert("books", newValueDict, controlValueDict)

        worklink = getWorkPage(bookid)
        if worklink:
            controlValueDict = {"BookID": bookid}
            newValueDict = {"WorkPage": worklink}
            myDB.upsert("books", newValueDict, controlValueDict)