Created
July 17, 2022 12:15
-
-
Save dima2306/bfb8bf85e68a62bc4ad70a56003e9c6a to your computer and use it in GitHub Desktop.
This file can import data into Notion. In this example, we import data from an HTML file exported from Firefox Pocket.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| declare(strict_types=1); | |
| /** | |
| * Created by PhpStorm. | |
| * User: dima23 | |
| * Date: 17.07.22 | |
| * Time: 13:55 | |
| */ | |
| /** | |
| * This file can import data into Notion. | |
| * In this example, we import data from an HTML file exported from Firefox Pocket. | |
| * If for some reason the process was interrupted, you can continue it by searching | |
| * for the specific criteria in the Notion database. See lines 43-45 and 48-50. | |
| * | |
| * Important note: | |
| * For import to work, you need to have the following settings in your Notion account: | |
| * 1. Create API token for the application. | |
| * 2. Share the <b>integration</b> with the application. | |
| * | |
| * @author Dimitri Simonishvili <[email protected]> | |
| */ | |
| require __DIR__.'/vendor/autoload.php'; | |
| use GuzzleHttp\Client; | |
| use Illuminate\Support\Carbon; | |
| $file = file_get_contents('public/ril_export.html'); | |
| $http = new Client(); | |
| $dom = new DOMDocument; | |
| $dom->loadHTML($file); | |
| $ass = $dom->getElementsByTagName('a'); | |
| $ass = iterator_to_array($ass->getIterator()); | |
| // Here we sort the array from oldest to newest based on date added. | |
| usort($ass, static function ($a, $b) { | |
| return $a->attributes->getNamedItem('time_added')->nodeValue <=> | |
| $b->attributes->getNamedItem('time_added')->nodeValue; | |
| }); | |
| //$find = array_filter($ass, function ($a) { | |
| // return strpos($a->nodeValue, 'Streaming HTTP response in PHP - turn long-running process into realtime UI') !== false; | |
| //}); | |
| foreach ($ass as $i => $as) { | |
| // if ($i <= 206) { | |
| // continue; | |
| // } | |
| /** @var DOMElement $as */ | |
| $href = $as->attributes->getNamedItem('href')->nodeValue; | |
| $formatted_tags = []; | |
| $tags = explode(',', $as->attributes->getNamedItem('tags')->nodeValue); | |
| if ($as->attributes->getNamedItem('tags')->nodeValue !== '') { | |
| foreach ($tags as $tag) { | |
| $formatted_tags[]['name'] = $tag; | |
| } | |
| } | |
| $title = $as->nodeValue; | |
| $date = Carbon::createFromTimestamp($as->attributes->getNamedItem('time_added')->nodeValue) | |
| ->timezone('Asia/Tbilisi') | |
| ->toIso8601String(); | |
| $response = $http->post('https://api.notion.com/v1/pages', [ | |
| 'headers' => [ | |
| 'Accept' => 'application/json', | |
| 'Content-Type' => 'application/json', | |
| 'Notion-Version' => '2022-06-28', | |
| 'Authorization' => 'Bearer {{INSERT_YOUR_TOKEN_HERE}}', | |
| ], | |
| 'json' => [ | |
| 'parent' => ['database_id' => '{{DATABASE_ID}}'], | |
| 'properties' => [ | |
| 'Name' => ['title' => [['text' => ['content' => $title]]]], | |
| 'Link' => ['url' => $href], | |
| 'Tags' => ['multi_select' => $formatted_tags], | |
| 'Date Added' => ['date' => ['start' => $date]], | |
| ], | |
| ], | |
| ]); | |
| dump($response->getBody()->getContents()); | |
| sleep(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment