How to Replace Links in Tweets or Add Links to Tweets With Perl

"Is this too sexy?" ~ Kim Kardashian via http://bit.ly/sIFojJ

"Is this too sexy?" ~ Kim Kardashian via http://bit.ly/sIFojJ

Near the end of 2009, I began sharing my dad’s stories online, beginning with the collection called The Road South. I put them on Scribd, a social document sharing and reading site. After a little practice, by tweeting about the stories, playing on curiosity and using hashtags liberally, I was able to get 1,000 clicks for free in one day. A while later, I removed the stories from Scribd (don’t ask me why), and I’ve only just recently put them back up (and more are coming). This means the links (and shortened bit.ly links) to those old Scribd documents are broken links. But I wanted to reuse the old tweets, perhaps with some edits. And with the help of the tool Timely.is (which schedules tweets when they’ll get the most visibility or engagement), it’ll be a cinch to get 1,000s of clicks (and reads and listens) to my dad’s stories once again.

I must clarify: there were many tweets per story. These coincided with the many reasons to read a particular story, or the many subjects of curiosity which would attract people’s attention and make them want to read, or the many funny or provocative things that were written or said by characters.

[I'll draw a parallel here... Each service or product you sell should have many tweets prepared for it. There should be tweets for all the benefits, perhaps for all the features, and surely for the points of curiosity as well. When these tweets are tweeted (and they may be repeated) they will work both together and on their own to get clicks to your website, specifically to the individual product page. Some people will see several of your tweets about a thing and then click a link, while others may click the first time they see a tweet about it. It's more common, though, that they would act after some repetition.]

To make it quick and easy to replace the old links in the old tweets with new links, I wrote a little Perl program and I want to share it with you so you can save time too.

The program reads an input file and produces and output file. The way it works is this… To tell it what the new link is, simply put the new shortened bit.ly link on its own line with no spaces before or after. The whole line is the link. That’s taken as a command to replace any links it sees afterwards (until the replacement link is changed to something else by the same method) with that link, whether those links are shortened or full length and regardless of how many links there are on a single line. The input file is just that, new shortened bit.ly links on their own lines followed by a series of tweets, one on each line. It can have empty lines, titles, headings and whatever else you want, that won’t be changed, will simply be copied into the output file.

Here’s the Perl program and sample input and output data to illustrate and make this clearer and easier to use yourself.

But before I show it to you, I’ve got to say this… Recently, I’ve been recording my dad, Dick Bird, telling his stories (new ones, not the 80 he’d already written) and we’re putting them together in a new collection called Off the Cuff. (You can listen to them on his new site, DicksTravels.com) We’ve been writing tweets for these new stories (we haven’t tweeted them yet) and I figured the program should be updated to automatically append these tweets with the shortened bit.ly links to the individual stories. So the second program listing (and associated data) is for that purpose. It not only replaces links as the first does, it also appends links to all lines without links in them.

Okay, here goes… (And two notes: the data files, you won’t see the whole lines here, but they’re there and you can copy-paste each whole code block to a file (I’ve given the filenames), secondly: sorry about the lack of indentation in the code, WordPress removed it and this is the first time I shared some perl on my blog. Let me know (COMMENT)  if you can do it better.)

replace-links-in-tweets.pl

# usage example:
# perl replace-links-in-tweets.pl dickstravels-oldTRStweets

# lineage:
# from dickstravels-make-new-pages.pl

# description:
# Takes bit.ly links on their own line (no spaces after it allowed!) as the current replacement
# Replaces any links on subsequent lines with the current replacement,
# regardless of how many links there are or how long or short they are

# written by Bevan Bird... Find me at http://bevanbird.com

use English;

# get input from command line
my $inputfile = $ARGV[0];
my $outputfile = $inputfile;
$inputfile .= ".txt";
$outputfile .= "-replaced.txt";

open(INPUT, "<", $inputfile) or die $!;
open(OUTPUT, ">", $outputfile) or die $!;

