Guest User

Untitled

a guest
Jan 22nd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. function tweetThread(stringToTweet,delim,img,r){ //pass a string variable with additional option to parse (used for bookending tweets)
  2. //////////////////////////Variables////////////////////////////////////////////////////////
  3. var ss = SpreadsheetApp.getActiveSpreadsheet(); //spreadsheet
  4. var twtMax = 280; //Character limit for Twitter (variable in case it changes)
  5. var tweets = new Array(0); //create final array to populate with tweets
  6. var total = ""; //variable for 'total' tweet-length section if parsing is required
  7. var start = 0; //starting character for .substring() function, default 0
  8. var excess = 0; //number of characters to strip off of 'total' to remove split words at end of tweet
  9. var parsed = ""; //parsed tweet (total - excess) to add to tweets
  10. var url = ""; //parsed image URLs
  11. console.log("tweetThread initiated!"); //log function initiation
  12. console.log("using params: " //log paramaters being passed through function
  13. + "|stringToTweet:" + stringToTweet
  14. + "|delim:" + delim
  15. + "|img:" + img);
  16. //////////////////////////Length Check/////////////////////////////////////////////////////
  17. try { //Attempt to check string value for parsing needs
  18. if (stringToTweet == undefined) {throw "Undefined Value: 'stringToTweet'"}; //thrown when tweet string is undefined
  19. var thread = stringToTweet.split(delim); //split initial string along provided delimeter
  20. console.log("Input Thread Length: " + thread.length); //log number of strings in thread
  21. } catch(e) { //Catch thrown error
  22. console.log("Cannot Split Thread. Error: " + e); //Log error
  23. }
  24. try { //attempt to evaluate parsed tweet thread
  25. if (thread == undefined) {throw "Undefined Value: 'thread'"}; //thrown when thread value doesn't exist
  26. for (i in thread){ //for each string in thread...
  27. if (thread[i].length == 0) {throw "Invalid Value: thread[" + i + "]"}; //thrown when thread has string error
  28. console.log("Thread section characters: " + thread[i].length); //log string length
  29. if (thread[i].length > twtMax){ //check thread[i] length
  30. console.log("Cannot Tweet! Too long! Parsing..."); //if thread [i] too long for tweet, needs to be split
  31. start = 0; //reset starting character for substring
  32. //////////////////////////Parsing Loop/////////////////////////////////////////////////////
  33. while (start<thread[i].length) { //As long as the starting position is NOT at the end of thread[i]...
  34. total = thread[i].substring(start,start + twtMax); //full length of tweet
  35. excess = total.length - total.lastIndexOf(" "); //amount of characters to cut off of 'total' to get to a space (end of last full word)
  36. parsed = thread[i].substring(start,start + twtMax - excess); //new text of tweet-length string ending in full word
  37. start = start + parsed.length + 1; //new start position is the first character after finished tweet
  38. tweets.push(parsed); //adds tweet to thread
  39. console.log("Parsed new tweet," + " length: " +
  40. parsed.length +
  41. " | Tweetable! Adding to tweet thread"); //log parse length, success
  42. console.log("Tweet Added: " + parsed);
  43. } //end parsing loop
  44. console.log("Parsing Complete"); //log parsing complete
  45. } else { //if thread[i] is tweet length...
  46. console.log("Tweetable! Adding to tweet thread"); //log length
  47. tweets.push(thread[i]) //add thread[i] to tweets
  48. console.log("Tweet Added: " + thread[i]);
  49. console.log("Tweet Thread length: " + tweets.length);
  50. }
  51. } //end loop
  52. } catch (e) { //Catch thrown error
  53. console.log("Parsing Error. Error: " + e); //Log error
  54. }
  55. //////////////////////////Split Images/////////////////////////////////////////////////////
  56. if (img){ //if image string has been passed...
  57. var imgs = img.split(","); //split CSV into individual URLs
  58. console.log(img + " contains " + imgs.length + " values!"); //log number of images found
  59. } else { //if no image string has been passed...
  60. console.log("No images passed through threading function"); //log no images
  61. } //end image processing
  62. //////////////////////////Output///////////////////////////////////////////////////////////
  63. if (r) {
  64. var replyTo = r
  65. } else {
  66. var replyTo = 0; //set reply (each tweet will reply to previous, first will be null
  67. }
  68. for (i in tweets){ //for each tweet in thread...
  69. console.log("Tweet Attempt: " + i); //log attempt number (starting @ 0)
  70. try{
  71. if (imgs[i]) { //if valid image URL exists...
  72. url = imgs[i].toString(); //set URL variable for tweet function
  73. console.log("imgs[" + i + "]: " + imgs[i]); //log image
  74. } else {
  75. url = null;
  76. console.log("imgs[" + i + "]: " + imgs[i]);
  77. }
  78. } catch (e) {
  79. url = null; //null variable for tweet function
  80. console.log("imgs[" + i + "] parse error: " + e); //log image error
  81. }
  82. var newTweet = sendTweet(tweets[i], replyTo, url); //set reply value for next tweet as return of tweet function
  83. Utilities.sleep(250); //wait 1/4 second (for return value, to avoid errors)
  84. if (i == 0) { //if first tweet in thread...
  85. var begins = newTweet //set value for first tweet ID
  86. console.log("Thread begins with tweet(ID): " + begins); //log first tweet ID
  87. }
  88. replyTo = newTweet //set replyTo value to response of last tweet
  89. if (replyTo == undefined) { //if no response...
  90. console.log("replyTo undefined! Breaking loop..."); //log undefined response
  91. break; //break thread, do not tweet more
  92. } else { //if response...
  93. console.log("Next Reply To: " + replyTo); //log reply
  94. }
  95. } //end thread tweeting
  96. return begins;
  97. } //END
Add Comment
Please, Sign In to add comment