0

I'm using Zapier to run a search on Twitter, create a daily digest of results (Zapier Digest), and send the digest to a mailing list (Mailgun). I seek to format the Tweets similar to Twitter's own emails where the tweet isn't text but HTML where any user mention (@name, corresponding URL is https://twitter.com/[NAME]) and URL in the tweet are actual links. (I don't want the conversion for any hashtags.)

This search, extract, and replace (with added HTML) isn't that hard. But it's too much for Zapier's simple Formatter text functions. So I turned to the Zapier Code action, thinking I could borrow code as I'm not a hardcore coder.

I found Python code as below (and others at that site). After a couple hours of testing I've given up on being able to successfully adapt it for use in Zapier Code. Zapier has limitations on how it uses Python. Their documentation on it is primitive.

Could someone here familiar with Zapier help me with converting that (or similar) code so I could run it on Zapier Python or Javascript? I truly would appreciate it.

Thank you, Marc


https://coderwall.com/p/wdtkhw/convert-links-username-mentions-and-hashtags-in-a-tweet

function twitterify($ret) {
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
    $ret = preg_replace("/#(\w+)/", "<a href=\"http://twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
    return $ret;

}
Rachit kapadia
  • 705
  • 7
  • 18

2 Answers2

0

David here, from the Zapier Platform team.

Marc, your main problem is that you're using a PHP snippet in our Python interpreter, which is probably why it's giving you trouble. If you have specific questions or there's something we can improve about our documentation, definitely let me know!

I'd check out the python re.sub() function instead. There's a great answer about it here. You'll also want to be able test your regex, for which there's a great site (as there are some sleight differences between PHP and Python regex. There's also a great language resource here if you want to learn more about the Python language.

​Let me know if you've got any other questions!

xavdid
  • 5,092
  • 3
  • 20
  • 32
0

David,

Regarding documentation, the help page is oriented to someone who has Python or Javascript experience. I don't. The following would help.

  • In-code commenting. For the most part, the help just says here's what an example does and shows the code. It doesn't explain what each line of code does.
  • The help virtually skips to advanced examples, such as comma separated lists. More basic examples would provide firmer ground for newbies like me.

I continued testing and came up with the following code for Zapier Python, based on the snippet above. While it executed without error (progress!), it made no change to the @user or URL in the input data test tweet.

Marc

import re
tweetHTML = re.sub('/@(\w+)/', '<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>', input_data ['tweet'])
tweetHTML = re.sub('#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#', '\\1<a href=\"\\2\" target=\"_blank\">\\2</a>', tweetHTML)
tweetHTML = re.sub('#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#', '\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>', tweetHTML)
return {'tweetHTML': tweetHTML}