Пример #1
0
note = Types.Note()
note.title = "Test note from EDAMTest.py"

# To include an attachment such as an image in a note, first create a Resource
# for the attachment. At a minimum, the Resource contains the binary attachment
# data, an MD5 hash of the binary data, and the attachment MIME type.
# It can also include attributes such as filename and location.
image_path = constants_path = os.path.join(os.path.dirname(__file__),
                                           "enlogo.png")
with open(image_path, 'rb') as image_file:
    image = image_file.read()
md5 = hashlib.md5()
md5.update(image)
hash = md5.digest()

data = Types.Data()
data.size = len(image)
data.bodyHash = hash
data.body = image

resource = Types.Resource()
resource.mime = 'image/png'
resource.data = data

# Now, add the new Resource to the note's list of resources
note.resources = [resource]

# To display the Resource as part of the note's content, include an <en-media>
# tag in the note's ENML content. The en-media tag identifies the corresponding
# Resource using the MD5 hash.
hash_hex = binascii.hexlify(hash)
    def resizeNoteImage(noteDetail):
        """Resize note each media image and update note content

        Args:
            noteDetail (Note): evernote note
        Returns:
            new resouce list with resized imgage resource
        Raises:
        """
        newResList = []
        originResList = noteDetail.resources
        if not originResList:
            # is None
            return newResList

        originResNum = len(originResList)
        for curResIdx, eachResource in enumerate(originResList):
            curResNum = curResIdx + 1
            if crifanEvernote.isImageResource(eachResource):
                imgFilename = eachResource.attributes.fileName
                logging.info("[%d/%d] imgFilename=%s", curResNum, originResNum,
                             imgFilename)

                resBytes = eachResource.data.body

                resizeImgInfo = utils.resizeSingleImage(resBytes)

                originFormat = resizeImgInfo["originFormat"]  # 'JPEG'
                originSize = resizeImgInfo["originSize"]  # (1080, 2340)
                originLen = resizeImgInfo["originLen"]  # 73348

                newFormat = resizeImgInfo["newFormat"]  # 'JPEG'
                newSize = resizeImgInfo["newSize"]  # (360, 780)
                newBytes = resizeImgInfo["newBytes"]
                newLen = resizeImgInfo["newLen"]  # 13795

                resizeRatio = resizeImgInfo[
                    "resizeRatio"]  # 0.18807602115940447
                resizeRatioInt = int(resizeRatio * 100)  # 18

                originLenStr = utils.formatSize(originLen)  # '71.6KB'
                newLenStr = utils.formatSize(newLen)  # '37.7KB'

                logging.info(
                    "Resized: %s,%sx%s,%s -> %s,%sx%s,%s => ratio=%d%%",
                    originFormat, originSize[0], originSize[1], originLenStr,
                    newFormat, newSize[0], newSize[1], newLenStr,
                    resizeRatioInt)
                # Resized image: origin: fmt=JPEG,size=1080x2340,len=71.6KB -> new: fmt=JPEG,size=360x780,len=13.5KB => ratio=18%

                newMd5Bytes = utils.calcMd5(
                    newBytes, isRespBytes=True
                )  # b'\xaa\x05r\x15l\xb8\xa9\x9a\xe3\xc3MR2\x08\xa8['
                newMime = utils.ImageFormatToMime[newFormat]  # 'image/jpeg'

                newData = Types.Data()
                newData.size = newLen
                newData.bodyHash = newMd5Bytes
                newData.body = newBytes

                newRes = Types.Resource()
                newRes.mime = newMime
                newRes.data = newData
                newRes.attributes = eachResource.attributes

                newResList.append(newRes)
            else:
                """
                    audio/wav
                    audio/mpeg
                    audio/amr
                    application/pdf
                    ...
                """
                newResList.append(eachResource)

        return newResList
Пример #3
0
def makeNote(authToken, noteStore, noteTitle, author_list, list_of_dicts):

    client = EvernoteClient(token=authToken, sandbox=False)

    noteStore = client.get_note_store()

    Errors = client.get_user_store()

    # open the image and take its md5_hash

    image = open("%s.png" % noteTitle, "rb").read()
    md5 = hashlib.md5()
    md5.update(image)
    image_hash = md5.hexdigest()

    # Assign the image content, length, and hash to a Data object.

    data = ttypes.Data()
    data.size = len(image)
    data.bodyHash = image_hash
    data.body = image

    # Create a new resource to hold the image.

    resource = ttypes.Resource()
    resource.mime = "image/png"
    resource.data = data

    # Create a resource list in which to put the resource created above.

    resource_list = []
    resource_list.append(resource)

    # Create note object

    readingNote = ttypes.Note()
    readingNote.title = noteTitle
    readingNote.notebookGuid = notebook_guid
    readingNote.resources = resource_list

    # Start filling in the note content, including a reference to the image that we added to the resources list above.

    nBody = '<?xml version="1.0" encoding="UTF-8"?>'
    nBody += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
    nBody += "<en-note>"
    nBody += "<div>"

    for list_item in author_list:
        row = ("author(s) = " + str(list_item))
        nBody += "<br>"
        nBody += row
        nBody += "</br>"

    for kindle_dict in list_of_dicts:

        dict_type = kindle_dict["type"]

        # Check to see if the dictionary is a note. If it is, make it blue.

        if dict_type == "note":
            row = (str(kindle_dict.get("content")) + " " + "(" +
                   str(kindle_dict.get("page_number")) + ", " +
                   str(kindle_dict.get("location_number")) + ")" + "\n")
            nBody += "<br>"
            nBody += '<span style="font-weight:bold;color:blue;">'
            nBody += row
            nBody += "</span>"
            nBody += "</br>"
        else:
            row = (str(kindle_dict.get("content")) + " " + "(" +
                   str(kindle_dict.get("page_number")) + ", " +
                   str(kindle_dict.get("location_number")) + ")" + "\n")
            nBody += "<br>"
            nBody += row
            nBody += "</br>"

    nBody += '<en-media type="image/png" hash="%s"/>' % image_hash

    nBody += "</div>"
    nBody += "</en-note>"

    # Create the note object.

    readingNote.content = nBody

    ## Attempt to create note in Evernote account

    try:
        note = noteStore.createNote(authToken, readingNote)
        return note

    except EDAMUserException as edue:
        print("EDAMUserException:", edue)
        return None

    except EDAMNotFoundException as ednfe:
        ## Parent Notebook GUID doesn't correspond to an actual notebook
        print("EDAMNotFoundException: Invalid parent notebook GUID", ednfe)
        return None