my $replacement_link;

while(<INPUT>) {
chomp;
if ($_ =~ /^http:\/\/bit\.ly\/[^\s]+$/){
$replacement_link = $_;
}else{
$_ =~ s/(http:\/\/[^\s]+)/$replacement_link/g;
}
print OUTPUT $_ . "\n";
}

close(INPUT) or die $!;
close(OUTPUT) or die $!;

dickstravels-oldTRStweets.txt

http://bit.ly/se76SD
The Road South #ebook blends black #humour, #travel, #sex, #fantasy, social issues, & extreme kayaking. >> http://bit.ly/6dK2FP
Check out The Road South short story collection and listen to them too! >> http://bit.ly/6dK2FP
Road South #ebook >> http://bit.ly/6dK2FP #fiction #travel #humour #kayak #safari #nude #revolution
Road South #ebook >> http://bit.ly/6dK2FP #fiction #culture #diving #fantasy #farming #humour

http://bit.ly/tPdGdz

Sexy Women, Men of Steel http://bit.ly/7lLVZD #story #fiction #travel #tropics #babe #reading

http://bit.ly/thZqF8

Papa Chango http://bit.ly/8eHZiZ #story #fiction #strange #adventure #action #tropics #babes #surgery

http://bit.ly/vBkKfF

Totally Unreal http://bit.ly/8iyzEb #story #fiction #fire #cyber #punk #conformist #VR #tech #future

http://bit.ly/ufymzE

Sexy Women, Men of Steel

http://bit.ly/tPdGdz

Sexy Women, Men of Steel >> http://bit.ly/7lLVZD #fiction #travel via @DicksTravels
Short story "Sexy Women, Men of Steel" http://bit.ly/7lLVZD #prose #fiction #travel #culture
So beautifully written, it borders on poetry, rather than prose. Not a word is wasted. ~ @Literatrix http://bit.ly/7lLVZD
Sexy Women, Men of Steel >> http://bit.ly/7lLVZD #story #fiction #travel #babe

It’s got to be her milk that makes them wild. A suckle would perk me up too when I’m feeling droopy. http://bit.ly/7lLVZD
One night she breaks. In the darkened kitchen I find her crying. The other kids are asleep. http://bit.ly/7lLVZD
Growing up to be sexy ladies. Fire in their eyes to scorch men of steel. http://bit.ly/7lLVZD
He’s putting a match to the fuse and opening the window, hurling a home-made bomb at the hostile rain. http://bit.ly/7lLVZD
"So ashamed they can’t ogle her tits." Sexy Women, Men of Steel >> http://bit.ly/7lLVZD #story #travel
"Mujeres sensitivas, hombres fuertes y de acero... sensitive women, strong men of steel" (los Angeles del Puerto) http://bit.ly/7lLVZD
Lighten Up, Papa Chango

http://bit.ly/thZqF8

Voluptuous Beach Girls http://bit.ly/8eHZiZ #story #fiction #strange #adventure #action #tropics #babes #surgery
Lighten Up, Papa Chango http://bit.ly/8eHZiZ #story #fiction #strange #adventure #action #tropics #sexy #babes #surgery
In Zipolite, Oaxaca, el Zopilote del Norte kayak-surfs at the beach and observes the local culture. >> http://bit.ly/7CuPWX
One of my favourite stories is "Lighten Up, Papa Chango", you can read it here: http://bit.ly/8eHZiZ

...

dickstravels-oldTRStweets-replaced.txt

http://bit.ly/se76SD
The Road South #ebook blends black #humour, #travel, #sex, #fantasy, social issues, & extreme kayaking. >> http://bit.ly/se76SD
Check out The Road South short story collection and listen to them too! >> http://bit.ly/se76SD
Road South #ebook >> http://bit.ly/se76SD #fiction #travel #humour #kayak #safari #nude #revolution
Road South #ebook >> http://bit.ly/se76SD #fiction #culture #diving #fantasy #farming #humour

