使用 Python 製作酷炫多彩的 Jenkins 插件詞雲圖

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Jenkins 插件名稱高頻關鍵詞有哪些?一切盡在酷炫多彩的詞雲圖"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作爲最流行的 CI/CD 工具,Jenkins 的優勢之一是其生態強大,而這與其插件體系分不開的。 目前 Jenkins 插件 1500+ (截止2020年06月17日,插件數量爲1749)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"近日發現詞雲比較好玩,於是想着以 Jenkins 插件名稱爲數據源,形成的詞雲會是什麼樣的呢,什麼關鍵字會比較突出呢? 想到就去做,帶着問題,帶着好奇心,開始了實踐之旅~"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"插件基本字段說明"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以 Jenkins 中文本地化插件爲例,在 Jenkins 官網插件詳情頁面可以看出: 其 ID 爲 localization-zh-cn,Name 爲 Localization: Chinese (Simplified)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1f/1f4bcd81d78de816ea0024a92bb6573c.png","alt":"localization-zh-cn","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"獲取所有 Jenkins 插件的名稱"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如何獲取所有 Jenkins 插件的名稱呢?這裏我想到3種方式,或許還有更多方式:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://plugins.jenkins.io/","title":null},"content":[{"type":"text","text":"插件官網"}]},{"type":"text","text":"爬蟲抓取"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://github.com/jenkins-infra/repository-permissions-updater","title":null},"content":[{"type":"text","text":"插件權限文件"}]},{"type":"text","text":"獲取"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http://updates.jenkins-ci.org/update-center.json","title":null},"content":[{"type":"text","text":"插件更新中心配置文件"}]},{"type":"text","text":"獲取"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對比上面的三種方式,插件權限文件中並沒有 Name 字段,插件更新中心配置文件相對從插件官網抓取比較簡單。 所以計劃從 update-center.json 進行解析,其中插件名稱在 json 中對應字段爲 title。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3e/3e4aabcb4b9cedd8a4cbd039bffc5d6b.png","alt":"update-center-json","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"生成 Jenkins 插件名稱文件"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讀取 update-center.json 中 plugin 的 title 字段,按行寫入到 jenkins-plugins.txt 文件,代碼如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# -*- coding: UTF-8 -*-\nimport json\n\n\nif __name__ == \"__main__\":\n json_obj = json.load(open(\"update-center.json\", \"r\"))\n plugins_obj = json_obj[\"plugins\"]\n with open(\"jenkins-plugins.txt\", \"w\") as fw:\n for plugin_name in plugins_obj:\n plugin_obj = plugins_obj[plugin_name]\n print plugin_obj[\"title\"]\n fw.write(plugin_obj[\"title\"].encode('utf-8') + \"\\n\")\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"jenkins-plugins.txt 文件共有 1749 行(與 Jenkins 1749個插件對應),其內容截圖如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2a/2a15e4e98c8f79753ea6588df23d6e97.png","alt":"jenkins-plugins-txt","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"生成詞雲圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏使用 Python 代碼生成詞雲圖,詞的來源爲 jenkins-plugins.txt,代碼如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# -*- coding: UTF-8 -*-\n\nfrom wordcloud import WordCloud\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom PIL import Image\n\n\ndef generate_word_cloud_image(background_image):\n # mask\n mask = np.array(Image.open(background_image))\n\n # generate word cloud\n wc = WordCloud(mask=mask, scale=1.5, mode='RGBA', background_color=\"white\", max_words=2000).generate(text=text)\n\n # show word cloud\n plt.imshow(wc, interpolation='bilinear')\n plt.axis('off')\n plt.show()\n\n # save to file\n background_image = str(background_image).split(\"/\")[-1].replace(\".png\", \"\")\n wc.to_file(\"word-cloud-img/\" + background_image + '-word-cloud.png')\n\n\nif __name__ == \"__main__\":\n with open('jenkins-plugins.txt') as f:\n text = f.read()\n\n for image_name in [\"background-img/kongfu.png\", \"background-img/jenkins-logo.png\"]:\n generate_word_cloud_image(background_image=image_name)\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"生成的詞雲圖各個關鍵字以不同大小和比例,繪製出一幅多彩的畫卷,感覺很美觀、很酷炫!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以 Jenkins logo 爲背景圖片,生成的詞雲圖如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/bc/bc1bdbf471c2059a96efb9a7f6afa130.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以 Jenkins 中文社區的 kongfu 爲背景圖片,生成的詞雲圖如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d1/d1c2785f94e4f72e38acdf5e3805c6fc.png","alt":"kongfu-word-cloud","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從詞雲圖中可以看出,Pipeline 詞頻最高,Build、API、Job 次之,緊接着還有 Publisher、Notifier、Trigger、Step、GitHub 等等。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"資源說明"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所有代碼及文件可以在 GitHub 倉庫找到,如果你也覺得好玩有趣,順便點個 star 吧,謝謝~"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://github.com/donhui/jenkins-plugins-word-cloud","title":null},"content":[{"type":"text","text":"https://github.com/donhui/jenkins-plugins-word-cloud"}]}]}]}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/62/62744ccd1ba761f7b41ea2818304bdc1.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章