【发布时间】:2015-02-28 07:52:04
【问题描述】:
发现这种方法可以从 gmail 获取未读电子邮件计数,但它在我的 mac 上不起作用:
我认为 mac 上的 sed 实现可能不是“标准”linux。
这是我正在努力解决的问题:
MAILCOUNTER=`curl -u $USERID:$PASSWORD --silent "https://mail.google.com/mail/feed/atom" \
| sed -n 's|<fullcount>\(.*\)</fullcount>|\1|p'`
USERID=youremail@gmail.com
PASSWORD=yoursecretpassword
WAIT=10
# Loop to check for new mail every X minutes:
while [ "1" -eq "1" ]; do
# Command line to fetch the number of unread emails:
MAILCOUNTER=`curl -u $USERID:$PASSWORD --silent "https://mail.google.com/mail/feed/atom" \
| sed -n 's|<fullcount>\(.*\)</fullcount>|\1|p'`
if [[ "$MAILCOUNTER" = "" ]]; then
echo "ERROR: The program coulndn't fetch the account for user \"$USERID\"."
echo "- Are you connected to the Internet?"
echo -e "- Is the userid and password correct for \"$USERID\"?\n"
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount=-1&
elif [[ "$MAILCOUNTER" -eq "0" ]]; then
echo "* There is 0 new email for user $USERID."
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount=0&
elif [[ "$MAILCOUNTER" -gt "0" ]]; then
echo "* There is $MAILCOUNTER new email for user $USERID."
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount=$MAILCOUNTER&
fi
echo "* Waiting $WAIT seconds before checking for emails again."
echo "* (^C to quit the program)"
sleep $WAIT
done
像这样的 curl 错误,这让我抓狂,因为在我的浏览器中输入 URL 会正确返回如下错误
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
网址返回:
<feed xmlns="http://purl.org/atom/ns#" version="0.3">
<title>Gmail - Inbox for myGmailName@gmail.com</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>1</fullcount>
<link rel="alternate" href="http://mail.google.com/mail" type="text/html"/>
<modified>2015-01-02T00:14:36Z</modified>
<entry>
我更正了 google 邮件权限,现在 curl 返回了
我从中得到:
curl -u MYUSRNAME:MYPASSWORD --silent "https://mail.google.com/mail/feed/atom"
这个:
<?xml version="1.0" encoding="UTF-8"?><feed version="0.3" xmlns="http://purl.org/atom/ns#"><title>Gmail - Inbox for MYEMAIL@gmail.com</title><tagline>New messages in your Gmail Inbox</tagline><fullcount>1</fullcount><link rel="alternate" href="http://mail.google.com/mail" type="text/html" /><modified>2015-01-02T02:03:50Z</modified><entry><title>test</title><summary>test</summary><link rel="alternate" href="http://mail.google.com/mail?account_id=MYEMAIL@gmail.com&message_id=14aa8623548638bc&view=conv&extsrc=atom" type="text/html" /><modified>2015-01-02T02:03:36Z</modified><issued>2015-01-02T02:03:36Z</issued><id>tag:gmail.google.com,2004:1489150113099430076</id><author><name>the author</name><email>themail@email.com</email></author></entry></feed>MacBookPro-2:arduino_gmail_checker admin$
我只想要这里的号码:
<fullcount>1</fullcount>
【问题讨论】:
-
它以什么方式“不起作用”?如果您将车开到车库并要求机械师修理它,因为它“不起作用”,您会期望他们能够帮助您吗?