http://bit.ly/tPdGdz

Sexy Women, Men of Steel http://bit.ly/tPdGdz #story #fiction #travel #tropics #babe #reading

http://bit.ly/thZqF8

Papa Chango http://bit.ly/thZqF8 #story #fiction #strange #adventure #action #tropics #babes #surgery

http://bit.ly/vBkKfF

Totally Unreal http://bit.ly/vBkKfF #story #fiction #fire #cyber #punk #conformist #VR #tech #future

http://bit.ly/ufymzE

Sexy Women, Men of Steel

http://bit.ly/tPdGdz

Sexy Women, Men of Steel >> http://bit.ly/tPdGdz #fiction #travel via @DicksTravels
Short story "Sexy Women, Men of Steel" http://bit.ly/tPdGdz #prose #fiction #travel #culture
So beautifully written, it borders on poetry, rather than prose. Not a word is wasted. ~ @Literatrix http://bit.ly/tPdGdz
Sexy Women, Men of Steel >> http://bit.ly/tPdGdz #story #fiction #travel #babe

It’s got to be her milk that makes them wild. A suckle would perk me up too when I’m feeling droopy. http://bit.ly/tPdGdz
One night she breaks. In the darkened kitchen I find her crying. The other kids are asleep. http://bit.ly/tPdGdz
Growing up to be sexy ladies. Fire in their eyes to scorch men of steel. http://bit.ly/tPdGdz
He’s putting a match to the fuse and opening the window, hurling a home-made bomb at the hostile rain. http://bit.ly/tPdGdz
"So ashamed they can’t ogle her tits." Sexy Women, Men of Steel >> http://bit.ly/tPdGdz #story #travel
"Mujeres sensitivas, hombres fuertes y de acero... sensitive women, strong men of steel" (los Angeles del Puerto) http://bit.ly/tPdGdz
Lighten Up, Papa Chango

http://bit.ly/thZqF8

Voluptuous Beach Girls http://bit.ly/thZqF8 #story #fiction #strange #adventure #action #tropics #babes #surgery
Lighten Up, Papa Chango http://bit.ly/thZqF8 #story #fiction #strange #adventure #action #tropics #sexy #babes #surgery
In Zipolite, Oaxaca, el Zopilote del Norte kayak-surfs at the beach and observes the local culture. >> http://bit.ly/thZqF8
One of my favourite stories is "Lighten Up, Papa Chango", you can read it here: http://bit.ly/thZqF8

...

link-tweets.pl

# usage example:
# perl link-tweets.pl "C:\Users\Bevan\Documents\My Work\Dick's Travels\dickstravels-tweets-by-titles-with-links"

# lineage:
# from replace-links-in-tweets.pl

# description:
# Takes bit.ly links on their own line (no spaces after it allowed!) as the current appendix
# For subsequent lines...
# if they have link(s), the link(s) is replaced with the current replacement...
# regardless of how many links there are or how long or short they are!
# if they do not have a link,
# if there are hashtag(s)
# a link is inserted as signified by {L}
# otherwise,
# a link is appended
# and then if the resulting line is too long for a tweet, it is marked with its length

# written by Bevan Bird... Find me at http://bevanbird.com

use English;

# get input from command line
my $inputfile = $ARGV[0];
my $outputfile = $inputfile;
$inputfile .= ".txt";
$outputfile .= "-linked.txt";

open(INPUT, "<", $inputfile) or die $!;
open(OUTPUT, ">", $outputfile) or die $!;

my $replacement_link;

