Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Upload Image to https://www.artstation.com/

Contents

  • Question
  • Question Info
  • Background / Research
  • Code Block
  • resources


Question:

How do I Programmatic/Automatically upload an image to Artstation\Project-page using Powershell?

No human input to select files during upload. Only predefined media data. If it can be done with powershell's built-in functionality that is preferred.

(see web-page reference image 'ArtStation Upload Page' below)



Question Info:

Environment Context:

  • OS: Windows 10, V:1803, Build:17134.165
  • Platform: PC
  • Powershell: V:5.1.17134.165 (Desktop edition), Build:10.0.17134.165, (runas Admin, ExecutionPolicy RemoteSigned)
  • InternetExplorer11: V:11.165.17134.0, UpdateV:11.0.75(KB4339093)
  • Website: https://www.artstation.com/myartstation/projects/new


Artstation has a 'drag and drop' section with an 'upload button' in the center that opens a file browser. (see image ArtStation Upload Page).

I see a few options.

  1. Simulate a file drag into the drag area (seems the most simple)
  2. Pass the file browser the file to upload without using button.click()
  3. Button.click() Open the file browser use a separate powershell process to capture the file browser and navigate to and select the file to upload.
  4. Manualy create/add a new child 'Art-Work' element to the project page


Background - Research:

Hey, my first post.

I'm a new powershell user and new programmer. I was trying to right a quality of life application to upload an image to Artstation via powershell.

I've been trying to figure out how to do it for about two week now (80+ hours) looking at hundreds of fora and sites with no yield. Maybe I don't know what I'm looking for or not using queries that will land me close to an answer. So I thought it was time to cast a line and see if I could get some help. Please explain it to me like I'm an idiot, from square one.

I haven't found anything on how to upload an image via powershell yet. Or at least nothing I can understand.


So far everything I've looked at the I thought might work requires me to know where I'm sending the file. Invoke-WebRequest / Invoke-RestMethod, Copy-item -session, Drag-drop javaScript. The only problem is I don't know. And the only thing I have is a internetExplorer.application ComObject with variables for the 'drag and drop zone 'object and the 'file upload button' object. They both have methods and properties that I'm not sure how to set or how/what data structure they require.


Past Queries: (MFF, IE, and Chrome;)

