11#! /usr/bin/env bash
22# ## Run this script on GitHub after zipping the mod to upload the mod on mods.factorio.com
3- # ## This a modified version of https://github.com/shanemadden/factorio-mod-portal-publish/blob/1930e44ecd86e3f2d90921da2a1f40b050fd12c3/entrypoint.sh
3+ # ## This a modified version of https://github.com/Penguin-Spy/factorio-mod-portal-publish/blob/5c669dc60672e6293afd5b1913a0c8475c5490c0/entrypoint.sh
4+ # ## You can generate your FACTORIO_MOD_API_KEY on https://factorio.com/create-api-key
45
56
67# ## Check commands
@@ -27,50 +28,29 @@ if ! echo "${MOD_VERSION}" | grep -P --quiet '^(v)?\d+\.\d+\.\d+$'; then
2728fi
2829
2930
30- # Get a CSRF token by loading the login form
31- CSRF=$( curl -b cookiejar.txt -c cookiejar.txt -s https://factorio.com/login? mods=1 | grep csrf_token | sed -r -e ' s/.*value="(.*)".*/\1/' )
32-
33-
34- # Authenticate with the credential secrets and the CSRF token, getting a session cookie for the authorized user
35- curl -b cookiejar.txt -c cookiejar.txt -s -e https://factorio.com/login? mods=1 -F " csrf_token=${CSRF} " -F " username_or_email=${FACTORIO_USER} " -F " password=${FACTORIO_PASSWORD} " -o /dev/null https://factorio.com/login
36-
37-
38- # Query the mod info, verify the version number we're trying to push doesn't already exist
39- curl -b cookiejar.txt -c cookiejar.txt -s " https://mods.factorio.com/api/mods/${MOD_NAME} /full" | jq -e " .releases[] | select(.version == \" ${MOD_VERSION} \" )"
40- # store the return code before running anything else
41- STATUS_CODE=$?
42-
43- if [[ $STATUS_CODE -ne 4 ]]; then
44- echo " Release already exists, skipping"
45- exit 0
46- fi
47- echo " Release doesn't exist for ${MOD_VERSION} , uploading"
48-
49-
50- # Load the upload form, getting an upload token
51- UPLOAD_TOKEN=$( curl -b cookiejar.txt -c cookiejar.txt -s " https://mods.factorio.com/mod/${MOD_NAME} /downloads/edit" | grep token | sed -r -e " s/.*token: '(.*)'.*/\1/" )
52- if [[ -z " ${UPLOAD_TOKEN} " ]]; then
53- echo " Couldn't get an upload token, failed"
31+ # Get an upload url for the mod
32+ URL_RESULT=$( curl -s -d " mod=${MOD_NAME} " -H " Authorization: Bearer ${FACTORIO_MOD_API_KEY} " https://mods.factorio.com/api/v2/mods/releases/init_upload)
33+ UPLOAD_URL=$( echo " ${URL_RESULT} " | jq -r ' .upload_url' )
34+ if [[ " ${UPLOAD_URL} " == " null" ]] || [[ -z " ${UPLOAD_URL} " ]]; then
35+ echo " Couldn't get an upload url, failed"
36+ ERROR=$( echo " ${URL_RESULT} " | jq -r ' .error' )
37+ MESSAGE=$( echo " ${URL_RESULT} " | jq -r ' .message // empty' )
38+ echo " ${ERROR} : ${MESSAGE} "
5439 exit 1
5540fi
5641
57- # Upload the file, getting back a response with details to send in the final form submission to complete the upload
58- UPLOAD_RESULT=$( curl -b cookiejar.txt -c cookiejar.txt -s -F " file=@${MOD_NAME} _${MOD_VERSION} .zip;type=application/x-zip-compressed" " https://direct.mods-data.factorio.com/upload/mod/${UPLOAD_TOKEN} " )
5942
60- # Parse 'em and stat the file for the form fields
61- CHANGELOG=$( echo " ${UPLOAD_RESULT} " | jq -r ' @uri "\(.changelog)"' )
62- INFO=$( echo " ${UPLOAD_RESULT} " | jq -r ' @uri "\(.info)"' )
63- FILENAME=$( echo " ${UPLOAD_RESULT} " | jq -r ' .filename' )
64- THUMBNAIL=$( echo " ${UPLOAD_RESULT} " | jq -r ' .thumbnail // empty' )
43+ # Upload the file
44+ UPLOAD_RESULT=$( curl -s -F " file=@${MOD_NAME} _${MOD_VERSION} .zip" " ${UPLOAD_URL} " )
6545
66- if [[ " ${FILENAME} " == " null" ]] || [[ -z " ${FILENAME} " ]]; then
46+ # The success attribute only appears on successful uploads
47+ SUCCESS=$( echo " ${UPLOAD_RESULT} " | jq -r ' .success' )
48+ if [[ " ${SUCCESS} " == " null" ]] || [[ -z " ${SUCCESS} " ]]; then
6749 echo " Upload failed"
50+ ERROR=$( echo " ${UPLOAD_RESULT} " | jq -r ' .error' )
51+ MESSAGE=$( echo " ${UPLOAD_RESULT} " | jq -r ' .message // empty' )
52+ echo " ${ERROR} : ${MESSAGE} "
6853 exit 1
6954fi
70- echo " Uploaded ${MOD_NAME} _${MOD_VERSION} .zip to ${FILENAME} , submitting as new version"
71-
72- # Post the form, completing the release
73- curl -b cookiejar.txt -c cookiejar.txt -s -X POST -d " file=&info_json=${INFO} &changelog=${CHANGELOG} &filename=${FILENAME} &file_size=${FILESIZE} &thumbnail=${THUMBNAIL} " -H " Content-Type: application/x-www-form-urlencoded" -o /dev/null " https://mods.factorio.com/mod/${MOD_NAME} /downloads/edit"
74- # TODO if that had a failure exit code then report failure, exit 1
7555
76- echo " Completed "
56+ echo " Upload of ${MOD_NAME} _ ${MOD_VERSION} .zip completed "
0 commit comments