while(<INPUT>) {
chomp;
#print "$_\n";
if ($_ =~ /^http:\/\/bit\.ly\/[^\s]+$/){
$replacement_link = $_;
#print "$replacement_link\n";
}else{
$_ =~ s/(http:\/\/[^\s]+)/$replacement_link/g;
if (!($_ =~ /http:\/\/bit\.ly\/[^\s]+/) && length($_)){

$l = length($replacement_link) + length($_) + 1;
if ($_ =~ /\#/){
$l -= 4;
$replacer = include_link_left_delimter($l) . $replacement_link;
$_ =~ s/{L}/$replacer/;
}else{
$replacer = " " . include_link_left_delimter($l) . $replacement_link;
$_ .= $replacer;
}
$l = length($_);
}
if ($l>140){ #too long for a tweet
$_ = "************************************ TOO LONG ("
. $l . " characters here... ) ***************************** " . $_;
}
}
print OUTPUT $_ . "\n";
}

close(INPUT) or die $!;
close(OUTPUT) or die $!;

sub include_link_left_delimter
{
my $l = shift;
if ($l>140){ #too long for a tweet
return ">> ";
}elsif ($l<138){
return ">> ";
}elsif ($l<139){
return "> ";
}else{
return "";
}
}

dickstravels-OTC-tweets.txt

http://bit.ly/t6u0Zg

If you think I'm going to sleep on the beach now, you're mistaken.
She’s a bit nervous already, the thought of being bait for an ambush / massacre won't do anything for her tranquility.
Having these two garrulous, gastrically-stricken policemen guffawing in her bar wasn't doing her trade any good.
That wasn't my fault, it was a government customs-man who did the typing.
People found their bank accounts wiped out overnight by a stroke of a pen.
I opened the hood and a flock of rats jumped out!
We had our hammocks slung from hooks in the walls and had a comfortable night's sleep.
Finally we got our traveller's cheques in Merida.
Following the route I'd taken in '74... Chichen Itza, Valladolid, Cancun and Tulum, my favourite beach.
We devoted our days to sunbathing and swimming and diving in the sea and the coral reef and spearing fish.
Getting coconuts down from the palms and with my machete opening them and drinking the juice. Having a lovely time.
We were getting ready for bed and saw a search light come through the trees... We froze.
I said, "Don't worry, the Army's on our side."
Too bad, we really need to buy a car... they looked hungrily at the Fiat.
A little bullshit goes a long way in Mexico.
Usually you get double the value of a car in Mexico... and the Army... they've probably got lots of money.
He and a corporal got in the car and they drove off at furious speed crashing through the bush, falling into potholes.
With the night vision binoculars I could see the outline of a yatch and three people on deck dropping the sails.

http://bit.ly/thZqF8

Sita and Will are frolicking naked in the foam.
Two sunburnt gringas in remnants of dresses lean over the hull of an upturned boat.
‘Don’t want to get the fishermen too excited,’ Sita grins. ‘They’re crazy enough already.’
Two sunburnt gringas... The wind playfully tosses their dresses up over their backsides.
Will’s eyes glaze with lust as he noses the car down the loose gravel path to the landing-stage.
They gesture at the beached fishing boats. ‘They came in empty. No more today.’
‘Don’t stop!’ he yells as I hesitate over the clutch pedal, then think better of it.
I slam on the pedals to hold it balanced over nothing for a few agonizing seconds...
‘A bowline on the bumper and a turn around that tree!’ My voice is a chango-monkey screech.
‘Hey, lighten up old man,’ says Sita severely. ‘You look like a bomb about to explode.’
My dread of crashing into concrete is replaced by terror of a gap in the boards...
I'm feeling fragile in the flesh and leery of wrecked cars and machinery in general.
A mureña - moray (wolf) eel? Why don’t you kill it before it bites you?’
‘You jabbed it in the face?’ he exclaims in wonder. ‘And it let you go?’

dickstravels-OTC-tweets-linked.txt

http://bit.ly/t6u0Zg

