Deepin简单的桌面天气实现脚本(自动更换带天气信息的壁纸)
Tofloor
poster avatar
adinlead
deepin
2017-04-20 03:31
Author
本帖最后由 adinlead 于 2017-4-21 13:43 编辑


github地址:
https://github.com/adinlead/DeepinAutoUpdate
新的脚本已经上传到Github,请到Github克隆或者下载

更新了配置文件[Setting.py]:
  1. # -*-coding:utf-8-*-
  2. # 城市代码
  3. city = '101120101'
  4. # 是否下载网络壁纸
  5. downwall=True
  6. # 本地壁纸路径
  7. wallpath = '/home/adinlead/Pictures/壁纸/2.jpg'
Copy the Code


运行主脚本:autoUpdateWallpaperMain.sh

新的项目使用了新的定时任务,运行一次即可:

  1. bash autoUpdateWallpaperMain.sh
  2. 后台运行:
  3. bash autoUpdateWallpaperMain.sh &
  4. 或者:
  5. ./autoUpdateWallpaperMain.sh &
Copy the Code


效果:


[page]
以下为旧的脚本内容,Github已经更新到最新

文件结构:
DeepinAutoUpdate/
└── src
    ├── Setting.py
    ├── font.ttf
    ├── ImageTools.py
    └── Main.py

资源文件:
随意一个你喜欢的,而且可以显示'℃'的TTF字体文件

代码文件:Setting.py
  1. # -*-coding:utf-8-*-
  2. city = '101120101'
  3. wallpath = '/home/adinlead/Pictures/壁纸/1.jpg'
Copy the Code


ImageTools.py
  1. # -*-coding:utf-8-*-
  2. import os
  3. import shutil
  4. from PIL import Image, ImageDraw, ImageFont


  5. def getPollutionLevel(lev):
  6.     lev = int(lev)

  7.     if lev < 51:
  8.         return '%s 优' % lev
  9.     elif lev < 101:
  10.         return '%s 良' % lev
  11.     elif lev < 151:
  12.         return '%s 轻度污染' % lev
  13.     elif lev < 201:
  14.         return '%s 中度污染' % lev
  15.     elif lev < 301:
  16.         return '%s 重度污染' % lev
  17.     else:
  18.         return '%s 严重污染' % lev


  19. def toWatermarkImage(result):
  20.     # Configurations:
  21.     textcolor = "#000000"

  22.     # Build rich text for ads
  23.     ad = []

  24.     # Wrap line for text
  25.     #   Special treated Chinese characters
  26.     #   Workaround By Felix Yan - 20110508
  27.     wraptext = [""]

  28.     result['WRDJ'] = getPollutionLevel(result['aqi'])
  29.     result['wse'] = result['wse'].replace('<', '')

  30.     # Format wrapped lines to rich text
  31.     wrap = [(text, textcolor) for text in wraptext]
  32.     wrap += ad

  33.     # Draw picture
  34.     i = Image.new("RGBA", (290, 150), (255, 255, 255, 120))
  35.     d = ImageDraw.Draw(i)

  36.     # 输出城市信息
  37.     d.text((20, 0), result['cityname'].decode('UTF8'), font=ImageFont.truetype("font.ttf", 72), fill=textcolor)

  38.     d.text((len(result['cityname']) * 72 + 26, 72 - 48), result['temp'].decode('UTF8'),
  39.            font=ImageFont.truetype("font.ttf", 48), fill=textcolor)
  40.     d.text((len(result['cityname']) * 72 + len(result['temp']) * 48 - 12, 72 - 32), "℃".decode("UTF8"),
  41.            font=ImageFont.truetype("font.ttf", 32), fill=textcolor)

  42.     d.line(((20, 85), (270, 85)), (0, 0, 0), width=2)

  43.     msg = "%(weather)s | %(WD)s %(WS)s %(wse)s" % result
  44.     d.text((20, 90), msg.decode('UTF8'), font=ImageFont.truetype("font.ttf", 20), fill=textcolor)

  45.     msg = "湿度:%(SD)s | %(WRDJ)s" % result

  46.     d.text((20, 115), msg.decode('UTF8'), font=ImageFont.truetype("font.ttf", 20), fill=textcolor)

  47.     # Write result to a temp file
  48.     filename = "/tmp/auw/watermark.png"
  49.     with open(filename, "wb") as s:
  50.         i.save(s, "PNG")
  51.     return filename


  52. def brand(wallpath, watermark):
  53.     new_wallpath = '/tmp/auw/wallpaper%s' % wallpath[wallpath.rfind('.'):]
  54.     shutil.copy(wallpath, new_wallpath)

  55.     im = Image.open(new_wallpath)
  56.     mark = Image.open(watermark)
  57.     layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
  58.     layer.paste(mark, (im.size[0] - mark.size[0], mark.size[1]))
  59.     out = Image.composite(layer, im, layer)
  60.     out.save(new_wallpath)
  61.     return new_wallpath
Copy the Code


