2022-02-20 12:53:13 +00:00
|
|
|
import configparser
|
2022-02-06 15:28:12 +00:00
|
|
|
import os
|
2022-02-20 12:53:13 +00:00
|
|
|
import re
|
|
|
|
import sys
|
2022-02-06 15:28:12 +00:00
|
|
|
from datetime import *
|
2022-02-20 13:01:06 +00:00
|
|
|
# import dateutil.relativedelta
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
import emoji as emojilib
|
|
|
|
|
2022-02-06 15:28:12 +00:00
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
def check_str_to_bool(input_text) -> bool:
|
|
|
|
if input_text == "True" or input_text == "true" or input_text == "TRUE":
|
2022-02-06 15:28:12 +00:00
|
|
|
return True
|
2022-02-20 12:53:13 +00:00
|
|
|
elif input_text == "False" or input_text == "false" or input_text == "FALSE":
|
2022-02-06 15:28:12 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
|
2022-02-06 15:28:12 +00:00
|
|
|
noteList = []
|
|
|
|
reactionList = []
|
2022-02-20 12:53:13 +00:00
|
|
|
reactList = []
|
2022-02-06 15:28:12 +00:00
|
|
|
emojiList = []
|
|
|
|
emojisTotal = 0
|
|
|
|
doubleList = []
|
|
|
|
text = ""
|
|
|
|
getReactions = False
|
|
|
|
|
|
|
|
configfilePath = os.path.join(os.path.dirname(__file__), 'miceco.cfg')
|
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
if not os.path.exists(configfilePath):
|
2022-02-06 15:28:12 +00:00
|
|
|
print("No config File found!")
|
2022-02-20 12:53:13 +00:00
|
|
|
print("Exit program!")
|
2022-02-06 15:28:12 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
# Load configuration
|
2022-02-06 15:28:12 +00:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(configfilePath)
|
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
url = "https://" + config.get("misskey", "instance") + "/api"
|
|
|
|
token = config.get("misskey", "token")
|
|
|
|
user = config.get("misskey", "user")
|
2022-02-06 15:28:12 +00:00
|
|
|
|
|
|
|
try:
|
2022-02-20 12:53:13 +00:00
|
|
|
getReactions = check_str_to_bool(config.get("misskey", "getReaction"))
|
2022-02-06 15:28:12 +00:00
|
|
|
except (TypeError, ValueError) as err:
|
|
|
|
getReactions = False
|
|
|
|
|
|
|
|
try:
|
2022-02-20 12:53:13 +00:00
|
|
|
req = requests.post(url + "/users/show", json={"username": user, "host": None, "i": token})
|
|
|
|
req.raise_for_status()
|
2022-02-06 15:28:12 +00:00
|
|
|
except requests.exceptions.HTTPError as err:
|
2022-02-20 12:53:13 +00:00
|
|
|
print("Couldn't get Username!\n" + str(err))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2022-02-06 15:28:12 +00:00
|
|
|
userid = req.json()["id"]
|
|
|
|
nickname = req.json()["name"]
|
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
today = date.today()
|
|
|
|
formerDate = today - timedelta(days=1)
|
|
|
|
# formerDate = today - timedelta(weeks=1) #Last week
|
|
|
|
# formerDate = today - dateutil.relativedelta.relativedelta(months=1)
|
|
|
|
formerDateMidnight = datetime.combine(formerDate, time(0, 0, 0))
|
|
|
|
todayMidnight = datetime.combine(today, time(0, 0, 0))
|
2022-02-06 15:28:12 +00:00
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
seit = int(formerDateMidnight.timestamp()) * 1000 # Javascript uses millisecond timestamp and Python uses float
|
|
|
|
bis = int(todayMidnight.timestamp()) * 1000
|
2022-02-06 15:28:12 +00:00
|
|
|
|
|
|
|
lastTimestamp = bis
|
2022-02-20 12:53:13 +00:00
|
|
|
formerTimestamp = 0
|
2022-02-06 15:28:12 +00:00
|
|
|
|
|
|
|
while True:
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
if (bis != lastTimestamp) and (formerTimestamp == lastTimestamp):
|
2022-02-06 15:28:12 +00:00
|
|
|
break
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
req = requests.post(url + "/users/notes", json={
|
|
|
|
"i": token,
|
|
|
|
"userId": userid,
|
|
|
|
"sinceDate": seit,
|
|
|
|
"untilDate": lastTimestamp,
|
|
|
|
"includeReplies": True,
|
|
|
|
"limit": 100,
|
|
|
|
"includeMyRenotes": False,
|
|
|
|
"withFiles": False,
|
|
|
|
"excludeNsfw": False
|
|
|
|
})
|
2022-02-06 15:28:12 +00:00
|
|
|
req.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as err:
|
2022-02-20 12:53:13 +00:00
|
|
|
print("Couldn't get Posts! " + str(err))
|
2022-02-06 15:28:12 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
for jsonObj in req.json():
|
2022-02-20 12:53:13 +00:00
|
|
|
# textDict = jsonObj
|
2022-02-06 15:28:12 +00:00
|
|
|
noteList.append(jsonObj)
|
2022-02-20 12:53:13 +00:00
|
|
|
|
2022-02-06 15:28:12 +00:00
|
|
|
formerTimestamp = lastTimestamp
|
2022-02-20 12:53:13 +00:00
|
|
|
|
2022-02-26 06:43:48 +00:00
|
|
|
if not len(noteList) <= 1: # If there is one or less notes, then break the while loop
|
|
|
|
lastTime = noteList[len(noteList) - 1]["createdAt"]
|
|
|
|
lastTimestamp = int(datetime.timestamp(datetime.strptime(lastTime, '%Y-%m-%dT%H:%M:%S.%f%z')) * 1000)
|
|
|
|
else:
|
|
|
|
break
|
2022-02-06 15:28:12 +00:00
|
|
|
|
|
|
|
for element in noteList:
|
2022-02-20 12:53:13 +00:00
|
|
|
if element["text"] is None: # Skip Notes without text
|
2022-02-06 15:28:12 +00:00
|
|
|
print("Skip Note " + element["id"] + " without Text\nTime noted: " + element["createdAt"])
|
|
|
|
continue
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
if element["text"].find(chr(8203) + chr(8203) + chr(8203)) > 0: # Skip notes with three Zero-Width-Space in a
|
|
|
|
# row (Marker to skip older MiCECo notes)
|
2022-02-06 15:28:12 +00:00
|
|
|
print("Skip Note " + element["id"] + " with Zero-Width-Space\nTime noted: " + element["createdAt"])
|
|
|
|
continue
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
# Process and count custom Emojis
|
2022-02-06 15:28:12 +00:00
|
|
|
emojis = element["emojis"]
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
if emojis is not None:
|
|
|
|
for emoji in emojis:
|
2022-02-26 06:43:48 +00:00
|
|
|
if emoji["name"].find(
|
|
|
|
"@") == -1: # Only emojis from the own instance, because reactions will be in "emojis"
|
2022-02-20 12:53:13 +00:00
|
|
|
# too
|
|
|
|
if not emoji["name"] in doubleList:
|
|
|
|
doubleList.append(emoji["name"]) # Easy way to prevent a double emoji in the list.
|
|
|
|
emojiDict = {"emoji": ":" + emoji["name"] + ":", "count": 0}
|
|
|
|
emojiList.append(emojiDict)
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
index = doubleList.index(emoji["name"])
|
|
|
|
|
|
|
|
emojiList[index]["count"] += element["text"].count(emojiList[index]["emoji"])
|
|
|
|
|
|
|
|
if element["cw"] is not None:
|
|
|
|
emojiList[index]["count"] += element["cw"].count(emojiList[index]["emoji"]) # Count those Emojis, that
|
|
|
|
# are in this note CW text
|
2022-02-26 06:43:48 +00:00
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
# Process UTF8 Emojis
|
|
|
|
if element["cw"] is not None:
|
|
|
|
UTF8text = element["text"] + " " + element["cw"]
|
|
|
|
else:
|
|
|
|
UTF8text = element["text"]
|
|
|
|
UTF8List = re.findall(emojilib.get_emoji_regexp(), UTF8text) # Find all UTF8 Emojis in Text and CW text
|
|
|
|
|
|
|
|
if len(UTF8List) > 0:
|
|
|
|
UTF8List = list(set(UTF8List))
|
|
|
|
for emoji in UTF8List:
|
|
|
|
if emoji not in doubleList:
|
|
|
|
doubleList.append(emoji) # Easy way to prevent a double emoji in the list.
|
|
|
|
emojiDict = {"emoji": emoji, "count": 0}
|
|
|
|
emojiList.append(emojiDict)
|
|
|
|
|
|
|
|
index = doubleList.index(emoji)
|
|
|
|
emojiList[index]["count"] += UTF8text.count(emoji)
|
2022-02-06 15:28:12 +00:00
|
|
|
|
|
|
|
doubleList = []
|
2022-02-20 12:53:13 +00:00
|
|
|
emojiList = sorted(emojiList, reverse=True, key=lambda d: d["count"]) # Sort it by the most used Emojis!
|
|
|
|
|
|
|
|
reactionCount = 0
|
|
|
|
|
2022-02-06 15:28:12 +00:00
|
|
|
if getReactions:
|
|
|
|
lastTimestamp = bis
|
2022-02-20 12:53:13 +00:00
|
|
|
|
2022-02-06 15:28:12 +00:00
|
|
|
while True:
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
if (bis != lastTimestamp) and (formerTimestamp == lastTimestamp):
|
2022-02-06 15:28:12 +00:00
|
|
|
break
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
req = requests.post(url + "/users/reactions", json={
|
|
|
|
"i": token,
|
|
|
|
"userId": userid,
|
|
|
|
"sinceDate": seit,
|
|
|
|
"untilDate": lastTimestamp
|
|
|
|
})
|
2022-02-06 15:28:12 +00:00
|
|
|
req.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as err:
|
2022-02-20 12:53:13 +00:00
|
|
|
print("Couldn't get Posts! " + str(err))
|
2022-02-06 15:28:12 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
for jsonObj in req.json():
|
2022-02-20 12:53:13 +00:00
|
|
|
# textDict = jsonObj
|
2022-02-06 15:28:12 +00:00
|
|
|
reactionList.append(jsonObj)
|
2022-02-20 12:53:13 +00:00
|
|
|
|
2022-02-06 15:28:12 +00:00
|
|
|
formerTimestamp = lastTimestamp
|
2022-02-26 06:43:48 +00:00
|
|
|
if not len(reactionList) <=1:
|
|
|
|
lastTime = reactionList[len(reactionList) - 1]["createdAt"]
|
|
|
|
lastTimestamp = int(datetime.timestamp(datetime.strptime(lastTime, '%Y-%m-%dT%H:%M:%S.%f%z')) * 1000)
|
|
|
|
else:
|
|
|
|
break
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
react = ""
|
|
|
|
index = None
|
|
|
|
reactionElement: dict
|
|
|
|
|
|
|
|
for reactionElement in reactionList:
|
|
|
|
react = reactionElement["type"]
|
|
|
|
react = react.replace("@.", "")
|
|
|
|
|
|
|
|
if react not in doubleList:
|
2022-02-06 15:28:12 +00:00
|
|
|
doubleList.append(react)
|
2022-02-20 12:53:13 +00:00
|
|
|
emojiDict = {"reaction": react, "count": 0}
|
|
|
|
reactList.append(emojiDict)
|
|
|
|
|
|
|
|
index = doubleList.index(react)
|
|
|
|
|
|
|
|
reactList[index]["count"] += 1
|
|
|
|
|
|
|
|
doubleList = []
|
|
|
|
reactList = sorted(reactList, reverse=True, key=lambda d: d["count"])
|
|
|
|
|
|
|
|
if len(reactList) > 0:
|
|
|
|
for react in reactList: # Summarize the number of Reactions used
|
2022-02-06 15:28:12 +00:00
|
|
|
reactionCount += react["count"]
|
2022-02-20 12:53:13 +00:00
|
|
|
|
|
|
|
reactText = "\n\n\nAnd used " + str(reactionCount) + " reactions:\n\n" + chr(9553) + " "
|
|
|
|
|
|
|
|
for reactionElement in reactList:
|
|
|
|
reactText += str(reactionElement["count"]) + "x " + reactionElement["reaction"] + " " + chr(9553) + " "
|
2022-02-06 15:28:12 +00:00
|
|
|
else:
|
2022-02-20 12:53:13 +00:00
|
|
|
reactText = "\n\nAnd didn't use any reactions."
|
2022-02-06 15:28:12 +00:00
|
|
|
else:
|
2022-02-20 12:53:13 +00:00
|
|
|
reactText = ""
|
2022-02-06 15:28:12 +00:00
|
|
|
|
|
|
|
for count in emojiList:
|
|
|
|
emojisTotal += count["count"]
|
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
if emojisTotal > 0:
|
|
|
|
text = nickname + " has written " + str(len(noteList)) + " Notes yesterday, " + formerDate.strftime(
|
|
|
|
'%a %d-%m-%Y') + "\nand used a total of " + str(emojisTotal) + " Emojis. #miceco" + chr(8203) + chr(8203) + chr(
|
|
|
|
8203) + "\n\n" + chr(
|
|
|
|
9553) + " "
|
2022-02-06 15:28:12 +00:00
|
|
|
for element in emojiList:
|
2022-02-20 12:53:13 +00:00
|
|
|
text += str(element["count"]) + "x\u00A0" + element["emoji"] + " " + chr(9553) + " "
|
2022-02-06 15:28:12 +00:00
|
|
|
else:
|
2022-02-20 12:53:13 +00:00
|
|
|
text = nickname + " has written " + str(len(noteList)) + " Notes yesterday, " + formerDate.strftime(
|
|
|
|
'%a %d-%m-%Y') + "\nand didn't used any emojis. #miceco" + chr(8203) + chr(8203) + chr(
|
|
|
|
8203)
|
2022-02-06 15:28:12 +00:00
|
|
|
|
|
|
|
text += reactText
|
|
|
|
|
2022-02-20 13:05:59 +00:00
|
|
|
# print(text)
|
|
|
|
|
2022-02-20 12:53:13 +00:00
|
|
|
try:
|
|
|
|
req = requests.post(url + "/notes/create", json={
|
|
|
|
"i": token,
|
2022-02-20 13:07:25 +00:00
|
|
|
"visibility": "public",
|
2022-02-20 12:53:13 +00:00
|
|
|
"text": text
|
|
|
|
})
|
2022-02-06 15:28:12 +00:00
|
|
|
req.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as err:
|
2022-02-20 12:53:13 +00:00
|
|
|
print("Couldn't create Posts! " + str(err))
|
2022-02-26 06:43:48 +00:00
|
|
|
sys.exit(1)
|