(Word I've used over many sessions)
"powershell", v5, https, upload, image, file, artstation, datatransfer, onDropEvent, copy-item, html tag , html, javascript, drag&drop function, input function, definition, ondrop, object format, https file transfer, convert, image to bites, package, object, $ie.document.getelementId('image-input-upload').value=".\$myImage" , detect, catch, capture, track, find, new, sub, process, file explorer, browser, navigate, select file, assign, CTS, uploader, dllhost, sendkeys, AssemblyName, System.Windows.Forms, microsoft.VisualBasic, multiPartContent, set-clipboard, get-clipboard, fileAsBinaryString



Code Block:

Sorry if this is ugly or doesn't make sense. The simple answer for that is, I don't know what the heck I'm doing. I'm just scrapping something together that will hopefully work.

#< START_GLOBAL-VARIABLES >#

#< Secure Document >#
    #TODO: Clean up memory before setting to null
    #If Either list exists on script run set to null
    if($CredList.Keys){$CredList = $null}
    if($keyWord.Count){$keyWord = $null}

    $CredList = @{}
    #TODO: remove debug elements
    #Debug Variable 'easy select has key'
    $keyWord = @()

    #Pull and Parse Security Data from file
    $CredBody = Get-Content $CertFile
    $CredBody | ForEach-Object {
        $s = $_ -split ", "
        $keyWord += [REGEX]::Match($s[0], "(?<=www.).*?(?=.com)").groups[0].value
        $CredList.add($s[0],($s[1],$s[2]))
    }

#TODO: remove debug elements
#< Debug Option >#
    #Split security file data and repack in hashtable
    $keyWord = $keyWord.GetEnumerator() | Sort-Object
    $httpHost = ($CredList.Keys -imatch $keyWord[0])
    $ActiveKey = ($CredList[$CredList.Keys -imatch $keyWord[0]])

###< END_GLOBAL-VARIABLES >###


###< START_ARTSTATION >###


if($httpHost -imatch 'artstation')
{
    #Todo: Move this to a global scope to reuse for other art sites.
        #Or create once and use navigate? More/less secure?

    #< Create IE window and poit at Artstation >#
        $ie = New-Object -ComObject 'InternetExplorer.Application'
        $ie.visible=$true
        $ie.navigate($httpHost)

        while($ie.ReadyState -ne 4) {start-sleep -m 100}


    ##< START_LOGIN >##

    if($ie.Document.location.href -eq 'https://www.artstation.com/users/sign_in')
    {
        #Todo: Encrypt Data Source + pass information securly to webpage
            #Pass data through windows security window?

        #< Find Desired Cert Elements >#
            $forms = $ie.Document.forms | ForEach-Object { $_ | ForEach-Object { if($_.id -imatch 'email'-or $_.id -imatch 'password'){return $_ } } }

        # UserName
            $UserForm = $forms | Where-Object {$_.id -imatch 'email'}
            $UserForm[0].focus()
            $ie.Document.activeElement.value = $ActiveKey[0][0]

        # Password
            $PassForm = $forms | Where-Object {$_.id -imatch 'password'}
            $PassForm[0].focus()
            $ie.Document.activeElement.value = $ActiveKey[0][1]

        #Submit Form
            $submitForm = $ie.Document.forms[1].elements | Where-Object { $_.name -imatch 'button'}
            $submitForm.focus()
            $submitForm.click()

            while($ie.ReadyState -ne 4) {start-sleep -m 100}
    }

    ##< END_LOGIN >##

    #TODO: remove debug elements
    #< Debug Option >#
        #Navigate to an Existing or New project page
        $result = UserPrompt $ie 'Is this a new project?' 'Project Setup' 'Question' {$ie.navigate("https://www.artstation.com/myartstation/projects/new")} {$ie.navigate("https://www.artstation.com/marketplace/manage/products/new")} {$null}
        while($ie.ReadyState -ne 4) {start-sleep -m 100}


    ##< START_UPLOAD >##


    #"Drag & Drop Rect"
        #<div class="well drop-files text-center hidden-sm hidden-xs" id="dropzone"><label class="btn btn-default" for="upload-image-input"><i class="far fa-arrow-from-bottom fa-pad-right"></i>Upload your images</label><div class="separator-sm"></div><div class="text-muted">Upload or drag &amp; drop images</div></div>
    #Pictures:
        #<input accept="image/jpeg,image/png,image/gif" id="upload-image-input" multiple="multiple" name="asset[image][]" type="file">
    #Video:
        #<a class="btn btn-default" id="project-add-video-btn" data-toggle="modal" data-target="#add-video-modal" href="#"><i class="far fa-film fa-pad-right"></i>Add Video</a>
    #SketchFab
        #<a class="btn btn-default" id="project-add-3dmodel-btn" data-toggle="modal" data-target="#add-3dmodel-modal" href="#"><i class="far fa-cube fa-pad-right"></i>Add Sketchfab</a>
    #Marmoset
        #<input multiple="multiple" name="asset[image][]" type="file">
    #Pano
        #<a class="btn btn-default" id="project-add-pano-btn" href="#"><i class="far icon-pano fa-pad-right"></i>Add 360 Pano</a>
    #ProjectThumbnail
        #<label class="upload-thumbnail-placeholder" for="upload-thumbnail"><i class="far fa-image fa-2x"></i><div class="small">Upload or drag &amp; drop image</div></label>


    ##< START_POPULATE-FIELDS >##

        #< Art-Image >#
            #Image
            #Upload
            #Captions
            #ImageFitting

        #< Thumbnail-Image >#
            #Image
            #Upload

        #< VARIABLES >#
            $fielfToken = 'title','description', 'medium','Matter', 'adult','albmus','portfolio', 'software', 'Tags'
            $Values = 'This is my title',
                      'This painting was done for my Twitch stream! come stop on by some time.',
                      'digital 3d'

        #< TITLE >#
            #Text Field [a-z,0-9]
            $titleToken = $ie.Document.forms | ForEach-Object{ $_ | Where-Object {$_.id -imatch $fielfToken[0]} }
            $titleToken[0].value = $Values[0]

        #< DESCRIPTION >#
            #Text Field [a-z,0-9]
            $descriptionToken = $ie.Document.forms | ForEach-Object{ $_ | Where-Object {$_.id -imatch $fielfToken[1]} }
            $descriptionToken.value = $Values[1]

        #< MEDIUM >#
            #dropdown[v]
            #Pull dropdown choices
            $mediumToken = $ie.Document.forms | ForEach-Object{ $_ | Where-Object {$_.id -imatch $fielfToken[2]} }
            #Set medium dropdown to $mediumToken.tostring()

        #< SUBJECT-MATTERr >#
            #dropdown[v]
            #Pull dropdown choices
            #$subjectToken
            #$subjectToken

        #< ADULT-CONTENT >#
            #checkbox[x]
            $adultToken = $ie.Document.forms | ForEach-Object{ $_ | Where-Object {$_.id -imatch $fielfToken[4]} }
            $adultToken.click()

        #< SOFTWARE >#
            #Dropdown[v]
            #Pull software list
            #$softwareToken
            #$softwareToken

        #< START_TAGS >#
            #Option 1: Previous[] pr Previous[X]
            #Option 2:  'tag1' 'tag2' 'tag3'. . .

            #if Previous ticked checkbox[x]
            #$foundToken[1].click()

            #if extra Tags = @()
            #$foundToken[0].length

        #< END_TAGS >#

        #< ALBUMS >#
            #checkbox[x] list ( AlbumsA[], AlbumsB[], AlbumsC[] )
            #Get list of available albums
            $albumsToken = $ie.Document.forms | ForEach-Object{ $_ | Where-Object {$_.classname -imatch 'col-md-3'} }
            $albumsToken.click()

        #< START_PORTFOLIO >#

            #ArtStation.com checkbox[x]
            #$adultToken = $ie.Document.forms | ForEach-Object{ $_ | Where-Object {$_.id -imatch $fielfToken[4]} }
            #$adulToken.click()

            #MyWebsite checkbox[x]
            #$adultToken = $ie.Document.forms | ForEach-Object{ $_ | Where-Object {$_.id -imatch $fielfToken[4]} }
            #$adulToken.click()

        #< END_PORTFOLIO >#

        #< Review >#
            #click ok when done reviewing
            #Edit text
            #Re-upload media

        #< Publish >#
            #Save.click()
            #Publish.click()


    ##< END_POPULATE-FIELDS >##


    #< Logout >#

    #$signOut = $ie.Document.all | Where-Object {$_.classname -imatch 'far fa-sign-out fa-fw fa-pad-right-x2'}
    #$signOut.focus()
    #$signOut.click()

    ###< END_ARTSTATION >###
}


Resources:

Links:
* community.box.com/t5/Platform-and-Development-Forum/Upload-a-file-using-API-in-PowerShell/td-p/35660
* app.box.com/s/46hwgi48n31g42vuqjk67d01abceypij
* stackoverflow.com/questions/23059945/upload-a-file-to-box-com-using-powershell
* get-powershellblog.blogspot.com/2017/09/multipartform-data-support-for-invoke.html
* blog.majcica.com/2016/01/13/powershell-tips-and-tricks-multipartform-data-requests/
* blog.majcica.com/2016/01/15/uploading-xl-deploy-dar-package-via-powershell/
* github.com/react-dropzone/react-dropzone

like image 777
Valthalin Avatar asked Jul 25 '18 08:07

Valthalin


1 Answers

I know you posted that question a long time ago, but here's a possible approach that you can probably make use of in some way.

function Click-DragControl
{
Param(
    [Parameter(Position=0,Mandatory=$TRUE)]
    [UIAutomation.UiElement]
    $ObjectDragFrom,

    [Parameter(Position=1,Mandatory=$TRUE)]
    [UIAutomation.UiElement]
    $ObjectDragTo
    )

$newX=[Int]($ObjectDragFrom.Current.BoundingRectangle.X+($ObjectDragFrom.Current.BoundingRectangle.Width/2))
$newY=[Int]($ObjectDragFrom.Current.BoundingRectangle.Y+($ObjectDragFrom.Current.BoundingRectangle.Height/2))

# In the event the object is inside a collapsible window, or is otherwise off-screen, the pixels we get back will be <0,0>.
# So we'll revert to our older method of clicking, because it can crawl through the control and still obtain the item.
if ($newX -eq 0 -and $newY -eq 0)
{
    $ObjectDragFrom.Mouse.LeftButtonClick()
    return $true
}

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($newX,$newY)
$signature=
@'
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@
$SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru
Try
{
    $SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0); # Left Mouse Down
}
Catch
{
    Write-Host("ERROR: Caught an Unhandled Exception in Click-Control. Originating Error:`n$Global:Error[0]")
    return $FALSE
}   

$newX=[Int]($ObjectDragTo.Current.BoundingRectangle.X+($ObjectDragTo.Current.BoundingRectangle.Width/2))
$newY=[Int]($ObjectDragTo.Current.BoundingRectangle.Y+($ObjectDragTo.Current.BoundingRectangle.Height/2))

# In the event the object is inside a collapsable window, or is otherwise off-screen, the pixels we get back will be <0,0>.
# So we'll revert to our older method of clicking, because it can crawl through the control and still obtain the item.
if($newX -eq 0 -and $newY -eq 0){
    $ObjectDragTo.Mouse.LeftButtonClick()
    return $TRUE
}

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($newX,$newY)
$signature=
@'
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@
$SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru
Try
{
    $SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0); # Left Mouse Up
    return $TRUE
}
Catch
{
    Write-Host("ERROR: Caught an Unhandled Exception in Click-DragControl. Originating Error:`n$Global:Error[0]")
    return $FALSE
}

}

like image 157
Jacob Rosenblum Avatar answered Oct 23 '22 19:10

Jacob Rosenblum