Пример #4
0
    def createNote(self,notebook,notename):
        isexist = False;  # 是否存在当天的笔记  目前认为不可能存在当天的笔记, 如果有一定要删除
        noteStore = Client_Production.client.get_note_store();
        NOTE_SUFFIX = '</en-note>';
        NOTE_HEADER = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">';
        f = NoteStore.NoteFilter()

        msg_content='<en-note>';
        #根据路径,得到txt
        notetxtpath=self.RootPath+'/'+notebook.name+'/'+notebook.name+notename+"/"+notebook.name+notename+'.txt'
        # notetxtpath=notetxtpath.encode()
        print(notetxtpath)
        if os.path.exists(notetxtpath.decode('utf-8')):
            txtobj=open(notetxtpath.decode('utf-8'),'rb');
            flag = 'attachmentFlag:'
            if txtobj:
                resources = []
                for line in open(notetxtpath.decode('utf-8')):
                    line = line.strip('\n')
                    isattach = flag in line
                    # 读取的内容,如果包含attachmentFlag: 截取后面的内容
                    if isattach:
                        flagPos = line.index(flag)
                        filename = line[flagPos + len(flag):]
                        data = Types.Data()
                        filepath = self.RootPath + '/' + notebook.name + '/' +  notebook.name+notename + filename;
                        #
                        filename = os.path.basename(filepath.encode('utf-8'))
                        data = Types.Data()
                        # try:
                        # 必须保证文件名在生成的时候是gbk, 在读取的时候也是gbk , 没有在linux测试过
                        # print(os.path.exists(filepath.decode('utf-8')))
                        # D:\wxhistory\2017 - 12 - 29\2017 - 12 - 29【联盟家长微课:郭宛灵微课\resources\mp4
                        # 因为抬头已经用utf-8作为编码,open要求传入的path必须是unicode
                        if os.path.exists(filepath.decode('utf-8')):
                            data.body = open(filepath.decode('utf-8'), 'rb').read()
                            data.size = len(data.body)
                            data.bodyHash = hashlib.md5(data.body).hexdigest()
                            resource = Types.Resource()
                            resource.mime = mimetypes.guess_type(filename)[0]
                            resource.data = data
                            attr = Types.ResourceAttributes()
                            attr.fileName = filename
                            resource.attributes = attr
                            hexhash = resource.data.bodyHash
                            # gif肯定是表情符号,限制大小以免影响到阅读
                            minetype = resource.mime
                            msg_content += line[0:flagPos] + '<br />'
                            if ('gif' in minetype):
                                msg_content += "<div style='max-width:20px;max-height:20px'><en-media  type=\"%s\" " \
                                               "hash=\"%s\" /><br /></div>" % \
                                               (resource.mime, hexhash);
                            else:
                                msg_content += "<en-media  type=\"%s\" hash=\"%s\" /><br " \
                                               "/>" % \
                                               (resource.mime, hexhash);
                            # 昵称 默认红色显示
                            resources.append(resource)

                    else:
                        # p = re.compile(r'([\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f])')
                        result, number =re.subn("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "",line)
                        # msg_content += line.decode().encode() + '<br />'
                        msg_content += result + '<br />'
            note = Types.Note();
            note.notebookGuid = notebook.guid
            note.title = notename.encode()
            note.content = NOTE_HEADER;
            #过滤非法字符
            print("过滤前:");
            print(msg_content)
            msg_content=re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+",u"",msg_content);
            errhref='<a href=""></a>';
            msg_content=msg_content.replace(errhref,'#爬虫过滤错误信息#')
            note.content += msg_content.encode('utf-8')
            note.content += NOTE_SUFFIX
            note.resources = resources
            print("过滤后:")
            print(note.content)
            print('将要创建' +notebook.name+'//'+ notename)
            note = noteStore.createNote(Client_Production.token, note);
            print('创建完成')
        else:
            return
Пример #5
0
    def savePicture(
        cls,
        filename,
    ):
        '''
        Save the picture to evernote
        Returns:
          STATUS_SAVE_OK                    : The picture is saved to evernote
          STATUS_UNSUPPORTED_FILE_TYPE      : The file type if not supported
          STATUS_SAVE_ERROR                 : Error saving picture to Evernote
          STATUS_SAVE_ERROR_NOTEBOOK_DELETED: Target notebook is deleted
          STATUS_SAVE_ERROR_AUTH_EXPIRED    : auth expired
        '''
        logging.debug(str(filename))
        extension = filename.split('.')[-1]
        if extension.upper() not in cls.FILE_TYPE:
            logging.debug('Unsupported file type : %s' % filename)
            return cls.STATUS_UNSUPPORTED_FILE_TYPE
        # auth_token = KeyRing.get_auth_token()
        # logging.debug('auth_token = '+str(auth_token))
        # if auth_token is None:
        # return False
        # logging.debug('')
        # client = EvernoteClient(token=auth_token)
        # try:
        #     noteStore = cls.client.get_note_store()
        # except Exception, e:
        #     logging.error(str(e))
        #     logging.error('Auth token is not correct')
        #     return False

        note = Types.Note()
        # if notebook_guid :
        note.notebookGuid = cls.notebook_guid
        note.title = os.path.basename(filename)
        note.content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
        note.content += '<en-note><br/>'

        # Calculate the md5 hash of the png
        md5 = hashlib.md5()
        with open(filename, 'rb') as f:
            png_bytes = f.read()
        md5.update(png_bytes)
        md5hash = md5.hexdigest()

        # Create the Data type for evernote that goes into a resource
        png_data = Types.Data()
        png_data.bodyHash = md5hash
        png_data.size = len(png_bytes)
        png_data.body = png_bytes

        # Add a link in the evernote boy for this content
        #<en-media width="640" height="480" type="image/jpeg" hash="f03c1c2d96bc67eda02968c8b5af9008"/>
        link = '<en-media type="image/png" hash="%s"/>' % md5hash
        note.content += link
        note.content += '</en-note>'

        # Create a resource for the note that contains the png
        png_resource = Types.Resource()
        png_resource.data = png_data
        png_resource.mime = "application/png"

        # Create a resource list to hold the png resource
        resource_list = []
        resource_list.append(png_resource)

        # Set the note's resource list
        note.resources = resource_list
        try:
            note = cls.note_store.createNote(note)
        except ErrorTypes.EDAMNotFoundException:
            # the target notenook is deleted
            logging.error('The target notenook is deleted')
            return cls.STATUS_SAVE_ERROR_NOTEBOOK_DELETED

        # TODO: auth expired

        logging.debug('Saving to Evernote Done, file = %s' % filename)
        return cls.STATUS_SAVE_OK
Пример #6
0
def main():
	""" GET: gets random gif from giphy and displays it along with the option to see another gif and to 
	save the gif to their evernote account"""
	if request.method == "GET": 
		if "access_token" in session.keys():
			#get random gif from giphy api
			response=requests.get("http://api.giphy.com/v1/gifs/random?api_key="+giphy_api_key).json()
			if not response:
				return render_template("error.html", error_message="error with connection to giphy")

			#get random image url and id from giphy api response
			giphy_url=response['data']['image_url']
			giphy_id=response['data']['id']

			#get tags and pass them to the page because the giphy api only show tags for random images
			giphy_tags=''
			try:
				response['data']['tags']
				for tag in response['data']['tags']:
					giphy_tags+=tag+', '
				giphy_tags=giphy_tags[:-2]
			except KeyError:
				pass

			return render_template("index.html", giphy_url=giphy_url, giphy_id=giphy_id, giphy_tags=giphy_tags) 
			session["access_token"]
		
		#if their Evernote access_token session varible is not set redirect them to Evernote to authoirze the applicaiton
		else:
			client = EvernoteClient(
				consumer_key=CONSUMER_KEY,
				consumer_secret=CONSUMER_SECRET,
				sandbox= True
				)
			try:
				request_token = client.get_request_token("http://localhost:8080/auth")
				session['oauth_token'] = request_token['oauth_token'] 
				session['oauth_token_secret'] = request_token['oauth_token_secret']
				authorize_url = client.get_authorize_url(request_token)
			except KeyError:
				return render_template("error.html", error_message="invalid API key and/or secret.  Please check the values of cosumer_key and sonsumer_secret in the server.py file are valid and <a href=\'/clear'>click here</a> to reset.")
			else:
				print authorize_url
				return redirect(authorize_url+"&suggestedNotebookName=Giphy") #suggest notebook name of giphy to user

		#if we do have the access token proceed to show them a gif they can save to Evernote
		
		
	"""POST: shows confomation of evernote gif save and presents option 
	to return to main page or see the note in evernote"""
	if request.method == 'POST':
		if request.form['giphy_id']:
			#get giphy_id from post request that was to be saved
			giphy_id=request.form['giphy_id']
			giphy_tags=request.form['giphy_tags']
			response=requests.get("http://api.giphy.com/v1/gifs/"+giphy_id+"?api_key="+giphy_api_key).json()
			giphy_url=response['data']['images']['original']['url']

			#generate Evernote client
			client = EvernoteClient(token=session["access_token"], sandbox=True)
			user_store = client.get_user_store()
			note_store = client.get_note_store()
			notebooks = note_store.listNotebooks()

			notebooks_dict=dict()
			for notebook in notebooks:
				notebooks_dict[notebook.name]=notebook.guid

			#if app notebook key use that notebok to save notes into
			if len(notebooks)==1 and notebooks[0].defaultNotebook==False: #assume app notebok key
				giphyNotebookGuid=notebooks[0].guid
			elif "Giphy" in notebooks_dict.keys(): #if notebook named Giphy exists use that notebook
				giphyNotebookGuid=notebooks_dict['Giphy']
			else: #make new notebook
				try:
					notebook=Types.Notebook()
					notebook.name="Giphy"
					notebook=note_store.createNotebook(notebook)
					giphyNotebookGuid=notebook.guid
				except EDAMUserException: #single app notebok key
					giphyNotebookGuid=notebooks[0].guid

			#create note title with user name + giphy id for unique identifier
			note_title=response['data']['username']+"-"+response['data']['id']

			#check to see if note exists already
			notebook_filter=NoteStoreTypes.NoteFilter()
			notebook_filter.guid=giphyNotebookGuid
			result_spec = NotesMetadataResultSpec(includeTitle=True)
			try:
				noteList    = note_store.findNotesMetadata(session["access_token"], notebook_filter,0 , 40000, result_spec)
				for note in noteList.notes:
					if note.title==note_title:
						shardId=user_store.getUser(session["access_token"]).shardId
						shareKey=note_store.shareNote(session["access_token"], note.guid)
						evernote_url="%s/shard/%s/sh/%s/%s" % (EN_URL,shardId,note.guid,shareKey)
						return render_template("already_there.html", giphy_url=giphy_url, evernote_url=evernote_url)
			except EDAMUserException: #if the key doesn't have read permissions just move on
				pass
			
			
			#get image
			image= requests.get(giphy_url, stream=True).content
			md5 = hashlib.md5()
			md5.update(image)
			gif_hash = md5.digest()

			data = Types.Data()
			data.size = len(image)
			data.bodyHash = gif_hash
			data.body = image

			resource = Types.Resource()
			resource.mime = 'image/gif'
			resource.data = data

			hash_hex = binascii.hexlify(gif_hash)

			
			note = Types.Note()
			note.notebookGuid=giphyNotebookGuid #create note for our Giphy notebook
			
			note.title=note_title #name based on Giphy username and id
			note.content = '<?xml version="1.0" encoding="UTF-8"?>'
			note.content += '<!DOCTYPE en-note SYSTEM ' \
			    '"http://xml.evernote.com/pub/enml2.dtd">'
			note.content += '<en-note><br/>'
			note.content += '<en-media type="image/gif" hash="' + hash_hex + '"/>'
			note.content += '</en-note>'

			#add tags to the note
			enTagList=note_store.listTags()
			enTagListNames= [tag.name for tag in enTagList]
			giphyTagList=giphy_tags.split(", ")

			if not note.tagGuids:
				note.tagGuids=[]

			for giphyTag in giphyTagList:
				if giphyTag in enTagListNames:
					for tag in enTagList:
						if tag.name == giphyTag:
							note.tagGuids.append(tag.guid)
				elif giphyTag=='':
					continue
				else:
					tag=Types.Tag()
					tag.name=giphyTag
					tag=note_store.createTag(tag)

					note.tagGuids.append(tag.guid)


			note.resources = [resource] # Now, add the new Resource to the note's list of resources

			note=note_store.createNote(note) # create the note
			
			user=user_store.getUser(session["access_token"])
			shardId=user.shardId
			
			try:
				shareKey=note_store.shareNote(session["access_token"], note.guid)
				evernote_url="%s/shard/%s/sh/%s/%s" % (EN_URL,shardId,note.guid,shareKey)
			except EDAMUserException:
				evernote_url=EN_URL + "/Home.action"
			return render_template("saved.html", giphy_url=giphy_url, evernote_url=evernote_url)
		else:
			return render_template("error.html", error_message="Error finding the GIF")

	else:
		return render_template("error.html", error_message="Unsuported HTTP method.  Please use GET or POST.")
Пример #7
0
def evernote(req):
    imgurl = req.GET['imgurl']
    title = req.GET['title']
    title = "트위터, 140문자가 세상을 바꾼다"
    auth_token = "S=s1:U=6bc25:E=146546e6e47:C=13efcbd4249:P=1cd:A=en-devtoken:V=2:H=eafbf063b51f5fef898e8cc1add8a3c6"

    if auth_token == "your developer token":
        print "Please fill in your developer token"
        print "To get a developer token, visit " \
            "https://sandbox.evernote.com/api/DeveloperToken.action"
        exit(1)

    # Initial development is performed on our sandbox server. To use the production
    # service, change sandbox=False and replace your
    # developer token above with a token from
    # https://www.evernote.com/api/DeveloperToken.action
    client = EvernoteClient(token=auth_token, sandbox=True)

    user_store = client.get_user_store()

    version_ok = user_store.checkVersion("Evernote EDAMTest (Python)",
                                         UserStoreConstants.EDAM_VERSION_MAJOR,
                                         UserStoreConstants.EDAM_VERSION_MINOR)
    print "Is my Evernote API version up to date? ", str(version_ok)
    print ""
    if not version_ok:
        exit(1)

    note_store = client.get_note_store()

    # List all of the notebooks in the user's account
    notebooks = note_store.listNotebooks()
    print "Found ", len(notebooks), " notebooks:"
    for notebook in notebooks:
        print "  * ", notebook.name

    print
    print "Creating a new note in the default notebook"
    print

    # To create a new note, simply create a new Note object and fill in
    # attributes such as the note's title.
    note = Types.Note()
    note.title = title

    # To include an attachment such as an image in a note, first create a Resource
    # for the attachment. At a minimum, the Resource contains the binary attachment
    # data, an MD5 hash of the binary data, and the attachment MIME type.
    # It can also include attributes such as filename and location.
    image = open(
        '/Users/loading/Dropbox/project/python/UsedBookSite/BookSite/book2.png',
        'rb').read()
    md5 = hashlib.md5()
    md5.update(image)
    hash = md5.digest()

    data = Types.Data()
    data.size = len(image)
    data.bodyHash = hash
    data.body = image

    resource = Types.Resource()
    resource.mime = 'image/png'
    resource.data = data

    # Now, add the new Resource to the note's list of resources
    note.resources = [resource]

    # To display the Resource as part of the note's content, include an <en-media>
    # tag in the note's ENML content. The en-media tag identifies the corresponding
    # Resource using the MD5 hash.
    hash_hex = binascii.hexlify(hash)

    # The content of an Evernote note is represented using Evernote Markup Language
    # (ENML). The full ENML specification can be found in the Evernote API Overview
    # at http://dev.evernote.com/documentation/cloud/chapters/ENML.php
    note.content = '<?xml version="1.0" encoding="UTF-8"?>'
    note.content += '<!DOCTYPE en-note SYSTEM ' \
        '"http://xml.evernote.com/pub/enml2.dtd">'
    note.content += '<en-note>' + title + '<br/>'
    note.content += '<en-media type="image/png" hash="' + hash_hex + '"/>'
    note.content += '</en-note>'

    # Finally, send the new note to Evernote using the createNote method
    # The new Note object that is returned will contain server-generated
    # attributes such as the new note's unique GUID.
    created_note = note_store.createNote(note)

    print "Successfully created a new note with GUID: ", created_note.guid
    return HttpResponse("ok")
Пример #8
0
def main():
    global StartPath, authData, authorize_url, token_url, auth_token
    # 1
    StartPath = getCurrentPath()
    if not path.isfile(StartPath + 'authorize.txt'):
        #model template (for new user) with interactive instructions
        persInfo = {}
        persInfo["shoeboxed"] = {
            "ID": "your_ID",
            "Account_ID": "auto_Account_ID",
            "Secret": "your_Secret",
            "redirect_uri": "your_redirect_uri",
            "state": "random_CSRFkey",
            "access_token": "auto_access_token",
            "refresh_token": "auto_refresh_token",
            "code": "code_from_url_here"
        }
        persInfo["evernote"] = {"auth_token": "your_auth_token"}

        #SHOEBOXED interactive instructions
        print(
            "Open the link below and create a new appp:\n\nhttps://app.shoeboxed.com/member/v2/user-settings#api"
            + "\n\nPaste 'ID' below and press Enter:")
        inp = raw_input()
        persInfo['shoeboxed']['ID'] = inp
        print("Paste 'Secret' below and press Enter:")
        inp = raw_input()
        persInfo['shoeboxed']['Secret'] = inp
        print("Paste 'redirect_uri' below and press Enter:")
        inp = raw_input()
        persInfo['shoeboxed']['redirect_uri'] = inp
        print(
            "\nOpen next link\n\n" + str(returnAuthURL(persInfo)) +
            "\n\nAuthorize to shoeboxed and allow access." +
            "You'll be redirected to 'redirect_uri'. Click on address line - there will be something like this: http://rezoh.ru/?code="
            +
            "933bed8f-cdc8-4628-8ce2-b51c976ed223&state=random_CSRF_key\n\nand paste 'code' value below and press enter:"
        )
        inp = raw_input()
        persInfo['shoeboxed']['code'] = inp

        #EVERNOTE interactive
        print("\nNow Evernote. Visit and create token \nhttps://www.evernote.com/api/DeveloperToken.action"\
              "\n\nPaste it below and press Enter:")
        inp = raw_input()
        persInfo['evernote']['auth_token'] = inp

        #save auth data
        authorize = open(StartPath + 'authorize.txt', 'w+')
        authorize.write(json.dumps(persInfo))
        authorize.close()
    authData = readAuthDataFromFile()

    if not path.isfile(StartPath + 'indexFile.txt'):
        indexFile = open(StartPath + 'indexFile.txt', 'w+')
        indexFile.write(json.dumps({'IDs': [], 'offset': 0}))
        indexFile.close()
        print "--Empty indexFile (JSON with IDs of Receipts) was created"
    ids = readIDsFromFile()

    if not path.isfile(StartPath + 'Num.txt'):
        print "--Error. No Num file was found. Enter Num (integer only, -1 meals all Recieps) below to create Num file:"
        inp = raw_input()
        NumFile = open(StartPath + 'Num.txt', 'w+')
        try:
            inp = int(inp)
            NumFile.write('Num=' + str(inp) + ';')
        except BaseException as ex:
            print 'Error 1:', ex, '\nNum file created with Num=-1, you can change it manually'
            NumFile.write('Num=-1;')
        NumFile.close()
    Num = readNumFromFile()

    # 2
    print '--Auth to SHOEBOXED'
    if authData['shoeboxed']['access_token'] == 'auto_access_token':
        #means no token
        print 'Creating access_token'
        r = obtainAccessToken()
        try:
            authData['shoeboxed']['access_token'] = r['access_token']
            authData['shoeboxed']['refresh_token'] = r['refresh_token']
        except BaseException as ex:
            print 'Error 2:', str(ex), ', trying to renew token.\n'
            r = refreshAccessToken()
            try:
                authData['shoeboxed']['access_token'] = r['access_token']
            except BaseException as exc:
                print 'Error 3:', str(exc), ', new r =', r, '\n'

    #SAVE AUTHDATA
    authorize = open(StartPath + 'authorize.txt', 'w+')
    authorize.write(json.dumps(authData))
    authorize.close()

    sUserInfo = callSAPI2()
    if sUserInfo.status_code in [401, 404]:
        print '\nrefreshing token'
        r = refreshAccessToken()
        try:
            authData['shoeboxed']['access_token'] = r['access_token']
            sUserInfo = json.loads(callSAPI2().text)
            authData['shoeboxed']['Account_ID'] = sUserInfo['accounts'][0][
                'id']
        except BaseException as exc:
            print 'Error 4:', str(exc), ', new r =', r,
            print '\n\nIncorrect data. Delete authorize.txt and restart script!'
            sys.exit(2)
    else:
        sUserInfo = json.loads(sUserInfo.text)
        print '\nsUserInfo =', sUserInfo, '\n'
        authData['shoeboxed']['Account_ID'] = sUserInfo['accounts'][0]['id']

    # 3
    print "\n--Auth to EVERNOTE"
    try:
        client = EvernoteClient(token=authData['evernote']['auth_token'],
                                sandbox=False)
    except BaseException as ex:
        print "\nError message :", ex
        print "\nIf token expired, visit \nhttps://www.evernote.com/api/DeveloperToken.action"\
              "\n\nPaste it below and press Enter:"
        inp = raw_input()
        authData['evernote']['auth_token'] = inp
        client = EvernoteClient(token=authData['evernote']['auth_token'],
                                sandbox=False)

    user_store = client.get_user_store()
    version_ok = user_store.checkVersion("Evernote EDAMTest (Python)",
                                         UserStoreConstants.EDAM_VERSION_MAJOR,
                                         UserStoreConstants.EDAM_VERSION_MINOR)

    # find notebook
    notebookTargetName = "Shoeboxed"
    targetNotebook = Types.Notebook()

    note_store = client.get_note_store()
    notebooks = []
    for notebook in note_store.listNotebooks():
        notebooks.append(notebook.name)
        if notebook.name == notebookTargetName:
            targetNotebook = notebook
    # or create if no
    if notebookTargetName not in notebooks:
        targetNotebook.name = notebookTargetName
        targetNotebook = note_store.createNotebook(targetNotebook)
        print "Notebook named " + notebookTargetName + " was created"

    notebookGUID = targetNotebook.guid
    print 'notebookGUID =', notebookGUID

    # ?
    S = [
        '1 - OTHER IN QB', '1 - RECEIPT IN QB',
        '1 - STATEMENT RECONCILED IN QB', 'Manoj - Ask Richard',
        'Manoj - Duplicate', 'Manoj - Entered', 'Richard', 'Richard Responded'
    ]
    s = []
    for t in S:
        s.append(t.lower().replace(' ', ''))

    # process starts here

    uncreatedNotes = []
    data = callSAPI(offset=0, limit=1)
    try:
        json_data = json.loads(data.text)
    except BaseException as ex:
        r = refreshAccessToken()
        try:
            authData['shoeboxed']['access_token'] = r['access_token']
            data = callSAPI(offset=0, limit=1)
            json_data = json.loads(data.text)
        except BaseException as exc:
            print 'Error 3:', str(exc), '\n\nRESTART ONLY'

    totalCount = json_data['totalCountFiltered']
    print '\n--total count of Receipts in Shoeboxed acc', totalCount

    i = 1
    offset = ids['offset']
    if Num == -1:
        Num = totalCount - offset
        print '\n--Left to sync =', Num  # absolute number to complete
    else:
        print '\n--Num to sync =', Num  # required number

    print '\n--offset now =', offset, '\n'
    for step in range(totalCount - offset):
        data = callSAPI(offset=step + offset, limit=1)
        try:
            json_data = json.loads(data.text)
            if data.status_code in [401, 404]:
                r = refreshAccessToken()
                try:
                    authData['shoeboxed']['access_token'] = r['access_token']
                    data = callSAPI(offset=step + offset, limit=1)
                    json_data = json.loads(data.text)
                except BaseException as exc:
                    print 'Error 7:', str(exc), '\n\nRESTART ONLY'
            oneReceipt = json_data['documents'][0]
        except BaseException as ex:
            print 'Error 6:', str(ex), '\n\nRESTART ONLY'

        if oneReceipt['id'] not in ids['IDs']:
            print '-Receipt #', i, '\n', oneReceipt
            # creating basic Note object
            note = Types.Note()
            note.notebookGuid = notebookGUID

            # deletes all spaces at the end of the word if word exists
            try:
                st = oneReceipt['vendor']
                c = len(oneReceipt['vendor']) - 1
                while c > 0:
                    if st[c] == " ":
                        st = st[:c]
                        c -= 1
                    else:
                        break
                oneReceipt['vendor'] = st
                del (st, c)
            except:
                pass

            # add title
            try:
                note.title = oneReceipt['issued'][:oneReceipt['issued'].find(
                    'T')] + ' - ' + oneReceipt['vendor']
            except:
                try:
                    note.title = 'none - ' + oneReceipt['vendor']
                except:
                    try:
                        note.title = oneReceipt['issued'][:oneReceipt['issued']
                                                          .find('T'
                                                                )] + ' - none'
                    except:
                        note.title = 'none - none'

            # add times
            try:
                t = oneReceipt['uploaded'][:oneReceipt['uploaded'].find('T')]
                t = time.mktime(time.strptime(t, "%Y-%m-%d")) * 1000
                note.created = t
            except:
                print 'No uploaded time'

            try:
                t = oneReceipt['modified'][:oneReceipt['modified'].find('T')]
                t = time.mktime(time.strptime(t, "%Y-%m-%d")) * 1000
                note.updated = t
            except:
                print 'No modified time'

            attachment = ""
            try:
                # downloading pdf by link and formating
                imageURL = oneReceipt['attachment']['url']
                image = urllib.urlopen(imageURL, proxies={}).read()
                md5 = hashlib.md5()
                md5.update(image)
                hash = md5.digest()

                # uploading to evernote
                data = Types.Data()
                data.size = len(image)
                data.bodyHash = hash
                data.body = image

                resource = Types.Resource()
                resource.mime = 'application/pdf'
                resource.data = data
                note.resources = [resource]

                hash_hex = binascii.hexlify(hash)
                attachment = "yes"
            except:
                print "Warning: No attachment"
                attachment = "no"

            # creating Tag object from Categories
            try:
                tags = [
                    'Shoeboxed', 'Shoeboxed ' + oneReceipt['source']['type']
                ]
            except:
                tags = ['Shoeboxed']

            try:
                if oneReceipt['source']['type'] == 'mail':
                    tags.append('G:' + oneReceipt['source']['envelope'])
            except:
                pass
                #will be no G-tags

            try:
                for tag in oneReceipt['categories']:
                    if tag.replace('&',
                                   '&amp;').encode('utf-8').lower().replace(
                                       ' ', '') in s:
                        tags.append('S:' +
                                    tag.replace('&', '&amp;').encode('utf-8'))
                    else:
                        tags.append('T:' +
                                    tag.replace('&', '&amp;').encode('utf-8'))
            except:
                pass
                #will be no tags at all

            note.tagNames = tags

            # representing Data
            note.content = '<?xml version="1.0" encoding="UTF-8"?>'
            note.content += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'\
                            '<en-note>'

            try:
                note.content += '<h2>'+oneReceipt['issued'][:oneReceipt['issued'].find('T')]+' - '+\
                                oneReceipt['vendor'].replace('&', '&amp;')+'</h2><table bgcolor="#F0F0F0" border="0" width="60%">'
            except:
                try:
                    note.content += '<h2>none - ' + oneReceipt['vendor'].replace(
                        '&', '&amp;'
                    ) + '</h2><table bgcolor="#F0F0F0" border="0" width="60%">'
                except:
                    try:
                        note.content += '<h2>' + oneReceipt[
                            'issued'][:oneReceipt['issued'].find(
                                'T'
                            )] + ' - none</h2><table bgcolor="#F0F0F0" border="0" width="60%">'
                    except:
                        note.content += '<h2>none - none</h2><table bgcolor="#F0F0F0" border="0" width="60%">'
                        print 'No ussued and no vendor name'

            try:
                note.content += makeTableRow(
                    'Receipt Date',
                    oneReceipt['issued'][:oneReceipt['issued'].find('T')])
            except:
                note.content += makeTableRow('Receipt Date', 'none')

            try:
                note.content += makeTableRow('Receipt Total',
                                             str(oneReceipt['total']))
            except:
                note.content += makeTableRow('Receipt Total', 'none')

            try:
                note.content += makeTableRow('Receipt Tax',
                                             str(oneReceipt['tax']))
            except:
                note.content += makeTableRow('Receipt Tax', 'none')

            try:
                note.content += makeTableRow('Receipt Currency',
                                             oneReceipt['currency'])
            except:
                note.content += makeTableRow('Receipt Currency', 'none')

            try:
                dig = int(oneReceipt['paymentType']['lastFourDigits'])
                note.content += makeTableDoubleRow(['Payment', 'Type'], [
                    oneReceipt['paymentType']['type'],
                    '**** **** **** ' + str(dig)
                ])
            except:
                try:
                    note.content += makeTableDoubleRow(['Payment', 'Type'], [
                        oneReceipt['paymentType']['type'],
                        '**** **** **** none'
                    ])
                except:
                    note.content += makeTableRow('Payment Type', 'none')

            try:
                note.content += makeTableRow(
                    'Notes', oneReceipt['notes'].replace('&', '&amp;'))
            except:
                note.content += makeTableRow('Notes', 'none')

            note.content += makeTableRow('&nbsp;', '&nbsp;')

            try:
                note.content += makeTableRow('Document ID', oneReceipt['id'])
            except:
                note.content += makeTableRow('Document ID',
                                             'error document ID, none')

            try:
                note.content += makeTableRow('Envelope ID',
                                             oneReceipt['source']['envelope'])
            except:
                pass

            try:
                note.content += makeTableRow(
                    'Date Uploaded',
                    oneReceipt['uploaded'][:oneReceipt['uploaded'].find('T')])
            except:
                note.content += makeTableRow('Date Uploaded', 'none')

            try:
                note.content += makeTableRow(
                    'Date Modified',
                    oneReceipt['modified'][:oneReceipt['modified'].find('T')])
            except:
                note.content += makeTableRow('Date Modified', 'none')

            try:
                note.content += makeTableRow('Invoice Number',
                                             oneReceipt['invoiceNumber'])
            except:
                note.content += makeTableRow('Invoice Number', 'none')

            try:
                note.content += makeTableRow(
                    'Total in Preferred Currency',
                    str(oneReceipt['totalInPreferredCurrency']))
            except:
                note.content += makeTableRow('Total in Preferred Currency',
                                             'none')

            try:
                note.content += makeTableRow(
                    'Tax in Preferred Currency',
                    str(oneReceipt['taxInPreferredCurrency']))
            except:
                note.content += makeTableRow('Tax in Preferred Currency',
                                             'none')

            try:
                note.content += makeTableRow('Trashed?',
                                             str(oneReceipt['trashed']))
            except:
                note.content += makeTableRow('Trashed?', 'none')

            try:
                note.content += makeTableRow('Document source',
                                             oneReceipt['source']['type'])
            except:
                note.content += makeTableRow('Document source', 'none')

            if attachment == "yes":
                note.content += '</table><br/><br/><en-media type="application/pdf" hash="' + hash_hex + '"/>'
            else:
                note.content += '</table><br/><br/>'  #if no attachment
            note.content += '</en-note>'

            try:
                created_note = note_store.createNote(note)
                #SAVE document ID every step - its safer
                ids['IDs'].append(oneReceipt['id'])
                ids['offset'] = i + offset
                i += 1
                indexFile = open(StartPath + 'indexFile.txt', 'w+')
                indexFile.write(json.dumps(ids))
                indexFile.close()
            except BaseException as ex:
                print "\nError creating note =", str(
                    ex), "\nreceipt id =", oneReceipt['id'], "\n"
                uncreatedNotes.append(oneReceipt['id'])
            print ""

        #don't move this statement below!
        if i > Num:
            break

    print '\nAll added notes ids:\n', ids['IDs']
    if len(uncreatedNotes) > 0:
        print '\nError occured in notes ids:\n', uncreatedNotes
    #SAVE AUTHDATA
    authorize = open(StartPath + 'authorize.txt', 'w+')
    authorize.write(json.dumps(authData))
    authorize.close()
Пример #9
0
 def open_note(self,
               guid,
               insert_in_content=True,
               filename=None,
               prompt=False,
               **unk_args):
     import hashlib, mimetypes
     if filename is None:
         view = self.window.active_view()
         if view is None:
             sublime.error_message(
                 "Evernote plugin could not open the file you specified!")
             return
         filename = view.file_name() or ""
         contents = view.substr(sublime.Region(0,
                                               view.size())).encode('utf8')
     else:
         filename = os.path.abspath(filename)
         if prompt:
             self.window.show_input_panel(
                 "Filename of attachment: ", filename,
                 lambda x: self.open_note(guid,
                                          insert_in_content,
                                          filename,
                                          prompt=False,
                                          **unk_args), None, None)
             return
         try:
             with open(filename, 'rb') as content_file:
                 contents = content_file.read()
         except Exception as e:
             sublime.error_message(
                 "Evernote plugin could not open the file you specified!")
             print(e)
             return
     try:
         noteStore = self.get_note_store()
         note = noteStore.getNote(self.token(), guid, True, False, False,
                                  False)
         mime = mimetypes.guess_type(filename)[0]
         LOG(mime)
         h = hashlib.md5(contents)
         if not isinstance(mime, str):
             mime = "text/plain"
         attachment = Types.Resource(
             # noteGuid=guid,
             mime=mime,
             data=Types.Data(body=contents,
                             size=len(contents),
                             bodyHash=h.digest()),
             attributes=Types.ResourceAttributes(
                 fileName=os.path.basename(filename), attachment=True))
         resources = note.resources or []
         resources.append(attachment)
         if insert_in_content and note.content.endswith(
                 "</en-note>"):  # just a precaution
             builtin = note.content.find(SUBLIME_EVERNOTE_COMMENT_BEG, 0,
                                         150)
             if builtin >= 0:
                 builtin_end = note.content.find(
                     SUBLIME_EVERNOTE_COMMENT_END, builtin)
                 content = note.content[0:builtin] + note.content[
                     builtin_end + len(SUBLIME_EVERNOTE_COMMENT_END) + 1:]
             else:
                 content = note.content
             note.content = content[0:-10] + \
                 '<en-media hash="%s" type="%s"/></en-note>' % (h.hexdigest(), mime)
         note.resources = resources
         noteStore.updateNote(self.token(), note)
         self.message("Succesfully attached to note '%s'" % note.title)
     except Errors.EDAMNotFoundException as e:
         sublime.error_message(
             "The note with the specified guid could not be found.")
     except Errors.EDAMUserException:
         sublime.error_message(
             "The specified note could not be found.\nPlease check the guid is correct."
         )
Пример #10
0
def cmd_add(args):
    """add [[:tag1] [:tag2] ...]
       [+<notebook>]
       <title>
       [resource1] [resource2] ..
       < <content>
    add a new note
       with the specified tags (or none if unspecified)
       in the specified notebook (or your current notebook if unspecified)
       with the specified title (required)
       adding the specified files as resources to that note (or none if unspecified)
       and content from stdin
    """

    # Parse args
    tags = []
    title = None
    resource_names = []
    for arg in args:
        if arg.startswith('+'):
            set_current_notebook_name(arg[1:])
        elif arg.startswith(':'):
            tags.append(arg[1:])
        elif title is None:
            title = arg
        else:
            resource_names.append(arg)

    if title is None:
        print("A title must be specified.")
        raise SyntaxError

    print("making note titled '%s' with tags'%s' and resources named '%s'" %
          (title, repr(tags), repr(resource_names)))

    nb_guid = get_current_notebook().guid

    resources = []
    attachments = ""
    for filename in resource_names:
        resource = Types.Resource()
        resource.data = Types.Data()
        resource.data.body = open(filename, 'r').read()
        resource.attributes = Types.ResourceAttributes(fileName=filename,
                                                       attachment=False)
        mime = mimetypes.guess_type(filename)[0]
        resource.mime = mime or ''
        hash = hashlib.md5()
        hash.update(resource.data.body)
        attachments += '<en-media type="%s" hash="%s" />\n' % (
            resource.mime, hash.hexdigest())
        resources.append(resource)

    content = wrap_content(sys.stdin.read() + attachments)

    note = Types.Note(title=title,
                      content=content,
                      tagNames=tags,
                      resources=resources,
                      notebookGuid=nb_guid)
    note = _retry(lambda: get_note_store().createNote(note))

    print("Note created!")
Пример #11
0
    def do_run(self,
               edit,
               insert_in_content=True,
               filename=None,
               prompt=False):
        import hashlib, mimetypes
        view = self.view
        if filename is None or prompt:
            view.window().show_input_panel(
                "Filename or URL of attachment: ", filename or "",
                lambda x: view.run_command(
                    "evernote_insert_attachment", {
                        'insert_in_content': insert_in_content,
                        "filename": x,
                        "prompt": False
                    }), None, None)
            return
        filename = filename.strip()
        attr = {}
        try:
            if filename.startswith("http://") or \
               filename.startswith("https://"):
                # download
                import urllib.request
                response = urllib.request.urlopen(filename)
                filecontents = response.read()
                attr = {"sourceURL": filename}
            else:
                datafile = os.path.expanduser(filename)
                with open(datafile, 'rb') as content_file:
                    filecontents = content_file.read()
                attr = {"fileName": os.path.basename(datafile)}
        except Exception as e:
            sublime.error_message(
                "Evernote plugin has troubles locating the specified file/URL.\n"
                + explain_error(e))
            return

        try:
            guid = self.view.settings().get("$evernote_guid")
            noteStore = self.get_note_store()
            note = noteStore.getNote(self.token(), guid, False, False, False,
                                     False)
            mime = mimetypes.guess_type(
                filename)[0] or "application/octet-stream"
            h = hashlib.md5(filecontents)
            attachment = Types.Resource(
                # noteGuid=guid,
                mime=mime,
                data=Types.Data(body=filecontents,
                                size=len(filecontents),
                                bodyHash=h.digest()),
                attributes=Types.ResourceAttributes(
                    attachment=not insert_in_content, **attr))
            resources = note.resources or []
            resources.append(attachment)
            note.resources = resources
            self.message("Uploading attachment...")
            noteStore.updateNote(self.token(), note)
            if insert_in_content:
                view.insert(
                    edit,
                    view.sel()[0].a,
                    '<en-media type="%s" hash="%s"/>' % (mime, h.hexdigest()))
                sublime.set_timeout(
                    lambda: view.run_command("save_evernote_note"), 10)
        except Exception as e:
            sublime.error_message(
                "Evernote plugin cannot insert the attachment.\n" +
                explain_error(e))