2013-03-09 311 views
0

我正在使用Google腳本來獲取電子郵件的正文。我試圖只保存包含在普通YouTube電子郵件中的鏈接,該電子郵件會通知您新用戶上傳的內容。我希望只使用JavaScript保存點擊郵件中包含的圖像時所遵循的鏈接。下面看到的是郵件的一部分,外觀的例子:通過電子郵件過濾掉YouTube視頻的鏈接

email sample

電子郵件的內容是這樣的:

<div> 
<table width="620" cellspacing="0" cellpadding="0" border="0" align="center"><tr><td bgcolor="#F0F0F0"> 
<table width="578" cellspacing="0" cellpadding="0" border="0" align="center"> 
<tr> 
<td height="16"></td> 
</tr> 
<tr> 
<td align="center" valign="top"> 
<img src="http://s.ytimg.com/yt/img/email/digest/email_header.png"> 
</td> 
</tr> 
<tr> 
<td height="16"></td> 
</tr> 
<tr> 
<td align="left" bgcolor="#FFFFFF"> 
<div style="border-style:solid;border-width:1px;border-color:#cccccc"> 
<table width="578" cellspacing="0" cellpadding="0" border="0" align="center"> 
<tr> 
<td height="22" colspan="3"></td> 
</tr> 
<tr> 
<td width="40"></td> 
<td width="498"> 
<table cellspacing="0" cellpadding="0" border="0"> 
<tr> 
<td bgcolor="#FFFFFF" align="left" width="50"> 
<img src="https://i.ytimg.com/i/N5jZlcpSP00e_zShDw9EfQ/1.jpg" height="50" width="50"> 
</td> 
<td width="16"></td> 
<td> 
<div style="font-family:arial,Arial,sans-serif;font-size:18px;color:#333333;line-height:24px" height="59"> 
FatalDubstepHD has uploaded <b>Empressto - No Love [Dubstep]</b>. 
</div> 
</td> 
</tr> 
</table> 
<table cellspacing="0" cellpadding="0" border="0"> 
<tr><td height="22"></td></tr> 
</table> 
<div style="border-bottom:1px solid #e1e1e1"></div> 
<table cellspacing="0" cellpadding="0" border="0"> 
<tr><td height="22"></td></tr> 
</table> 
<table cellspacing="0" cellpadding="0" border="0" style="width:480px;margin:0 9px"> 
<tr> 
<td> 
<a href="http://www.youtube.com/watch?v=zTYri0ei45Q&amp;feature=em-uploademail" style="text-decoration:none;display:block" class="nonplayable" target="_blank"> 
<img src="http://i2.ytimg.com/vi/zTYri0ei45Q/hqdefault.jpg" height="360" width="480" border="0"> 
</a> 
</td> 
</tr> 
<tr><td height="16"></td> 
</tr> 
<tr> 
<td width="480"> 
<a href="http://www.youtube.com/watch?v=zTYri0ei45Q&amp;feature=em-uploademail" style="text-decoration:none" target="_blank"> 
<div style="font-family:arial,Arial,sans-serif;font-size:15px;color:#333333;line-height:20px;font-weight:bold"> 
Empressto - No Love [Dubstep] 
</div> 
</a> 
<div style="font-family:arial,Arial,sans-serif;font-size:13px;color:#999999;line-height:20px"> 
by 
<a href="http://www.youtube.com/user/FatalDubstepHD?feature=em-uploademail" style="font-size:13px;color:#999999;line-height:20px;text-decoration:none" target="_blank">FatalDubstepHD</a> 
</div> 
</td> 
</tr> 
</table> 
<table cellspacing="0" cellpadding="0" border="0"> 
<tr><td height="16"></td></tr> 
</table> 
<table cellspacing="0" cellpadding="0" border="0"> 
<tr> 
<td width="498"> 
<div style="font-family:arial,Arial,sans-serif;font-size:13px;color:#333333;line-height:20px"> 
● Free Download<br><a href="https://soundcloud.com/empresstoro/empressto-no-love" target="_blank">https://soundcloud.com/<WBR>empresstoro/empressto-no-love</a><br><br>● Empressto <br><a href="http://www.facebook.com/EmpreSSto" target="_blank">http://www.facebook.com/<WBR>EmpreSSto</a><br><a href="http://www.twitter.com/Empressto" target="_blank">http://www.twitter.com/<WBR>Empressto</a><br><a href="http://www.youtube.com/user/TheOfficialEmpressto" target="_blank">http://www.youtube.com/user/<WBR>TheOfficialEmpressto</a><br><br>● Fatal Dubstep<br><a href="http://www.facebook.com/FatalMusicUK" target="_blank">http://www.facebook.com/<WBR>FatalMusicUK</a><br><a href="http://www.twitter.com/FatalMusicUK" target="_blank">http://www.twitter.com/<WBR>FatalMusicUK</a><br><br>● Owner Of Fatal<br><a href="http://www.twitter.com/Mihalis_Costa" target="_blank">http://www.twitter.com/<WBR>Mihalis_Costa</a><br><br>● Contact Email/Submit Music<br><a href="mailto:[email protected]" target="_blank">[email protected]</a> 
</div> 
</td> 
</tr> 
</table> 
</td> 
<td width="40"></td> 
</tr> 
<tr> 
<td height="22" colspan="3"></td> 
</tr> 
</table> 
</div> 
</td> 
</tr> 
<tr> 
<td height="16"></td> 
</tr> 
<tr> 
<td align="center"> 
<table cellspacing="0" cellpadding="0" border="0" align="center"> 
<tr> 
<td width="40"></td> 
<td width="498"> 
<div style="color:#999999;line-height:14px;font-size:11px;font-family:arial,Arial,sans-serif"> 
You were sent this email because you chose to receive updates about new videos uploaded by <a href="http://www.youtube.com/user/FatalDubstepHD?feature=em-uploademail" style="text-decoration:none;color:#1c62b9" target="_blank">FatalDubstepHD</a>. If you don&#39;t want these updates anymore, you can change your preferences by visiting the <a href="http://www.youtube.com/subscription_manager" style="text-decoration:none;color:#1c62b9" target="_blank">subscription manager</a>. 
</div> 
</td> 
<td width="40"></td> 
</tr> 
</table> 
</td> 
</tr> 
<tr> 
<td height="16"></td> 
</tr> 
<tr> 
<td> 
<table cellspacing="0" cellpadding="0" border="0" align="center"> 
<tr> 
<td width="40"></td> 
<td width="498"> 
<div style="color:#999999;font-size:11px;line-height:14px;font-family:arial,Arial,sans-serif"> 
©2013 YouTube, LLC 901 Cherry Ave, San Bruno, CA 94066 
</div> 
</td> 
<td width="40"></td> 
</tr> 
</table> 
</td> 
</tr> 
<tr> 
<td height="22"></td> 
</tr> 
</table> 
</td></tr></table> 
</div> 