Main.py
  1. # -*-coding:utf-8-*-
  2. import commands
  3. import json
  4. import os

  5. import time
  6. import urllib2
  7. import ImageTools

  8. import sys
  9. import Setting

  10. reload(sys)
  11. sys.setdefaultencoding('utf8')

  12. if not os.path.isdir('/tmp/auw/'):
  13.     os.mkdir('/tmp/auw/')


  14. def main():
  15.     # 读取配置文件的信息

  16.     citycode = Setting.city
  17.     if len(sys.argv) == 1 or len(sys.argv[1]) == 0:
  18.         wallpath = Setting.wallpath
  19.     else:
  20.         wallpath = sys.argv[1]
  21.         config = "# -*-coding:utf-8-*-\ncity = '%s'\nwallpath = '%s'" % (citycode,wallpath)
  22.         config_file = open('Setting.py', 'w')
  23.         config_file.write(config)
  24.         config_file.close()

  25.     # 设置请求地址与请求参数
  26.     request_url = 'http://d1.weather.com.cn/sk_2d/%s.html?_=%s' % (citycode, int(round(time.time() * 1000)))

  27.     request = urllib2.Request(request_url)
  28.     request.add_header('referer', 'http://www.weather.com.cn/weather1d/101120101.shtml')
  29.     response = urllib2.urlopen(request)

  30.     # 读取请求结果并转换为JSON
  31.     result = response.read()
  32.     result = result.replace('var dataSK = ', '')
  33.     result = json.loads(result)

  34.     watermark = ImageTools.toWatermarkImage(result)

  35.     new_wallpath = ImageTools.brand(wallpath, watermark)

  36.     commands.getstatusoutput("gsettings set org.gnome.desktop.background picture-uri %s" % new_wallpath)


  37. if __name__ == __name__:
  38.     main()
Copy the Code














Reply Favorite View the author
All Replies
avatar
xnwm
deepin
2017-04-21 04:27
#1
很喜欢 。不知道能否来个安装教程? 我是小白 不是很懂安装步骤。。谢谢
Reply View the author
avatar
arlen
deepin
2017-04-21 07:05
#2
不废话了,膜拜大神,虽然本小白不会弄。
Reply View the author
avatar
adinlead
deepin
2017-04-21 21:34
#3
https://bbs.deepin.org/post/137891
很喜欢 。不知道能否来个安装教程? 我是小白 不是很懂安装步骤。。谢谢

github上已经写明了简单教程
望喜欢
Reply View the author
avatar
xnwm
deepin
2017-04-21 22:39
#4
https://bbs.deepin.org/post/137891
github上已经写明了简单教程
望喜欢

好的  我去看看 开始我没看 呵呵
Reply View the author
avatar
xnwm
deepin
2017-04-23 22:34
#5
https://bbs.deepin.org/post/137891
github上已经写明了简单教程
望喜欢

昨晚我下载了 然后放到home / 9527 文件夹下面 然后运行 autoUpdateWallpaper.sh 提示 找不到 Main.sh 这个文件在src 目录下 这个是为何? 还是有什么路径没改?我看了几个.sh 里面都没什么别的路径要改的
Reply View the author
avatar
yueyekongwen
deepin
2017-04-25 00:39
#6
https://bbs.deepin.org/post/137891
昨晚我下载了 然后放到home / 9527 文件夹下面 然后运行 autoUpdateWallpaper.sh 提示 找不到 Main.sh 这 ...

路径定位到src吧
Reply View the author
avatar
xnwm
deepin
2017-04-25 01:10
#7

好吧  今晚我再试试。。
Reply View the author
avatar
adinlead
deepin
2017-05-02 22:25
#8
https://bbs.deepin.org/post/137891
好吧  今晚我再试试。。

autoUpdateWallpaperMain.sh中
有一个命令(第三行为备注,第四行为代码):
  1. # 先定位到运行目录(如果目录不同请修改此处)
  2. cd /home/adinlead/Project/PyProject/DeepinAutoUpdate/src/
Copy the Code

您需要将这个路径修改一下
Reply View the author
avatar
186******39
deepin
2017-09-22 00:09
#9
作者好久没有更新了,已经不能用。
我做了些更改,写这里吧,懒得git了

1. autoUpdateWallpaperMain.sh 中删除 echo time_text ${ls_date}
2. bing的地址都变了,DownBingWallpaper.py 中 url = 'https://bing.ioliu.cn/?p=%s' % page
3. ImageTools.py 中增加了自动缩放图片的功能
  1. def returnSize(im):
  2.     #返回图片大小,返回两个值,第一个返回值总为最大
  3.     max,min = im.size
  4.     if max > min:
  5.         return max,min
  6.     else:
  7.         return min,max

  8. def changeSize(wallpath):
  9.     img = Image.open(wallpath)
  10.     max,min = returnSize(img)
  11.     value = max/1680
  12.     min = min/value
  13.     newimg = img.resize((1680,min),Image.ANTIALIAS)
  14.     newimg.save(wallpath)
Copy the Code
在Main.py里DownBingWallpaper.downNow(wallpath)后面加上ImageTools.changeSize(wallpath)实现缩放

4. Main.py里最后一行改成
  1. commands.getstatusoutput('qdbus --literal com.deepin.wm /com/deepin/wm com.deepin.wm.SetTransientBackground "file://%s"' % new_wallpath)
Copy the Code


Reply View the author
avatar
sunhanwu
deepin
2017-10-04 21:31
#10
有没有遇到IOError: cannot identify image file '/tmp/auw/wallpaper.jpg'
Reply View the author
avatar
rekols
deepin
2017-10-04 21:53
#11
蛤?还能翻页   )
Reply View the author
avatar
186******39
deepin
2018-11-08 23:12
#12
本帖最后由 l0rraine 于 2018-11-8 15:17 编辑

DeepinAutoUpdate.tar
额,辛苦了,兄弟,我把我的文件共享下,你可以参考参考
Reply View the author
avatar
陈思煜
deepin
2018-11-09 01:44
#13
https://bbs.deepin.org/post/137891
Deepin更换壁纸命令

gsettings set com.deepin.wrap.gnome.desktop.background picture-uri ~/Pictures/Wa ...

谢谢 终于找到更新壁纸的命令了
Reply View the author