If you think I'm going to sleep on the beach now, you're mistaken. >> http://bit.ly/t6u0Zg
She’s a bit nervous already, the thought of being bait for an ambush / massacre won't do anything for her tranquility. http://bit.ly/t6u0Zg
Having these two garrulous, gastrically-stricken policemen guffawing in her bar wasn't doing her trade any good. >> http://bit.ly/t6u0Zg
That wasn't my fault, it was a government customs-man who did the typing. >> http://bit.ly/t6u0Zg
People found their bank accounts wiped out overnight by a stroke of a pen. >> http://bit.ly/t6u0Zg
I opened the hood and a flock of rats jumped out! >> http://bit.ly/t6u0Zg
We had our hammocks slung from hooks in the walls and had a comfortable night's sleep. >> http://bit.ly/t6u0Zg
Finally we got our traveller's cheques in Merida. >> http://bit.ly/t6u0Zg
Following the route I'd taken in '74... Chichen Itza, Valladolid, Cancun and Tulum, my favourite beach. >> http://bit.ly/t6u0Zg
We devoted our days to sunbathing and swimming and diving in the sea and the coral reef and spearing fish. >> http://bit.ly/t6u0Zg
Getting coconuts down from the palms and with my machete opening them and drinking the juice. Having a lovely time. >> http://bit.ly/t6u0Zg
We were getting ready for bed and saw a search light come through the trees... We froze. >> http://bit.ly/t6u0Zg
I said, "Don't worry, the Army's on our side." >> http://bit.ly/t6u0Zg
Too bad, we really need to buy a car... they looked hungrily at the Fiat. >> http://bit.ly/t6u0Zg
A little bullshit goes a long way in Mexico. >> http://bit.ly/t6u0Zg
Usually you get double the value of a car in Mexico... and the Army... they've probably got lots of money. >> http://bit.ly/t6u0Zg
He and a corporal got in the car and they drove off at furious speed crashing through the bush, falling into potholes. http://bit.ly/t6u0Zg
With the night vision binoculars I could see the outline of a yatch and three people on deck dropping the sails. >> http://bit.ly/t6u0Zg

http://bit.ly/thZqF8

Sita and Will are frolicking naked in the foam. >> http://bit.ly/thZqF8
Two sunburnt gringas in remnants of dresses lean over the hull of an upturned boat. >> http://bit.ly/thZqF8
‘Don’t want to get the fishermen too excited,’ Sita grins. ‘They’re crazy enough already.’ >> http://bit.ly/thZqF8
Two sunburnt gringas... The wind playfully tosses their dresses up over their backsides. >> http://bit.ly/thZqF8
Will’s eyes glaze with lust as he noses the car down the loose gravel path to the landing-stage. >> http://bit.ly/thZqF8
They gesture at the beached fishing boats. ‘They came in empty. No more today.’ >> http://bit.ly/thZqF8
‘Don’t stop!’ he yells as I hesitate over the clutch pedal, then think better of it. >> http://bit.ly/thZqF8
I slam on the pedals to hold it balanced over nothing for a few agonizing seconds... >> http://bit.ly/thZqF8
‘A bowline on the bumper and a turn around that tree!’ My voice is a chango-monkey screech. >> http://bit.ly/thZqF8
‘Hey, lighten up old man,’ says Sita severely. ‘You look like a bomb about to explode.’ >> http://bit.ly/thZqF8
My dread of crashing into concrete is replaced by terror of a gap in the boards... >> http://bit.ly/thZqF8
I'm feeling fragile in the flesh and leery of wrecked cars and machinery in general. >> http://bit.ly/thZqF8
A mureña - moray (wolf) eel? Why don’t you kill it before it bites you?’ >> http://bit.ly/thZqF8
‘You jabbed it in the face?’ he exclaims in wonder. ‘And it let you go?’ >> http://bit.ly/thZqF8

Related posts:

About Bevan

I’m the Inspirational Connector, and I connect people in inspiring ways! “Make happy those who are near, and those who are far will come.” ~ Chinese Proverb
This entry was posted in Uncategorized. Bookmark the permalink.