這裏的問題是,如何我篩選後的網址從這樣的電子郵件,並且它看起來像文本3D被添加到網址,這是使網站無效,所以我也需要從網址中刪除。我怎麼能做到這一點的任何想法?

編輯:

這裏是我使用的,它缺少解析數據的代碼:

function processInbox() { 
    // get all threads in inbox 
    var label = GmailApp.getUserLabelByName("Social/YouTube/Dubstep"); 
    var doneLabel = GmailApp.getUserLabelByName("Social/YouTube/Dubstep (added)"); 
    var messages = label.getThreads(); 
    var max = Math.min(messages.length,1); 
    for(var i = 0; i < max; ++i) { 
    var email = messages[i].getMessages()[0]; 
    var contents = email.getBody(); 

    //PARSE HERE 
    var xml = Xml.parse(contents,true); 
    Logger.log(xml); 
    GmailApp.sendEmail('MY EMAIL', 'Xml Contents', Logger.getLog()); 



    //messages[i].markRead(); 
    //messages[i].removeLabel(label).addLabel(doneLabel); 
    } 
}; 

EDIT2:新代碼:

function processInbox() { 
    // get all threads in inbox 
    var label = GmailApp.getUserLabelByName("Social/YouTube/Dubstep"); 
    var doneLabel = GmailApp.getUserLabelByName("Social/YouTube/Dubstep (added)"); 
    var messages = label.getThreads(); 
    var max = Math.min(messages.length,5); 
    for(var i = 0; i < max; ++i) { 
    var email = messages[i].getMessages()[0]; 
    var contents = email.getBody(); 
    var st = contents.indexOf('<a href="http://www.youtube.com/watch?v='); 
    var str1 = contents.substr(st+9,80); 
    var end = str1.indexOf('&amp'); 
    var link = str1.substring(0,end); 
    Logger.log(link); 
    GmailApp.sendEmail('EMAIL', 'Xml Contents', Logger.getLog()); 

    Logger.clear(); 

    //messages[i].markRead(); 
    //messages[i].removeLabel(label).addLabel(doneLabel); 
    } 

回答

1

我想最好的辦法就是使用XML service來解析HTML並從那裏獲取元素。

documentation here,設置寬大參數true

我前一段時間使用類似的方法in this post,你可以看到的想法...


編輯: 我沒有」 t成功地用這種方法... 我嘗試了別的使用字符串操作:

... 
var contents = email.getBody(); 
var st = contents.indexOf('<a href="http://www.youtube.com/watch?v='); 
var str1 = contents.substr(st+9,80); 
var end = str1.indexOf('&amp'); 
var link = str1.substring(0,end); 
Logger.log(link);  

它適用於這種情況下,你將不得不檢查它是否一直工作。

注:我測試了來自YouTube通過電子郵件發送給其他共享鏈接,它似乎每次;-)

+0

這種方法是不是真的使用功能在谷歌的腳本,然後是它的工作?我使用getBody()更新了其中一封電子郵件的確切輸出。實際上是否有使用Google腳本的內置函數來獲取XML數據的方法? – ComputerLocus 2013-03-09 22:29:38

+0

?? XML服務是Google腳本中的默認服務......我並不真正瞭解您的評論。抱歉,如果我錯過了什麼。 – 2013-03-09 22:37:43

+0

我添加了我目前正在使用的代碼。有沒有什麼方法可以像我現在這樣使用代碼,還是必須完全改變它來使用你的XML方法?如果我必須改變它,我會怎樣做呢?我對JavaScript和Google腳本非常陌生,所以我一直在尋找一個基本的解決方案,但我想這不會是一個? – ComputerLocus 2013-03-09 22:42:03