-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrosspost.sh
230 lines (186 loc) · 8.13 KB
/
crosspost.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env bash
function dev()
{
if [[ -z $(sed -n -e 's/.*to://p' $keys) ]];then
read -p "Enter the dev.to API key : " dev_key
sed -i "/to:/ s/$/$dev_key/" $keys
fi
curl -s -o dev/null -X POST -H "Content-Type: application/json" \
-H "api-key: $dev_key" \
-d '{"article":{"body_markdown":"'"$body"'"}}' \
https://dev.to/api/articles
if [[ $? -eq 0 ]];then
echo "Posted on dev.to"
else
echo -e "Error...\nFailed to post on dev.to"
fi
}
function hashnode()
{
if [[ -z $(sed -n -e 's/.*node://p' $keys ) ]]; then
read -p "Enter the hashnode.com token : " hashtoken
sed -i "/node:/ s/$/$hashtoken/" $keys
fi
if [[ -z $(sed -n -e 's/.*node_id://p' $keys ) ]]; then
read -p "Enter the hashnode username : " hash_uname
curl -s -o dev/null 'https://api.hashnode.com/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://api.hashnode.com' -H 'Authorization: "'"$hash_token"'"' --data-binary '{"query":"query user {\n user(username: \"'"$hash_uname"'\") {\n publication {\n _id\n }\n }\n}\n"}' --compressed >temp.json
hash_id=$(grep -o -P '(?<=id":").*(?=")' temp.json)
sed -i "/hashnode_id:/ s/$/$hash_id/" $keys
fi
awk '{print $0"\\r\\n"}' temp.txt >temp.md
cat temp.md | tr -d '\r\n' | sed 's/\\r\\n/<br>/g'> body.md
body=$(cat body.md)
# comment out the below two lines for non-publication posts
#curl 'https://api.hashnode.com/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json'\
# -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://api.hashnode.com' -H 'Authorization: '$hash_token'' --data-binary '{"query":"mutation {\n createStory(\n input: {\n title: \"'"$title"'\",\n contentMarkdown: \"'"$body"'\"\n tags: [\n {\n _id: \"56744721958ef13879b94ff1\",\n name: \"General Programming\",\n slug: \"programming\"\n }\n ]\n }\n ) {\n message\n post{\n title\n }\n }\n}\n\n "}' --compressed
curl -s -o dev/null 'https://api.hashnode.com/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json'\
-H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://api.hashnode.com' -H 'Authorization: '$hashtoken'' --data-binary '{"query":"mutation {\n createPublicationStory(\n input: {\n title: \"'"$title"'\",\n contentMarkdown: \"'"$body"'\"\n tags: [\n {\n _id: \"56744721958ef13879b94ffc\",\n name: \"General Programming\",\n slug: \"programming\"\n }\n ]\n coverImageURL:\"'"$cover_image"'\" }\n publicationId:\"'"$hash_id"'\",\n hideFromHashnodeFeed:false\n ) {\n message\n post{\n title\ncoverImage\n }\n }\n}\n"}' --compressed
if [[ $? -eq 0 ]];then
echo "Posted on hashnode.com"
else
echo -e "Error...\nFailed to post on hashnode.com"
fi
}
function medium()
{
if [[ -z $(sed -n -e 's/.*ium://p' $keys ) ]]; then
read -p "Enter the medium.com API token : " med_token
med_id=$(curl -o dev/null -H "Authorization: Bearer $med_token" https://api.medium.com/v1/me | json_pp >temp.json)
med_id=$(grep -o -P '(?<=id).*(?=,)' temp.json )
med_id=$(echo ${med_id:3} | tr -d '"')
sed -i "/ium:/ s/$/$med_token/" $keys
sed -i "/id:/ s/$/$med_id/" $keys
fi
tags=$(echo $tags | sed "s/ //g")
tags=$(echo $tags | sed "s/,/','/g" | sed "s/$/'/" | sed "s/^/'/" | sed "s/\b\(.\)/\u\1/g")
sed -i "1a # $title" temp.txt
sed -i "1a ![]($cover_image)" temp.txt
sed -i "2a ## $subtitle" temp.txt
awk '{print $0"\\r\\n"}' temp.txt >temp.md
cat temp.md | tr -d '\r\n' > body.md
body=$(cat body.md)
curl -s -o dev/null -H "Authorization: Bearer $med_token " -H "Content-Type: application/json" \
-H "Accept: application/json" -H "Accept-Charset: utf-8" --request "POST" \
-d '{ "title": "'"$title"'", "contentFormat": "markdown", "content": "'"$body"'", "canonicalUrl": "'"$canonical_url"'", "tags": ["'"$tags"'"],"publishStatus": "public" }'\
"https://api.medium.com/v1/users/$med_id/posts"
if [[ $? -eq 0 ]];then
echo "Posted on medium.com"
else
echo -e "Error...\nFailed to post on medium.com"
fi
}
function codenewbie()
{
if [[ -z $(sed -n -e 's/.*codenewbie://p' $keys) ]];then
read -p "Enter the codenewbie API key : " codenbkeys
sed -i "/codenewbie:/ s/$/$codenbkeys/" $keys
fi
curl -X POST -H "Content-Type: application/json" \
-H "api-key: $codenbkeys" \
-d '{"article":{"body_markdown":"'"$body"'"}}' \
https://community.codenewbie.org/api/articles
echo $?
if [[ $? ]];then
echo -e "\nPosted on codenewbie\n"
else
echo -e "Error...\nFailed to post on dev.to"
fi
}
touch keys.txt body.md temp.md temp.txt
keys=keys.txt
if [[ ! -s $keys ]];then
head -n 1 $keys | echo "dev.to:" >$keys
sed -i "1a medium:" $keys
sed -i "2a medium_id:" $keys
sed -i "3a hashnode:" $keys
sed -i "4a hashnode_id:" $keys
sed -i "5a codenewbie:" $keys
else
dev_key=$(sed -n -e 's/dev.to://p' $keys)
med_token=$(sed -n -e 's/medium://p' $keys)
med_id=$(sed -n -e 's/medium_id://p' $keys)
hashtoken=$(sed -n -e 's/hashnode://p' $keys)
hash_id=$(sed -n -e 's/hashnode_id://p' $keys)
codenbkeys=$(sed -n -e 's/codenewbie://p' $keys)
fi
if [[ -n $1 ]];
then
file=$1
else
read -p "Enter the name of the file : " file
fi
if [[ -z $(sed '/^---/p;q' $file) ]];then
read -p "Enter the title of your post : " title
read -p "Enter the subtitle of your post : " subtitle
read -p "Enter the status of your post(true/false) : " statusp
read -p "Enter the tags for your post : " tags
read -p "Enter the canonical url of the post : " canonical_url
read -p "Enter the cover_image url of the post : " cover_image
read -p "Should this be a part of a series? (y/n) : " series_bool
if [[ $series_bool = "y" || $series_bool = "Y" ]]; then
read -p "Enter the name of the series : " series
fi
sed -i "1i ---" $file
sed -i "1a title: $title" $file
sed -i "2a subtitle: $subtitle" $file
sed -i "3a published: $statusp" $file
if [[ -n $tags ]]; then
sed -i "4a tags: $tags" $file
fi
if [[ -n $canonical_url ]]; then
sed -i "5a canonical_url: $canonical_url" $file
fi
if [[ -n $canonical_url ]]; then
sed -i "6a cover_image: $cover_image" $file
fi
if [[ -n $series ]]; then
sed -i "7a series: $series" $file
fi
sed -i "8a ---" $file
fi
title=$(sed -n '2 s/^[^=]*title: *//p' $file)
subtitle=$(sed -n '3 s/^[^=]*subtitle: *//p' $file)
tags=$(sed -n '5 s/^[^=]*tags: *//p' $file)
canonical_url=$(sed -n '6 s/^[^=]*url: *//p' $file)
cover_image=$(sed -n '7 s/^[^=]*age: *//p' $file)
awk '{print $0" "}' $file >temp.txt
sed -i '1,+7 s/$/\\r\\n/g' temp.txt
sed -i '/^- / s/$/\\r\\n/g' temp.txt
sed -i '/^[0-9]. / s/$/\\r\\n/' temp.txt
awk '!NF{$0="\\r\\n\\r\\n"}1' temp.txt >temp.md
cat temp.md | tr -d '\r\n' >body.md
body=$(cat body.md)
echo -e "\n1. dev.to \n"
echo -e "2. hashnode.com \n"
echo -e "3. medium.com \n"
echo -e "4. All of the above\n"
echo -e "5. dev.to and medium\n"
echo -e "6. codenewbie \n"
read -p "Where you want to cross post to? " num
if [[ $num -eq 1 ]];then
if [[ -z $(sed -n -e 's/.*to://p' $keys) ]];then
read -p "Enter the dev.to API key : " dev_key
sed -i "/to:/ s/$/$dev_key/" $keys
fi
dev
elif [[ $num -eq 2 ]];then
hashnode
elif [[ $num -eq 3 ]];then
medium
elif [[ $num -eq 4 ]];then
dev
hashnode
medium
elif [[ $num -eq 5 ]];then
dev
medium
elif [[ $num -eq 6 ]];then
if [[ -z $(sed -n -e 's/.*codenewbie://p' $keys) ]];then
read -p "Enter the codenewbie API key : " codenbkeys
sed -i "/codenewbie:/ s/$/$codenbkeys/" $keys
fi
codenewbie
else
echo "Invalid Input"
fi
rm temp.md temp.txt body.md