Exemplo n.º 1
0
def test_uploadBlock():
    '''uploads a simple block to Notion using add_new'''
    #arrange
    blockDescriptor = {
        'type': TextBlock,
        'title': 'This is a test of the test system'
    }
    notionBlock = Mock()
    notionBlock.children.add_new = Mock()

    #act
    uploadBlock(blockDescriptor, notionBlock, '')

    #assert
    notionBlock.children.add_new.assert_called_with(TextBlock, title='This is a test of the test system')
Exemplo n.º 2
0
def test_uploadBlock_image_local_file_scheme_url_encoded():
    '''uploads an Image block with local image to Notion if it has a file:// scheme'''
    #arrange
    blockDescriptor = {
        'type': ImageBlock,
        'title': 'test',
        'source': 'file://TEST%20IMAGE%20HAS%20SPACES.png'
    }
    notionBlock = Mock()
    notionBlock.children.add_new.return_value = newBlock = Mock(spec=blockDescriptor['type'])

    #act
    uploadBlock(blockDescriptor, notionBlock, 'tests/DUMMY.md')

    #assert
    notionBlock.children.add_new.assert_called_with(ImageBlock, title='test', source='file://TEST%20IMAGE%20HAS%20SPACES.png')
    newBlock.upload_file.assert_called_with(str(Path('tests/TEST IMAGE HAS SPACES.png')))
Exemplo n.º 3
0
def test_uploadBlock_image_local():
    '''uploads an Image block with local image to Notion'''
    #arrange
    blockDescriptor = {
        'type': ImageBlock,
        'title': 'test',
        'source': 'TEST_IMAGE.png'
    }
    notionBlock = Mock()
    notionBlock.children.add_new.return_value = newBlock = Mock(spec=blockDescriptor['type'])

    #act
    uploadBlock(blockDescriptor, notionBlock, 'tests/DUMMY.md')

    #assert
    notionBlock.children.add_new.assert_called_with(ImageBlock, title='test', source='TEST_IMAGE.png')
    newBlock.upload_file.assert_called_with(str(Path('tests/TEST_IMAGE.png')))
Exemplo n.º 4
0
def test_uploadBlock_image():
    '''uploads an external image block to Notion without uploading'''
    #arrange
    blockDescriptor = {
        'type': ImageBlock,
        'title': 'test',
        'source': 'https://example.com'
    }
    notionBlock = Mock()
    newBlock = Mock(spec=blockDescriptor['type'])
    notionBlock.children.add_new = Mock(return_value=newBlock)

    #act
    uploadBlock(blockDescriptor, notionBlock, '')

    #assert
    notionBlock.children.add_new.assert_called_with(ImageBlock, title='test', source='https://example.com')
    newBlock.upload_file.assert_not_called()
Exemplo n.º 5
0
def test_uploadBlock_image_local_img_func():
    '''uploads an Image block with local image to Notion with a special transform'''
    #arrange
    blockDescriptor = {
        'type': ImageBlock,
        'title': 'test',
        'source': 'NONEXIST_IMAGE.png'
    }
    notionBlock = Mock()
    notionBlock.children.add_new.return_value = newBlock = Mock(spec=blockDescriptor['type'])
    imagePathFunc = Mock(return_value=Path('tests/TEST_IMAGE.png'))

    #act
    uploadBlock(blockDescriptor, notionBlock, 'tests/DUMMY.md', imagePathFunc=imagePathFunc)

    #assert
    imagePathFunc.assert_called_with('NONEXIST_IMAGE.png', 'tests/DUMMY.md')
    notionBlock.children.add_new.assert_called_with(ImageBlock, title='test', source='NONEXIST_IMAGE.png')
    newBlock.upload_file.assert_called_with(str(Path('tests/TEST_IMAGE.png')))
Exemplo n.º 6
0
def test_uploadBlock_collection():
    #arrange
    blockDescriptor = {
        'type': CollectionViewBlock,
        'schema': {
            'J=}2': {
                'type': 'text',
                'name': 'Awoooo'
            },
            'J=}x': {
                'type': 'text',
                'name': 'Awooo'
            },
            'title': {
                'type': 'text',
                'name': 'Awoo'
            }
        },
        'rows': [
            ['Test100', 'Test200', 'Test300'],
            ['', 'Test400', '']
        ]
    }
    schema = blockDescriptor['schema']
    rows = blockDescriptor['rows']
    notionBlock = Mock()
    newBlock = Mock(spec=blockDescriptor['type'])
    notionBlock.children.add_new = Mock(return_value=newBlock)

    collection = Mock()
    notionBlock._client.create_record = Mock(return_value=collection)
    notionBlock._client.get_collection = Mock(return_value=collection)

    #act
    uploadBlock(blockDescriptor, notionBlock, '')

    #assert
    notionBlock.children.add_new.assert_called_with(CollectionViewBlock)
    notionBlock._client.create_record.assert_called_with("collection", parent=newBlock, schema=schema)
    notionBlock._client.get_collection.assert_called_with(collection)
