Skip to content

Commit

Permalink
chore: docs for python package
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Destructive authored Jul 23, 2022
1 parent c5fd245 commit dde085c
Showing 1 changed file with 96 additions and 57 deletions.
153 changes: 96 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,109 @@

> Crosspost your articles to dev.to/medium.com/hashnode.com from the command line
- Crossposter is a shellscript(BASH) to automate crossposting to platforms like dev.to, medium.com and hashnode.com. The script takes in markdown version of your post with a few inputs from you and posts it to those platforms. You would require a token/key for each of those platforms to post it from the command line.
- Crossposter is a script(Python/BASH) to automate crossposting to platforms like dev.to, medium.com and hashnode.com. The script takes in markdown version of your post with a few inputs from you and posts it to those platforms. You would require a token/key for each of those platforms to post it from the command line.

There are two versions of the script:

- [Shellscript](#crossposter-shell-script) (BASH)
- [Python](#crossposter-python-script)
- [Shellscript](#crossposter-shell-script) (BASH)


The actual script is still not perfect (has a few bugs). Though it posts on `dev.to` and `medium.com` easily, the `hashnode.com` is buggy as it parses the raw markdown into the post and doesn't render as desired. So, **its a under-development script**, fell free to raise any issues or PRs on the official GitHub repo.

## Crossposter Python Script

The python script initially is now a python package. You can essily install and configure it as per your requirements.

[![PyPI version](https://badge.fury.io/py/crossposter.svg)](https://pypi.org/project/crossposter/)

Dependencies for the python script:

- [requests](https://docs.python-requests.org/en/latest/user/install/#install)
- [pyyaml](https://pypi.org/project/PyYAML/)
- [python-frontmatter](https://pypi.org/project/python-frontmatter/)
- [rich](https://pypi.org/project/rich/)


Running the script:

Make sure you install the python package with pip or any other package manager like pipenv or poetry you would like:

```
pip install crossposter
# OR
pipenv install crossposter
# OR
poetry install crossposter
# OR
pipx install crossposter
```

### Configuration

- Create a config file `config.json` for customization
- Create a folder for the output files

Inside the config file, you can specify the two attributes for the working of the script. The first is for generating the `canonical_url` for the post and the next is the output folder for the generated files for different platforms.

```json
{
"blog_link": "https://www.meetgor.com/",
"output_folder": "crossout"
}

```

One it is installed, you can then specify the markdown file which you want to use as a post with:

```
crosspost <filename>.md
# OR with pipx
pipx run crossposter
```

This will prompt you for a few things, if not provided in the frontmatter of the command. There will be need of inputs like `title`, `description`, `tags`, `status`, `cover-image`, etc. If you provide it in the frontmatter, there will be no input prompt. Once the input is taken or the frontmatter is picked, it will set those frontmatter for various publications automatically.

```
# post to dev.to
crosspost <filename>.md --dev
# post to codenewbie.org
crosspost <filename>.md --cdb
# post to medium.com
crosspost <filename>.md --med
```

![image](https://user-images.githubusercontent.com/40317114/180605810-99a5616a-3524-4a10-97c4-e118763d9464.png)

![image](https://user-images.githubusercontent.com/40317114/180605864-cbb83793-1944-4bfb-b95f-ef609e3e48e5.png)

For further help on CLI command, you can get the help argument

```
crosspost --h
```

**ShellScript is no longer developed because of content parsing issues, it has been converted to a python package**

Though some basic functions will work in shell script like simple frontmatter, devto and medium posting.

## Crossposter Shell Script

The crossposter shell script is a file to be used from a linux/unix command line in a shell environment. The procedure to execute the file is as follows:
Expand Down Expand Up @@ -69,58 +163,3 @@ Lets see the script in action

If you want to add in more stuff to the post, you can check out the [DEV.to API docs](https://developers.forem.com/api#operation/createArticle) which is powered by [Forem](https://www.forem.com/), there a ton of options you can hook to the front-matter in the shellscript.

**NOTE: There is a limit of 10 requests per 30 seconds, so keep in mind while testing the script and don't try to spam**

### Posting on **hashnode.com**:

This part is still under development as it only displays the raw markdown in the post, also the `tags` are too heavy to implement from the API as `id` of every tag is required along with the `slug` and `name`. Still it serves some purpose at least. For posting on `hashnode.com`, we need `Personal Access Token`. This can be generated by going to [Developer Settings](https://hashnode.com/settings/developer). You will also require the user-id of your `hashnode` account. You can get your user-id/username from the [settings](https://hashnode.com/settings) tab under profile information. We require Username for posting to the Publication Blog if any. As usual, the `Personal Access Token` for interacting with the [Hashnodes' GraphQL API](https://api.hashnode.com/). The API is quite user friendly and provides everything in one place. There are docs for running each and every `query` and `mutations` present in the API.

You can paste the token when prompted from the script or manually type in the `keys.txt` text file as `hashnode:token` on the 4th line. Yes, that should be on the `4th` line, thats make retrieving much more easier and safe. Next also input in the `username` when the script asks for the input or again type in on the `5th` line, `hashnode_id:username` in the text file `keys.txt`. Please enter the credentials from the script prompt so as to avoid errors and misconfigurations when doing manually

This will create the Post on hashnode with the title, subtitle, cover image correctly but will mess up the content. I tried hard but its just not happening. There needs to be some character for newline as the API rejects the `rn` characters passed in, so I have substited them with `br` and the result is raw markdown. **As the Hashnode API is still under development and they are bringing changes and new features in, the API should improve in its core functionality and make it much easier for creating some common queries**. So, I'll create issue on GitHub for posting the actual content via the script.

So, this is the demonstration of posting on hashnode.

[hashnode](https://gitlab.com/MR_DESTRUCTIVE/tblog-img/-/raw/main/hashnode.gif)

### Posting on **medium.com**:

Medium API is much more versatile and markdown friendly, though it has some limitation on the number of posts you can make in a day. For posting on `Medium.com`, we will require the `Integration Token` which can be generated on the [settings tab](https://medium.com/me/settings). As similar to `hashnode`, you can name the token whatever you like and then get the token. Paste the token when prompted from the script or manually type in the `keys.txt` text file as `medium:token` on the `2nd` line. We also require the Medium_id, but we can get that from the token itself, so inside the script once the token is obtained, the curl command is executed to fetch in the `id` and it is stored on the next(`3rd`) line in the `keys.txt` file for actually posting on `medium.com`. So that is all the configuration you need for posting on `medium.com`.

There is some documentation on [Medium API](https://github.com/Medium/medium-api-docs), we can even post to a Publication, that shall be created in future. Also the cover images can be posted on medium, it is not currently done but that can again be a #TODO. **The tags are not rendered on Medium yet with the script.** The way we can parse strings is limited in BASH, so this might still be a doable thing later. Most of the checkboxes are ticked like title, subtitle, cover-image, canonical url, and importantly the content.

Let's look at post on medium from the script.

[medium](https://gitlab.com/MR_DESTRUCTIVE/tblog-img/-/raw/main/medium.gif)

### All platforms:

Now, once you have configured every thing, you can opt for the `4` choice that is post on all platforms(dev.to, hashnode and medium), but as hashnode is not looking a good option right now, so there is the `5` option for only `dev.to` and `medium`.

[all-platforms](https://gitlab.com/MR_DESTRUCTIVE/tblog-img/-/raw/main/crossposter.gif)

## Contributions

This is not a perfect script and has a lot of bugs due to encoding the markdown in BASH. It is subject to improve over time as I get to understand more string manipulation in BASH and render the post as expected. Please feel free to open up a genuine and smallest of issues.

## Crossposter Python Script

A python version of the Crossposter script is in development, it currently supports posting on [dev.to](https://github.com/Mr-Destructive/crossposter/blob/main/pyscript/dev.py) and [codenewbie community](https://github.com/Mr-Destructive/crossposter/blob/main/pyscript/codenewbie.py). Support for hashnode and medium is in pipeline and will be updated soon.

[crossposter.py](https://github.com/Mr-Destructive/crossposter/tree/main/pyscript)

Dependencies for the python script:

- [requests](https://docs.python-requests.org/en/latest/user/install/#install)

Running the script:

- Make sure to copy the copy the python folder from the [GitHub repo](https://github.com/Mr-Destructive/crossposter/tree/main/pyscript).
- Create the markdown file in that directory and enter the front-matter in the file.
- Run the main `app.py` file along with the name of the markdown file as follows to proceed ahead.

```
python app.py file_name
```
Currently, the front matter should be manually inserted in the markdown file, though the input prompt for the same will be featured.

0 comments on commit dde085c

Please sign in to comment.