每天自动爬取momentum壁纸并保存

来源:互联网 发布:单片机 以太网MAC 编辑:程序博客网 时间:2024/06/07 02:56
#!/usr/bin/pythonimport osimport jsonimport urllib2import urllibimport timedef auto_down(url, filename):    try:        urllib.urlretrieve(url, filename)    except urllib.ContentTooShortError:        print 'Network conditions is not good.Reloading.'        auto_down(url, filename)directory = os.path.dirname(os.path.realpath(__file__)) + '/pictures/'print directorytoday = time.strftime("%Y-%m-%d")req = urllib2.Request('https://api.momentumdash.com/feed/bulk?syncTypes=backgrounds&localDate=' + today)req.add_header('Host', 'api.momentumdash.com')req.add_header('Accept', '*/*')req.add_header('X-Momentum-ClientId', '237eeecc-e1eb-490f-9a4c-94eafffeca1e')req.add_header('X-Momentum-Version', '0.91.1')req.add_header('Content-Type', 'application/json')response = urllib2.urlopen(req)data = json.loads(response.read())if not os.path.exists(directory):    os.makedirs(directory)for bg in data['backgrounds']:    # name = bg['filename'].rsplit('/', 1)[-1]    path = directory + today + ".png"    # print name    # no file type data, so assume .png    if not os.path.exists(path):        auto_down(bg['filename'], path)