importpywikibotfrompywikibot.commsimporteventstreamsdefhas_bot_rights(username):site_wikidata=pywikibot.Site("wikidata","wikidata")user_page=pywikibot.User(site_wikidata,username)return'bot'inuser_page.groups()deflog_change(user_page,item_id,user,timestamp,edit_summary):# Append the information to the user page on ku.wikipedia.orglog_entry=f"\n|-\n|{{{{wde|{item_id}}}}}\n|{user}\n|{timestamp}\n|<nowiki>{edit_summary}</nowiki>\n"# Find the index of the last occurrence of '|}'last_pipe_brace_index=user_page.text.rfind('|}')# Insert the new log entry before '|}'user_page.text=user_page.text[:last_pipe_brace_index]+log_entry+user_page.text[last_pipe_brace_index:]print(f"Log saved for {item_id}")# Save the modified textuser_page.save(summary=f"Update log with Wikidata change - {edit_summary}")defmonitor_and_log_changes():site_wikidata=pywikibot.Site("wikidata","wikidata")site_kuwiki=pywikibot.Site("ku","wikipedia")# Specify the target user page on ku.wikipedia.orguser_page_title="User:Balyozxane/log wikidata.py/log"user_page=pywikibot.Page(site_kuwiki,user_page_title)# Set to keep track of already logged items and their timestampslogged_items=set()try:print("Starting Wikidata monitor...")# Create an EventStreams object for recent changesstream=eventstreams.EventStreams(streams=['recentchange'])# Infinite loop to continuously monitor recent changesforeventinstream:try:ifevent['type']=='edit'andevent['wiki']=='wikidatawiki':# Check if the change is related to descriptions in Kurdishif('wbsetdescription-add:1|ku-latn'inevent['comment']or'wbsetdescription-set:1|ku-latn'inevent['comment']or'wbeditentity-update-languages-short:0||ku'inevent['comment']or'wbsetdescription-add:1|ku'inevent['comment']or'wbsetdescription-set:1|ku'inevent['comment']):# Check if "quickstatements/#/batch" is in the commentif'quickstatements/#/batch'inevent['comment']:print(f"Ignoring edit with 'quickstatements/#/batch' comment for {item_id}")continueitem_id=event['title']timestamp=event['timestamp']user=event['user']ifuser!='Balyozxane':# Check if the item and timestamp are already loggedif(item_id,timestamp)notinlogged_items:edit_summary=event['comment']ifhas_bot_rights(user):print(f"{user} has bot rights on Wikidata.")else:print(f"{user} does not have bot rights on Wikidata.")# Call the log_change functionprint(f"Edit by {user} found for {item_id}")log_change(user_page,item_id,user,timestamp,edit_summary)# Add the item and timestamp to the setlogged_items.add((item_id,timestamp))exceptExceptionase:print(f"Error processing event: {e}")continueexceptKeyboardInterrupt:print("Monitoring stopped by user.")exceptExceptionase:print(f"Error occurred: {e}")if__name__=="__main__":monitor_and_log_changes()