Skip to content

Instantly share code, notes, and snippets.

@mia-riezebos
Last active February 15, 2022 01:30
Show Gist options
  • Save mia-riezebos/daeb2494f4707d4c96d8f53d1c49a5e1 to your computer and use it in GitHub Desktop.
Save mia-riezebos/daeb2494f4707d4c96d8f53d1c49a5e1 to your computer and use it in GitHub Desktop.
A good-looking, functional ATF format for twitter playback reporting, using WACUP
$puts(_title,$iflonger($put(_title,[%artist% - ]$if2(%title%,$filepart(%filename%))),81,$cut($get(_title),78)...,$get(_title)))
$puts(_album,[from: %album%][ '('%year%')'])
$puts(_link,$if(%comment%,$split($substr(%comment% nolink,$if2($ifgreater($strstr(%comment%,https://),0,$strstr(%comment%,https://),)$ifgreater($strstr(%comment%,http://),0,$strstr(%comment%,http://),),$add($len(%comment%),2)),), ,0),nolink))
$puts(_rating,$if(%rating%,$repeat(★,%rating%)$repeat(☆,$sub(5,%rating%)),unrated))
'♬♫♪ ◖( ˘ ɜ˘ )◗ ♪♫♬'
$get(_title)
$get(_album)
purchase: $get(_link)
my rating: $get(_rating)

A good-looking, functional ATF format for twitter playback reporting, using WACUP

I've done a lot of puzzling with provided ATF functions over the past day, and I've encountered and solved a bunch of issues. I will walk through my process and provide my entire format at the end.

What I wanted to achieve after seeing the twitter plugin, was an account that basically automatically recommends the music I enjoy when I listen to it. I wanted WACUP to post a tweet of the song I'm currently listening to, if two criteria were met:

  • the song has a %rating%
  • the song has a %comment% and the %comment% contains a url

It's quite simple to achieve the filters to prevent tweeting when there's 1 or more of the metadata fields missing.

scroll down for final product

Conditional Posting

By using an $if(%blah%,%blah%,noblah) in the formatting, and adding noblah to the phrase filter (replacing "blah" with any existing field of course), WACUP will not post any tweets if the metadata fields are empty, I implemented this as follows:

Filter by phrase: unrated;nocomment

$if(%comment%,%comment%,nocomment)
$if(%rating%,%rating%,unrated)

Extracting a link from a comment

This was quite the challenge, I won't lie. I'm used to having loops & advanced string manipulation at my disposal so this definitely had me looking at string manipulation in a different light.

I wanted to credit artists and provide followers with a quick way to purchase or stream the songs I'm recommending. All purchases made through bandcamp automatically get a %comment% field added, with the link to the page the release was purchased from. This is why I chose to use the %comment% field for track links.

The issue that I encountered, was that some songs have comments that either contain no links, or are so long they won't fit in 280 characters. This meant I had to extract just the link (I chose to go for the first link) in the %comment%.

After plenty of time lost to dead ends, a friend of mine gave me a new perspective, which resulted in the following format.

$if(%comment%,$split($substr(%comment% nolink,$if2($ifgreater($strstr(%comment%,https://),0,$strstr(%comment%,https://),)$ifgreater($strstr(%comment%,http://),0,$strstr(%comment%,http://),),$add($len(%comment%),2)),), ,0),nolink)

I've "formatted" it to be more human-readable, but keep in mind this doesn't actually work in ATF.

$if(
    %comment%,
    $split(
        $substr(%comment% nolink,
            $if2(
                $ifgreater($strstr(%comment%,https://),0,$strstr(%comment%,https://),)
                $ifgreater($strstr(%comment%,http://),0,$strstr(%comment%,http://),),
                %add($len(%comment%),2)
            ),
        )
        , ,0
    ),
    nolink
)
  1. if %comment%, is non-empty, return value, else return nolink.
  2. return the first value before a whitespace ' '
  3. return a substring of %comment% nolink from a certain index
    1. if there is an occurrence of either https:// or http:// in %comment%, the index for $substr is the character index of that occurrence.
    2. else, the index for $substr() is the length of %comment% (+2 to adjust for the non-0-indexing of $substr(), and the whitespace).
  • if there is a url somewhere in %comment%, the first one will be at the beginning of the $substr(). $split() will return everything before the first whitespace ' ' (just the url).
  • if there is no url in %comment%, the $substr() and %split() will return nolink
  • if there is no %comment%, return nolink

Et voilà, we have extracted the first url from the %comment%! And we've also made sure that if there are no links in a non-empty %comment%, it will still return nolink, so that a tweet doesn't get sent out.

Extra

Since using this stuff inline can be quite hard to read in-practice, I like to first process the values I need, store them in variables, and just $get() them at a later point. See below:

$puts(_link,$if(%comment%,$split($substr(%comment% nolink,$if2($ifgreater($strstr(%comment%,https://),0,$strstr(%comment%,https://),)$ifgreater($strstr(%comment%,http://),0,$strstr(%comment%,http://),),$add($len(%comment%),2)),), ,0),nolink))
$puts(
    _link,
    $if(
        %comment%,
        $split(
            $substr(%comment% nolink,
                $if2(
                    $ifgreater($strstr(%comment%,https://),0,$strstr(%comment%,https://),)
                    $ifgreater($strstr(%comment%,http://),0,$strstr(%comment%,http://),),
                    %add($len(%comment%),2)
                ),
            )
            , ,0
        ),
        nolink
    )
)

Formatting

I thought it was important to make sure my tweets look somewhat presentable, especially because I'm reviewing other people's hard work.

Text

Of course we need to include the artist, track title, and, preferrably, which album the song is from.

Title

$puts(_title,$iflonger($put(_title,[%artist% - ]$if2(%title%,$filepart(%filename%))),81,$cut($get(_title),78)...,$get(_title)))
$puts(
    _title,
    $iflonger(
        $put(
            _title,
            [%artist% - ]$if2(%title%,$filepart(%filename%))
        ),
        81,
        $cut($get(_title),78)...,
        $get(_title)
    )
)

If non-empty, this stores %title% to _title, else the %filename%. Includes %artist%, if non-empty.

Album

$puts(_album,[from: %album%][ '('%year%')'])
$puts(
    _album,
    [from: %album%][ '('%year%')']
)

Rating

simply having a number represent the rating is a little boring, so I chose to format my rating with unicode stars!

$puts(_rating,$if(%rating%,$repeat(★,%rating%)$repeat(☆,$sub(5,%rating%)),unrated))
$puts(
    _rating,
    $if(
        %rating%,
        $repeat(★,%rating%)$repeat(☆,$sub(5,%rating%)),
        unrated
    )
)
  1. if %rating% is non-empty, return a formatted rating, else unrated
  2. $repeat() repeats %rating% amount of filled stars, and 5 - %rating% amount of hollow stars
  3. store the return value in _rating

Final

Simply slapping in the one-liners we made before, we can make a nice looking tweet format, quite quickly and simply! At the top, I've pasted all the one-liners, which store the formatted values in the variables.

Below these, we can start composing the tweet. It will look like there's some whitespace at the top, in the preview window, we don't need to worry about this, Twitter trims this off, automatically.

MAKE SURE YOU ADD TO "Filter by phrase": unrated;nolink

$puts(_title,$iflonger($put(_title,[%artist% - ]$if2(%title%,$filepart(%filename%))),81,$cut($get(_title),78)...,$get(_title)))
$puts(_album,[from: %album%][ '('%year%')'])
$puts(_link,$if(%comment%,$split($substr(%comment% nolink,$if2($ifgreater($strstr(%comment%,https://),0,$strstr(%comment%,https://),)$ifgreater($strstr(%comment%,http://),0,$strstr(%comment%,http://),),$add($len(%comment%),2)),), ,0),nolink))
$puts(_rating,$if(%rating%,$repeat(★,%rating%)$repeat(☆,$sub(5,%rating%)),unrated))

'♬♫♪ ◖( ˘ ɜ˘ )◗ ♪♫♬'

$get(_title)
$get(_album)

purchase: $get(_link)

my rating: $get(_rating)

A functional, running example: https://twitter.com/patchstep_plays/status/1479237837316923397

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment