
标题: [其他] 中年入门程序员作品,Mac下自动下载Bing每日墙纸的Python script,16楼 [打印本页]
作者: eva3d 时间: 2013-9-25 13:56 标题: 中年入门程序员作品,Mac下自动下载Bing每日墙纸的Python script,16楼
复制内容到剪贴板
代码:
#!/usr/bin/env python
import os
import md5
import pprint
import sys
import subprocess
from time import strftime
from urllib import URLopener
from urllib2 import urlopen
from xml.dom.minidom import parseString
# Defines source and destination of image
rss_feed = 'http://feeds.feedburner.com/bingimages';
dst_dir = os.path.expanduser('~/Pictures/DeskFeed/')
# Originally found on: http://hints.macworld.com/article.php?story=20110721062846337
# Applescript modified for dual monitors by: ViVIDboarder
SCRIPT = """/usr/bin/osascript<<end
tell application "System Events"
set picture of every desktop to POSIX file "%s"
end tell
END"""
def set_desktop_background(destination):
subprocess.Popen(SCRIPT%destination, shell=True)
def parseFeed(rss):
destination = "%s%s.jpg" % (dst_dir, strftime( "%y-%m-%d")
if os.path.exists(destination):
sys.exit(0)
try:
rss_contents = urlopen( rss )
except:
print "Failed to read rss feed %s" % rss
return
rss_src = rss_contents.read()
rss_contents.close()
dom = parseString( rss_src )
firstitem = dom.getElementsByTagName('item')[0]
link = firstitem.getElementsByTagName( 'enclosure' )[0].getAttribute('url')
URLopener().retrieve(link, destination)
set_desktop_background(destination)
def main():
parseFeed(rss_feed)
if __name__ == "__main__":
main()
</end
源代码奉上
这个是用来自动获取每日bing壁纸,并且应用到Mac壁纸的一段python
使用起来有两个不方便
1. feedsburner被墙了,运行不畅
2. 即使想办法获取到了feed,得到实际图片也是 http://www.istartedsomething.com/bingimages/ 这里的,分辨率最高只有1366x768
经过我的试验,发现可以从bing首页入手,修改文件名获取高分辨率墙纸
例如打开今日的bing首页,html源码中可以找到复制内容到剪贴板
代码:
background-image:url(http://s.cn.bing.net/az/hprichbg/rb/AustriaAutumn_ZH-CN11875113209_1366x768.jpg)
可截取出今日壁纸的有效地址复制内容到剪贴板
代码:
http://s.cn.bing.net/az/hprichbg/rb/AustriaAutumn_ZH-CN11875113209_1366x768.jpg
然后将后面的 1366x768 改为 1920x1080 即改为复制内容到剪贴板
代码:
http://s.cn.bing.net/az/hprichbg/rb/AustriaAutumn_ZH-CN11875113209_1920x1080.jpg
即可下载到本日Bing壁纸的高清版本
请移除原代码中关于feed获取的部分,添加刚才上述的从bing首页获取文件地址,改文件名下载的部分
为感谢连续一周奉上全部祭扫
[ 本帖最后由 eva3d 于 2013-9-25 17:16 编辑 ]
作者: 蚁力神 时间: 2013-9-25 14:04
猫熊爹你不觉得你太高端了么
作者: eva3d 时间: 2013-9-25 14:06
posted by wap, platform: XBOX引用:
原帖由 @蚁力神 于 2013-9-25 14:04 发表
猫熊爹你不觉得你太高端了么
喵的,现在只会写文案和画PPT的屌丝看见各种代码就尿了...
作者: ffcactus 时间: 2013-9-25 14:33
关键是没几个人现在用 Mac 并装了python呀。
作者: 蚁力神 时间: 2013-9-25 14:36
引用:
原帖由 ffcactus 于 2013-9-25 14:33 发表 
关键是没几个人现在用 Mac 并装了python呀。
我装了,主要是搭梯子用
作者: zztg 时间: 2013-9-25 14:40
虽然明知ff是吃了药....不过我还是说下osx自带了python....而且10.9也把版本跟进到2.7.5了....10.8好像还是2.7
作者: 黑暗骑士巫妖王 时间: 2013-9-25 14:58
posted by wap, platform: Chrome
你的思路没问题,但是截取的办法有问题,bing首页的背景是用异步载入的,你直接读bing首页源代码是没有背景图片字段的。
作者: ffcactus 时间: 2013-9-25 15:07
posted by wap, platform: iPhone引用:
原帖由 @zztg 于 2013-9-25 14:40 发表
虽然明知ff是吃了药....不过我还是说下osx自带了python....而且10.9也把版本跟进到2.7.5了....10.8好像还是2.7
我又没有mac,我怎么知道带不带,你们全家吃了药还差不多,用苹果的神经病果然比较多。
作者: eva3d 时间: 2013-9-25 16:41
自力更生ing,翻了几个教程,已经成功替换出正确的url来了复制内容到剪贴板
代码:
#---------------------------------import---------------------------------------
import urllib2;
import re;
#------------------------------------------------------------------------------
def main():
userMainUrl = "http://cn.bing.com";
req = urllib2.Request(userMainUrl);
resp = urllib2.urlopen(req);
respHtml = resp.read();
WallPaper = re.search("g_img={url:'(?P<wpurl>.+?)'", respHtml);
print "WallPaper=",WallPaper;
if(WallPaper):
wpurl = WallPaper.group("wpurl");
print "wpurl=",wpurl;
newurl = wpurl.replace('1366x768','1920x1080');
print "newurl=",newurl;
###############################################################################
if __name__=="__main__":
main();
运行结果复制内容到剪贴板
代码:
python test.py
WallPaper= <_sre.SRE_Match object at 0x106502468>
wpurl= http://s.cn.bing.net/az/hprichbg/rb/AustriaAutumn_ZH-CN11875113209_1366x768.jpg
newurl= http://s.cn.bing.net/az/hprichbg/rb/AustriaAutumn_ZH-CN11875113209_1920x1080.jpg
作者: zj14 时间: 2013-9-25 16:43
posted by wap, platform: Chrome
#!/usr/bin/env python
import os
import pprint
import sys
import subprocess
import urllib2
from time import strftime
from urllib import URLopener
from urllib2 import urlopen
from xml.dom.minidom import parseString
# Defines source and destination of image
#rss_feed = 'http://feeds.feedburner.com/bingimages';
url = 'http://www.bing.com'
size = '1920x1080'
dst_dir = os.path.expanduser('~/Pictures/DeskFeed/')
# Originally found on: http://hints.macworld.com/article.php?story=20110721062846337
# Applescript modified for dual monitors by: ViVIDboarder
SCRIPT = """/usr/bin/osascript<<end
tell application "System Events"
set picture of every desktop to POSIX file "%s"
end tell
END"""
def set_desktop_background(destination):
subprocess.Popen(SCRIPT%destination, shell=True)
def parseFeed(url):
destination = "%s%s.jpg" % (dst_dir, strftime( "%y-%m-%d"
)
if os.path.exists(destination):
sys.exit(0)
req = urllib2.Request(url)
content = urllib2.urlopen(req).read()
link = content[content.index('g_img={url:') + len('g_img={url:') + 1 : len(content)]
link = link[0 : link.index(',id:') - 1]
links = link.split('_')
link = links[0] + '_' + links[1] + '_' + size + '.jpg'
print link
URLopener().retrieve(link, destination)
set_desktop_background(destination)
def main():
parseFeed(url)
if __name__ == "__main__":
main();
作者: 腻水染花腥 时间: 2013-9-25 16:45
引用:
原帖由 ffcactus 于 2013-9-25 06:33 发表 
关键是没几个人现在用 Mac 并装了python呀。
引用:
原帖由 ffcactus 于 2013-9-25 07:07 发表 
posted by wap, platform: iPhone
我又没有mac,我怎么知道带不带,你们全家吃了药还差不多,用苹果的神经病果然比较多。
张口就来赞
作者: 20060602 时间: 2013-9-25 16:50
没 LS 那两个优雅,但应该能用:复制内容到剪贴板
代码:
#!/usr/bin/env python
import os
import md5
import pprint
import sys
import subprocess
from time import strftime
from urllib import URLopener
from urllib2 import urlopen
from xml.dom.minidom import parseString
#get bing wallpaper
# Defines source and destination of image
rss_feed = 'http://cn.bing.com/';
dst_dir = os.path.expanduser('~/Pictures/DeskFeed/')
# Originally found on: http://hints.macworld.com/article.php?story=20110721062846337
# Applescript modified for dual monitors by: ViVIDboarder
SCRIPT = """/usr/bin/osascript<<end
tell application "System Events"
set picture of every desktop to POSIX file "%s"
end tell
END"""
def set_desktop_background(destination):
subprocess.Popen(SCRIPT%destination, shell=True)
startString ="g_img={url:'"
def parseFeed(rss):
destination = "%s%s.jpg" % (dst_dir, strftime( "%y-%m-%d"))
if os.path.exists(destination):
sys.exit(0)
try:
rss_contents = urlopen( rss )
except:
print "Failed to read rss feed %s" % rss
return
rss_src = rss_contents.read()
rss_contents.close()
print rss_src
spPos = rss_src.find(startString)
if spPos == -1:
return
startPos = spPos + len(startString)
endPos = rss_src.find("'",startPos)
if endPos == -1:
return
link = rss_src[startPos:endPos].replace('1366x768','1920x1080')
URLopener().retrieve(link, destination)
set_desktop_background(destination)
def main():
parseFeed(rss_feed)
if __name__ == "__main__":
main()
作者: EraserKing 时间: 2013-9-25 16:52
复制内容到剪贴板
代码:
import httplib2
import os.path
h = httplib2.Http('cache')
headers = {'Content-type': 'application/x-www-form-urlencoded'}
url = 'http://www.bing.com/'
response, content = h.request(url, 'GET')
htmlContent = content.decode('utf-8')
imageUrl = htmlContent[htmlContent.find(';g_img'):]
imageUrl = imageUrl[imageUrl.find("'") + 1:]
imageUrl = imageUrl[:imageUrl.find("'")]
imageUrl = imageUrl.replace('1366x768', '1920x1080')
print(imageUrl)
response, content = h.request(imageUrl, 'GET')
imageFile = open(os.path.basename(imageUrl), 'wb')
imageFile.write(content)
imageFile.close()
看了下源代码 好像就一处有 不是'background-image'那里 直接提出来算了
作者: cxj3000 时间: 2013-9-25 17:10
posted by wap, platform: GALAXY S IV
用bing desktop 的是不是该有优越感了?壁纸每日自动更新,还有好几张可以备选
作者: eva3d 时间: 2013-9-25 17:12
看16楼罢
[ 本帖最后由 eva3d 于 2013-9-25 17:15 编辑 ]
作者: eva3d 时间: 2013-9-25 17:13
好吧,全部写完了...... 这年头还是得靠自己啊... 有需要的拿去,明天继续研究如何让这个py自动定时运行
Mac下,在图片目录里面建立一个 DeskFeed 的目录,把代码保存为“随便什么名字.py” 然后终端里面运行 python 随便什么名字.py
理论上马上壁纸就会更换了复制内容到剪贴板
代码:
#!/usr/bin/env python
import os
import md5
import pprint
import sys
import subprocess
import urllib2
import re
from time import strftime
from urllib import URLopener
from urllib2 import urlopen
from xml.dom.minidom import parseString
dst_dir = os.path.expanduser('~/Pictures/DeskFeed/')
SCRIPT = """/usr/bin/osascript<<END
tell application "System Events"
set picture of every desktop to POSIX file "%s"
end tell
END"""
def set_desktop_background(destination):
subprocess.Popen(SCRIPT%destination, shell=True)
def main():
userMainUrl = "http://cn.bing.com";
req = urllib2.Request(userMainUrl);
resp = urllib2.urlopen(req);
respHtml = resp.read();
WallPaper = re.search("g_img={url:'(?P<wpurl>.+?)'", respHtml);
# print "WallPaper=",WallPaper;
if(WallPaper):
wpurl = WallPaper.group("wpurl");
# print "wpurl=",wpurl;
newurl = wpurl.replace('1366x768','1920x1080');
# print "newurl=",newurl;
destination = "%s%s.jpg" % (dst_dir, strftime( "%y-%m-%d"));
URLopener().retrieve(newurl, destination)
set_desktop_background(destination)
if __name__=="__main__":
main();
作者: beterhans 时间: 2013-9-25 17:22
posted by wap, platform: iPad
支持下中年人
作者: jojo99 时间: 2013-9-25 17:26
支持下,一直想着咋存每天喜欢的bing壁纸。
作者: Running82 时间: 2013-9-25 18:31
adsl-172-10-1-115:~ admin$ python bing.py
python: can't open file 'bing.py': [Errno 2] No such file or directory
adsl-172-10-1-115:~ admin$
我怎么不行呀。。。
作者: eva3d 时间: 2013-9-25 20:31
posted by wap, platform: Firefox
图片目录下面建立 DeskFeed目录了么
作者: gogoout 时间: 2013-9-25 21:48
不错,今日桌面已取到~来支持下
作者: bpsim 时间: 2013-9-25 22:26
posted by wap, platform: Android
弄个apk吧,用着的bing daily wallpaper应用好像不是full hd墙纸。
作者: ooo 时间: 2013-9-25 22:44
posted by wap, platform: iPhone
赞一个,谢楼主
作者: 萨兰丁 时间: 2013-9-25 23:13
posted by wap, platform: GOOGLE (Nexus 4)
WIN下直接搜Bingwallpaper有现成的软件可以每天换……
作者: liangbsb 时间: 2013-9-25 23:14
感谢分享,为了bing的壁纸我专门装了bing搜索
作者: limboking 时间: 2013-9-26 00:22
不错不错
虽然mac上基本很少看到桌面- -
作者: eva3d 时间: 2013-9-26 08:51
昨晚把开机运行的也搞定了..
在应用程序里打开Automator,建立应用程序
[attach]575073[/attach]
然后在左边选择实用工具,再选择运行Shell工具,其他设置如图
我这里的情况是假设16楼的script保存为bing.py,存放在 ~/Pictures/bing.py,注意路径必须是绝对路径,不能用~/,而必须用/Users/你的用户名/
[attach]575074[/attach]
Automator保存为一个应用文件,随便放哪里都可以,我是放应用程序里面了
在系统偏好设置(齿轮)里打开用户与群组,选择对应你的用户名,点击右边上方的登录项,把之前用Automator建立的应用程序添加到启动项目,下次开机就自动执行了,壁纸每日开机一换,今天开机直接就换成海云台夜景了
[attach]575075[/attach]
作者: Running82 时间: 2013-9-26 12:56
bing.py 放在哪里有要求吗?我怎么还是没法用?
作者: Running82 时间: 2013-9-26 12:58
我已经有DeskFeed了啊。。。
作者: Running82 时间: 2013-9-26 13:00
adsl-172-10-1-115:~ admin$ python /users/admin/pictures/bing.py
File "/users/admin/pictures/bing.py", line 1
SyntaxError: Non-ASCII character '\xff' in file /users/admin/pictures/bing.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
adsl-172-10-1-115:~ admin$
我打路径了也不行。。。
作者: moody 时间: 2013-9-26 13:30
可以了,多谢
[ 本帖最后由 moody 于 2013-9-26 13:32 编辑 ]
作者: Running82 时间: 2013-9-26 13:35
posted by wap, platform: iPhone
楼上怎么用的呀?
作者: eva3d 时间: 2013-9-26 13:38
posted by wap, platform: XBOX
runnning你是不是复制的代码有误啊 第一行就出错了... 难道你是粘贴到了mac的文本编辑里面去了? 如果是这样,请在保存前按 SHIFT+COMMAND+T,变成纯文本再保存,否则直接保存是RTF格式的富文本。
作者: Running82 时间: 2013-9-26 13:44
posted by wap, platform: iPhone
我等下试试。
能否直接把那个py的文件上传一下啊?谢谢。
作者: eva3d 时间: 2013-9-26 13:57
posted by wap, platform: XBOX
收附件
作者: chucky 时间: 2013-9-26 14:17
引用:
原帖由 ffcactus 于 2013-9-25 14:33 发表 
关键是没几个人现在用 Mac 并装了python呀。
MAC下python不是默认安装的吗?
作者: 去日留痕 时间: 2013-9-26 15:08
posted by wap, platform: iPhone
谢谢楼主分享
作者: Running82 时间: 2013-9-26 16:37
喷了 我还是不行。。。
RunningdeMacBook-Air:~ admin$ python bing.pty
python: can't open file 'bing.pty': [Errno 2] No such file or directory
RunningdeMacBook-Air:~ admin$
是我命令行不对嘛?
作者: Running82 时间: 2013-9-26 16:38
RunningdeMacBook-Air:~ admin$ python bing.py
python: can't open file 'bing.py': [Errno 2] No such file or directory
RunningdeMacBook-Air:~ admin$
刚刚多打了个 T,现在我正确输入了 还是不行啊。。。
作者: finalx 时间: 2013-9-26 16:43
引用:
原帖由 Running82 于 2013-9-26 16:38 发表 
RunningdeMacBook-Air:~ admin$ python bing.py
python: can't open file 'bing.py': [Errno 2] No such file or directory
RunningdeMacBook-Air:~ admin$
刚刚多打了个 T,现在我正确输入了 还是不行啊。。。 ...
找不到这个文件,你把bing.py放到那个目录里了? python $HOME/your_dir/bing.py 把your_dir换成bing.py所在的目录
作者: Running82 时间: 2013-9-26 16:48
我就放在图片的目录下啊。。。应该放在哪里?
作者: finalx 时间: 2013-9-26 17:06
引用:
原帖由 Running82 于 2013-9-26 16:48 发表 
我就放在图片的目录下啊。。。应该放在哪里?
或者你看一下文件的属性,里面有个绝对路径。然后复制一下这个绝对路径就行了。不用再加$HOME了。
作者: Running82 时间: 2013-9-26 19:05
#!/usr/bin/env python
import os
import md5
import pprint
import sys
import subprocess
import urllib2
import re
from time import strftime
from urllib import URLopener
from urllib2 import urlopen
from xml.dom.minidom import parseString
dst_dir = os.path.expanduser('~/Pictures/DeskFeed/')
SCRIPT = """/usr/bin/osascript<<END
tell application "System Events"
set picture of every desktop to POSIX file "%s"
end tell
END"""
def set_desktop_background(destination):
subprocess.Popen(SCRIPT%destination, shell=True)
def main():
userMainUrl = "http://cn.bing.com";
req = urllib2.Request(userMainUrl);
resp = urllib2.urlopen(req);
respHtml = resp.read();
WallPaper = re.search("g_img={url
?P<wpurl>.+?)'", respHtml);
# print "WallPaper=",WallPaper;
if(WallPaper):
wpurl = WallPaper.group("wpurl"
;
# print "wpurl=",wpurl;
newurl = wpurl.replace('1366x768','1920x1080');
# print "newurl=",newurl;
destination = "%s%s.jpg" % (dst_dir, strftime( "%y-%m-%d"
);
URLopener().retrieve(newurl, destination)
set_desktop_background(destination)
if __name__=="__main__":
main();
我没有找到哪里可以打路径的地方啊。。。
作者: ninggu2008 时间: 2013-9-26 19:17
引用:
原帖由 Running82 于 2013-9-26 19:05 发表 
#!/usr/bin/env python
import os
import md5
import pprint
import sys
import subprocess
import urllib2
import re
from time import strftime
from urllib import URLopener
from urllib2 import urlo ...
无语,你没用过Linux?
$ python /users/xxx/pictures/bing.py
xxx替换成你的用户名试试
作者: Running82 时间: 2013-9-26 21:09
成功了 感谢ls,lz 还有大家
作者: babyking 时间: 2013-9-26 22:43
posted by wap, platform: iPad
高Duan
欢迎光临 TGFC Lifestyle (http://bbs.tgfcer.com/) |
Powered by Discuz! 6.0.0 |