Exemplo n.º 7
0
            if 'activityName' in activity:
                row.activity = activity['activityName']
            if 'stepCount' in activity and activity['stepCount'] > 0:
                row.step_count = activity['stepCount']

        if 'stepCount' in entry:
            row.step_count = entry['stepCount']

        if 'weather' in entry:
            wx = entry['weather']
            if 'conditionsDescription' in wx:
                row.weather = wx['conditionsDescription']

        if 'creationDevice' in entry:
            row.device = entry['creationDevice']

        blocks = md2notion.convert(body)
        for block in blocks:
            if block['type'] is ImageBlock:
                block = moment_to_block(entry, block)
                if block is None: continue

            md2notion.uploadBlock(block, row, source_file)

        if args.raw:
            row.children.add_new(DividerBlock)
            row.children.add_new(CodeBlock,
                                 language='json',
                                 wrap=True,
                                 title=json.dumps(entry))
Exemplo n.º 8
0
def createNotionTask(token, collectionURL, content, url):
    def convertImagePath(imagePath, mdFilePath):
        parsed_url = urllib.parse.urlparse(url)
        domain = parsed_url.scheme + '://' + parsed_url.netloc

        relative_url = os.path.abspath(
            str(pathlib.Path().absolute()) + '/images/' + imagePath)
        new_url = urllib.parse.urljoin(domain, imagePath)
        r = http.request('GET', new_url)
        img = r.data

        os.makedirs(os.path.dirname(relative_url), exist_ok=True)
        with open(relative_url, 'wb') as f:
            f.write(img)

        return Path(
            os.path.abspath(str(pathlib.Path().absolute()) + imagePath))

    if (content):
        client = NotionClient(token)
        cv = client.get_collection_view(collectionURL)
        print(cv.collection.parent.views)
        row = cv.collection.add_row()

        if ('task:' in content):
            content = content.replace('task:', '')

        if ('Task:' in content):
            content = content.replace('Task:', '')

        row.title = content

        if (url and "http://ifttt.com/missing_link" not in url):
            expanded_url = urlexpander.expand(url)
            if ('imgur' in expanded_url):
                if 'gallery/' in expanded_url:
                    gallery = expanded_url.split('gallery/')[1]

                    client = ImgurClient(client_id, client_secret)
                    items = client.get_album_images(gallery)

                    imgur_object = ""
                    for item in items:
                        img = "<img src='" + item.link + "' /><br>"
                        imgur_object += img

                    text = prettierfier.prettify_html(imgur_object)
                    doc = Document(text)
                    text = doc.summary()

                    output = pypandoc.convert_text(text,
                                                   'gfm-raw_html',
                                                   format='html')

                    if (output == ""):
                        page = row.children.add_new(BookmarkBlock)
                        page.link = url
                        page.title = content
                    else:
                        rendered = convert(output)
                        for blockDescriptor in rendered:
                            uploadBlock(blockDescriptor,
                                        row,
                                        content,
                                        imagePathFunc=convertImagePath)
            else:
                # try:
                #     row.url = url
                #
                #     http = urllib3.PoolManager()
                #     r = http.request('GET', url)
                #
                #     text = prettierfier.prettify_html(str(r.data))
                #     soup = BeautifulSoup(str(r.data))
                #     metas = soup.find_all('meta')
                #     doc = Document(text)
                #     text = doc.summary()
                #     print(metas)
                #     output = pypandoc.convert_text(text, 'gfm-raw_html', format='html')
                #     output = output.replace('\\\\n', '')
                #     output = output.replace('\\\\t', '')
                #     output = output.replace("\\\\'", "\'")
                #     print(output)
                #
                #
                #     if (output == ""):
                #         print("wtf1")
                #         raise ValueError('No website data')
                #
                #     rendered = convert(output)
                #
                #     # Upload all the blocks
                #     for blockDescriptor in rendered:
                #         uploadBlock(blockDescriptor, row, doc.title(),imagePathFunc=convertImagePath)
                # except:
                page = row.children.add_new(BookmarkBlock)
                page.link = url
                page.title = content
        else:
            row.children.add_new(TextBlock, title=content)

        # shutil.rmtree(Path(str(pathlib.Path().absolute()) + '/images/'), ignore_error=True)
        return content
Exemplo n.º 9
0
            current_row.remove()
            error = False

        try:
            with open(path, 'r', encoding='utf-8') as mdFile:
                rendered = convert(mdFile)
                title = rendered.pop(0).get('title').split('\n', 1)

                row = current_row = cv.collection.add_row()
                row.date_created = datetime.fromtimestamp(
                    os.stat(os.path.dirname(path)).st_birthtime)
                row.name = title[0]

                if os.getenv('REPORT'):
                    row.report = os.getenv('REPORT')

                if len(title) == 2:
                    row.children.add_new(TextBlock, title=title[1])

                for blockDescriptor in rendered:
                    uploadBlock(blockDescriptor, row, mdFile.name)

        except HTTPError as err:
            print(f'Error: {err}')
            print(f'Retrying in {API_RETRY_DELAY} seconds...')
            time.sleep(API_RETRY_DELAY)
            error = True
            continue

        break