Skip to content

Instantly share code, notes, and snippets.

@mpco
Created September 10, 2017 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpco/dbe1fbd094b03c4ad5cbfdf0fecdadff to your computer and use it in GitHub Desktop.
Save mpco/dbe1fbd094b03c4ad5cbfdf0fecdadff to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import urllib.request
import json
import time
import sys
import os
# https://m.okjike.com/topics/5575a562e4b06db055be5425?username=DB5A4902-B148-448A-8F05-5A94AC27D171
url = sys.argv[1]
# https://m.okjike.com/topics/5575a562e4b06db055be5425
topicUrl = url.split("?")[0]
# 5575a562e4b06db055be5425
topicID = topicUrl.split("/")[-1]
apiUrlBase = "https://app.jike.ruguoapp.com/1.0/messages/showDetail?topicId="
r = urllib.request.urlopen(apiUrlBase + topicID)
topicObj = json.loads(r.read())
topicTitle = topicObj["topic"]["content"]
topicInfo = topicObj["topic"]["briefIntro"]
# print("Title: " + topicTitle)
# print("Info : " + topicInfo)
# 最新一条
latestMsg = topicObj["messages"][0]
msgTitle = latestMsg["content"]
msgTime = latestMsg["createdAt"]
msgUrl = latestMsg["originalLinkUrl"] if latestMsg["sourceRawValue"] != "none" else "none"
# 保存的文件夹
dirName = os.path.join(os.path.dirname(os.path.realpath(__file__)), time.strftime("%Y%m%d"))
os.mkdir(dirName)
# sourceRawValue 有三种:"link", "none", "video"
# 视频类消息没有图片,所以需要判断
if latestMsg["sourceRawValue"] in ["link", "none"]:
msgPicObj = latestMsg["pictureUrls"]
msgPicUrlList = [[item["picUrl"], item["format"]] for item in msgPicObj]
for index, item in enumerate(msgPicUrlList):
itemUrl = item[0]
itemFormat = item[1]
savePath = os.path.join(dirName, str(index) + "." + itemFormat)
_, headers = urllib.request.urlretrieve(itemUrl, savePath)
# 消息信息保存到txt文件
infoFilePath = os.path.join(dirName, "info_" + latestMsg["sourceRawValue"] + ".txt")
with open(infoFilePath, 'w') as f:
f.write("Title: " + msgTitle + "\n")
f.write("Time: " + msgTime + "\n")
f.write("Link: " + msgUrl + "\n")
print("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment