使用Python获取Minecraft的最新资讯

如果想要了解Minecraft最新的版本信息,又不想到网页上翻找,怎么办呢?其实,我们可以使用Python来获取最新的版本信息和新闻。

先说版本信息,Java版的版本更新情况可以在官方提供的Json文件中看到,地址为http://launchermeta.mojang.com/mc/game/version_manifest.json。我们可以使用Python的Json解析库对其进行解析,然后打印在控制台上。代码如下:

from urllib.request import urlopen
import json,time,datetime

def getTime(timestr):
    return time.strftime("%b/%d/%Y,%H:%M:%S(%a)", time.strptime(timestr[0:-7],'%Y-%m-%dT%H:%M:%S'))

json_url = 'http://launchermeta.mojang.com/mc/game/version_manifest.json'
 
response = urlopen(json_url)

#req is a string
req = response.read()

'''
with open('version_manifest.json','wb') as f:
    f.write(req)
'''

versions = json.loads(req)
print('Welcome to the Minecraft Version Checker!\nThis week is %s%s%s\n' %(datetime.datetime.now().isocalendar()[0]-2000,'w',str(datetime.datetime.now().isocalendar()[1]).zfill(2)))
print('Latest Release:  ',versions['latest']['release'])
print('Latest Snapshot: ',versions['latest']['snapshot'],'\n\nRecent Versions:')

print('%-21s%-14s%-30s' %('Version Id:','Type:','Release Time(GMT):'))

i = 0
for ver in versions['versions']:
    print('%-21s%-14s%-30s' %(ver['id'],ver['type'],getTime(ver['releaseTime'])))
    i += 1
    if i == 10:
        break
print('\n')

如果我们还想要一些更详细的新闻,怎么办呢。一个解决方法是请求其他网站的RSS,比如Minecraft Global和9Minecraft,代码如下:

import feedparser,time

src = ['https://www.minecraftglobal.com/feed/','http://www.9minecraft.net/feed/','https://wikiminecraft.com/feed/']

for url in src:
    feed = feedparser.parse(url)
    print("News Source:",feed.channel.title)
    print("There are",len(feed.entries),"pieces of news from this source.\n")
    for e in feed.entries:
        print('%-70s%s     By %s' %('[ '+e.title+' ]',time.strftime("%b/%d/%Y,%H:%M:%S(%a)", e.published_parsed),e.author))
        print('>>',e.description[3:200],'...[',e.link[0:100],']')
    print('\n\n')

其中的src就是加载源,可以按需修改。这样一来,就可以方便地获取Minecraft的最新资讯啦!

posted @ 2020-09-20 00:30  DevBobcorn  阅读(238)  评论(0编辑  收藏  举报