2018-06-15 13:56:35 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2018-06-21 23:56:04 +00:00
#Recent changes Gamepedia compatible Discord webhook is a project for using a webhook as recent changes page from MediaWiki.
#Copyright (C) 2018 Frisk
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published
#by the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU Affero General Public License for more details.
#You should have received a copy of the GNU Affero General Public License
#along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-06-22 10:18:22 +00:00
#WARNING! SHITTY CODE AHEAD. ENTER ONLY IF YOU ARE SURE YOU CAN TAKE IT
#You have been warned
2018-06-23 10:02:54 +00:00
import time , logging , json , requests , datetime , re , gettext , math , random , os . path , schedule
2018-06-15 13:56:35 +00:00
from bs4 import BeautifulSoup
2018-06-21 23:35:24 +00:00
from collections import defaultdict , Counter
2018-06-17 12:39:22 +00:00
from urllib . parse import quote_plus
2018-06-23 10:02:54 +00:00
2018-06-15 13:56:35 +00:00
with open ( " settings.json " ) as sfile :
settings = json . load ( sfile )
2018-06-17 12:57:06 +00:00
if settings [ " limitrefetch " ] < settings [ " limit " ] and settings [ " limitrefetch " ] != - 1 :
settings [ " limitrefetch " ] = settings [ " limit " ]
2018-06-18 13:34:27 +00:00
logging . basicConfig ( level = settings [ " verbose_level " ] )
2018-06-17 12:57:06 +00:00
if settings [ " limitrefetch " ] != - 1 and os . path . exists ( " lastchange.txt " ) == False :
2018-06-17 12:39:22 +00:00
with open ( " lastchange.txt " , ' w ' ) as sfile :
2018-06-23 14:14:37 +00:00
sfile . write ( " 99999999999 " )
2018-06-15 13:56:35 +00:00
logging . info ( " Current settings: {settings} " . format ( settings = settings ) )
2018-06-18 13:34:27 +00:00
if settings [ " lang " ] != " en " or settings [ " lang " ] == " " :
lang = gettext . translation ( ' rcgcdw ' , localedir = ' locale ' , languages = [ settings [ " lang " ] ] )
lang . install ( )
else :
_ = lambda s : s
2018-06-18 09:30:05 +00:00
2018-06-15 13:56:35 +00:00
def send ( message , name , avatar ) :
2018-06-18 13:34:27 +00:00
try :
req = requests . post ( settings [ " webhookURL " ] , data = { " content " : message , " avatar_url " : avatar , " username " : name } , timeout = 10 )
except :
pass
2018-06-15 13:56:35 +00:00
2018-06-16 11:16:15 +00:00
def safe_read ( request , * keys ) :
if request is None :
return None
try :
request = request . json ( )
for item in keys :
request = request [ item ]
except KeyError :
logging . warning ( " Failure while extracting data from request on key {key} in {change} " . format ( key = item , change = request ) )
return None
except ValueError :
logging . warning ( " Failure while extracting data from request in {change} " . format ( key = item , change = request ) )
return None
return request
2018-06-15 13:56:35 +00:00
2018-06-18 21:31:53 +00:00
def webhook_formatter ( action , STATIC , * * params ) :
logging . debug ( " Received things: {thing} " . format ( thing = params ) )
colornumber = None if isinstance ( STATIC [ " color " ] , str ) else STATIC [ " color " ]
2018-06-16 11:16:15 +00:00
data = { }
data [ " embeds " ] = [ ]
embed = defaultdict ( dict )
if " title " in params :
article_encoded = params [ " title " ] . replace ( " " , " _ " ) . replace ( ' ) ' , ' \ ) ' )
2018-06-16 12:55:51 +00:00
if re . match ( r " \ b \ d { 1,3} \ . \ d { 1,3} \ . \ d { 1,3} \ . \ d { 1,3} \ b " , params [ " user " ] ) is not None :
author_url = " https:// {wiki} .gamepedia.com/Special:Contributions/ {user} " . format ( wiki = settings [ " wiki " ] , user = params [ " user " ] )
if params [ " user " ] not in list ( recent_changes . map_ips . keys ( ) ) :
contibs = safe_read ( recent_changes . safe_request ( " https:// {wiki} .gamepedia.com/api.php?action=query&format=json&list=usercontribs&uclimit=max&ucuser= {user} &ucprop= " . format ( wiki = settings [ " wiki " ] , user = params [ " user " ] ) ) , " query " , " usercontribs " )
2018-06-16 11:16:15 +00:00
if contibs is None :
logging . warning ( " WARNING: Something went wrong when checking amount of contributions for given IP address " )
2018-06-16 12:55:51 +00:00
params [ " user " ] = params [ " user " ] + " (?) "
2018-06-16 11:16:15 +00:00
else :
2018-06-16 12:55:51 +00:00
params [ " user " ] = " {author} ( {contribs} ) " . format ( author = params [ " user " ] , contribs = len ( contibs ) )
recent_changes . map_ips [ params [ " user " ] ] = len ( contibs )
2018-06-16 11:16:15 +00:00
else :
2018-06-16 12:55:51 +00:00
recent_changes . map_ips [ params [ " user " ] ] + = 1
params [ " user " ] = " {author} ( {amount} ) " . format ( author = params [ " user " ] , amount = recent_changes . map_ips [ params [ " user " ] ] )
2018-06-16 11:16:15 +00:00
else :
2018-06-16 12:55:51 +00:00
author_url = " https:// {wiki} .gamepedia.com/User: {user} " . format ( wiki = settings [ " wiki " ] , user = params [ " user " ] . replace ( " " , " _ " ) )
2018-06-16 11:16:15 +00:00
if action in [ 1 , 37 ] : #edit or new page
editsize = params [ " size " ]
2018-06-16 18:27:01 +00:00
print ( editsize )
2018-06-16 11:16:15 +00:00
if editsize > 0 :
if editsize > 6032 :
colornumber = 65280
else :
2018-06-16 18:27:01 +00:00
colornumber = 35840 + ( math . floor ( editsize / ( 52 ) ) ) * 256
2018-06-16 11:16:15 +00:00
elif editsize < 0 :
if editsize < - 6032 :
colornumber = 16711680
else :
2018-06-16 18:27:01 +00:00
colornumber = 9175040 + ( math . floor ( ( editsize * - 1 ) / ( 52 ) ) ) * 65536
2018-06-16 11:16:15 +00:00
elif editsize == 0 :
colornumber = 8750469
link = " https:// {wiki} .gamepedia.com/index.php?title= {article} &curid= {pageid} &diff= {diff} &oldid= {oldrev} " . format ( wiki = settings [ " wiki " ] , pageid = params [ " pageid " ] , diff = params [ " diff " ] , oldrev = params [ " oldrev " ] , article = article_encoded )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = " {article} ( {new} {minor} {editsize} ) " . format ( article = params [ " title " ] , editsize = " + " + str ( editsize ) if editsize > 0 else editsize , new = _ ( " (N!) " ) if action == 37 else " " , minor = _ ( " m " ) if action == 1 and params [ " minor " ] else " " )
2018-06-16 12:55:51 +00:00
elif action == 5 : #sending files
2018-06-16 11:16:15 +00:00
urls = safe_read ( recent_changes . safe_request ( " https:// {wiki} .gamepedia.com/api.php?action=query&format=json&prop=imageinfo&list=&meta=&titles= {filename} &iiprop=timestamp % 7Curl&iilimit=2 " . format ( wiki = settings [ " wiki " ] , filename = params [ " title " ] ) ) , " query " , " pages " )
undolink = " "
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
if urls is not None :
2018-06-16 18:27:01 +00:00
img_info = next ( iter ( urls . values ( ) ) ) [ " imageinfo " ]
2018-06-16 11:16:15 +00:00
embed [ " image " ] [ " url " ] = img_info [ 0 ] [ " url " ]
else :
return
if params [ " overwrite " ] :
2018-06-16 12:09:28 +00:00
img_timestamp = [ x for x in img_info [ 1 ] [ " timestamp " ] if x . isdigit ( ) ]
2018-06-17 12:39:22 +00:00
undolink = " https:// {wiki} .gamepedia.com/index.php?title= {filename} &action=revert&oldimage= {timestamp} % 21 {filenamewon} " . format ( wiki = settings [ " wiki " ] , filename = article_encoded , timestamp = " " . join ( img_timestamp ) , filenamewon = article_encoded [ 5 : ] )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Uploaded a new version of {name} " ) . format ( name = params [ " title " ] )
2018-06-16 11:16:15 +00:00
embed [ " fields " ] = [ { " name " : _ ( " Options " ) , " value " : _ ( " ([preview]( {link} ) | [undo]( {undolink} )) " ) . format ( link = embed [ " image " ] [ " url " ] , undolink = undolink ) } ]
else :
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Uploaded {name} " ) . format ( name = params [ " title " ] )
2018-06-17 12:39:22 +00:00
article_content = safe_read ( recent_changes . safe_request ( " https:// {wiki} .gamepedia.com/api.php?action=query&format=json&prop=revisions&titles= {article} &rvprop=content " . format ( wiki = settings [ " wiki " ] , article = quote_plus ( params [ " title " ] , safe = ' ' ) ) ) , " query " , " pages " )
2018-06-16 11:16:15 +00:00
if article_content is None :
logging . warning ( " Something went wrong when getting license for the image " )
return 0
2018-06-24 10:31:07 +00:00
content = list ( article_content . values ( ) ) [ 0 ] [ ' revisions ' ] [ 0 ] [ ' * ' ]
try :
2018-06-24 18:28:42 +00:00
matches = re . search ( re . compile ( settings [ " license_regex " ] , re . IGNORECASE ) , content )
2018-06-16 11:16:15 +00:00
if matches is not None :
2018-06-24 10:31:07 +00:00
license = matches . group ( " license " )
2018-06-16 11:16:15 +00:00
else :
2018-06-24 18:28:42 +00:00
if re . search ( re . compile ( settings [ " license_regex_detect " ] , re . IGNORECASE ) , content ) is None :
2018-06-24 10:31:07 +00:00
license = _ ( " **No license!** " )
else :
license = " ? "
except IndexError :
logging . error ( " Given regex for the license detection is incorrect. It does not have a capturing group called \" license \" specified. Please fix license_regex value in the config! " )
license = " ? "
2018-06-24 18:28:42 +00:00
except re . error :
logging . error ( " Given regex for the license detection is incorrect. Please fix license_regex or license_regex_detect values in the config! " )
license = " ? "
2018-06-17 12:39:22 +00:00
embed [ " fields " ] = [ { " name " : _ ( " Options " ) , " value " : _ ( " ([preview]( {link} )) " ) . format ( link = embed [ " image " ] [ " url " ] ) } ]
2018-06-16 11:16:15 +00:00
params [ " desc " ] = _ ( " {desc} \n License: {license} " ) . format ( desc = params [ " desc " ] , license = license )
2018-06-16 12:55:51 +00:00
elif action == 6 :
2018-06-24 20:44:29 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = params [ " title " ] . replace ( " " , " _ " ) )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Deleted page {article} " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
elif action == 7 :
2018-06-24 20:44:29 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = params [ " title " ] . replace ( " " , " _ " ) )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Deleted redirect {article} by overwriting " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
elif action == 14 :
2018-06-22 15:14:13 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = params [ " target " ] . replace ( " " , " _ " ) )
2018-06-16 11:16:15 +00:00
params [ " desc " ] = " {supress} . {desc} " . format ( desc = params [ " desc " ] , supress = _ ( " No redirect has been made " ) if params [ " supress " ] == True else _ ( " A redirect has been made " ) )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Moved {article} to {target} " ) . format ( article = params [ " title " ] , target = params [ " target " ] )
2018-06-16 12:55:51 +00:00
elif action == 15 :
2018-06-24 20:44:29 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = params [ " target " ] . replace ( " " , " _ " ) )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Moved {article} to {title} over redirect " ) . format ( article = params [ " title " ] , title = params [ " target " ] )
2018-06-16 12:55:51 +00:00
elif action == 16 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
embed [ " title " ] = _ ( " Moved protection settings from {article} to {title} " ) . format ( article = params [ " title " ] , title = params [ " target " ] )
2018-06-16 12:55:51 +00:00
elif action == 17 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {user} " . format ( wiki = settings [ " wiki " ] , user = params [ " blocked_user " ] . replace ( " " , " _ " ) . replace ( ' ) ' , ' \ ) ' ) )
user = params [ " blocked_user " ] . split ( ' : ' ) [ 1 ]
time = _ ( " infinity and beyond " ) if params [ " duration " ] == " infinite " else params [ " duration " ]
2018-06-16 12:55:51 +00:00
embed [ " title " ] = _ ( " Blocked {blocked_user} for {time} " ) . format ( blocked_user = user , time = time )
elif action == 19 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {user} " . format ( wiki = settings [ " wiki " ] , user = params [ " blocked_user " ] . replace ( " " , " _ " ) . replace ( ' ) ' , ' \ ) ' ) )
user = params [ " blocked_user " ] . split ( ' : ' ) [ 1 ]
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Changed block settings for {blocked_user} " ) . format ( blocked_user = user )
2018-06-16 12:55:51 +00:00
elif action == 18 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {user} " . format ( wiki = settings [ " wiki " ] , user = params [ " blocked_user " ] . replace ( " " , " _ " ) . replace ( ' ) ' , ' \ ) ' ) )
user = params [ " blocked_user " ] . split ( ' : ' ) [ 1 ]
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Unblocked {blocked_user} " ) . format ( blocked_user = user )
2018-06-16 12:55:51 +00:00
elif action == 25 :
2018-06-22 10:18:22 +00:00
link = " https:// {wiki} .gamepedia.com/Special:CommentPermalink/ {commentid} " . format ( wiki = settings [ " wiki " ] , commentid = params [ " commentid " ] )
#link = "https://{wiki}.gamepedia.com/UserProfile:{target}".format(wiki=settings["wiki"], target=params["target"].replace(" ", "_").replace(')', '\)')) old way of linking
2018-06-16 11:16:15 +00:00
embed [ " title " ] = _ ( " Left a comment on {target} ' s profile " ) . format ( target = params [ " target " ] )
2018-06-16 12:55:51 +00:00
elif action == 29 :
2018-06-22 10:18:22 +00:00
#link = "https://{wiki}.gamepedia.com/UserProfile:{target}".format(wiki=settings["wiki"], target=params["target"].replace(" ", "_").replace(')', '\)'))
link = " https:// {wiki} .gamepedia.com/Special:CommentPermalink/ {commentid} " . format ( wiki = settings [ " wiki " ] , commentid = params [ " commentid " ] )
2018-06-16 11:16:15 +00:00
embed [ " title " ] = _ ( " Replied to a comment on {target} ' s profile " ) . format ( target = params [ " target " ] )
2018-06-16 12:55:51 +00:00
elif action == 26 :
2018-06-22 10:18:22 +00:00
#link = "https://{wiki}.gamepedia.com/UserProfile:{target}".format(wiki=settings["wiki"], target=params["target"].replace(" ", "_").replace(')', '\)'))
link = " https:// {wiki} .gamepedia.com/Special:CommentPermalink/ {commentid} " . format ( wiki = settings [ " wiki " ] , commentid = params [ " commentid " ] )
2018-06-16 11:16:15 +00:00
embed [ " title " ] = _ ( " Edited a comment on {target} ' s profile " ) . format ( target = params [ " target " ] )
2018-06-16 12:55:51 +00:00
elif action == 28 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/UserProfile: {target} " . format ( wiki = settings [ " wiki " ] , target = params [ " target " ] . replace ( " " , " _ " ) . replace ( ' ) ' , ' \ ) ' ) )
if params [ " field " ] == " profile-location " :
field = _ ( " Location " )
elif params [ " field " ] == " profile-aboutme " :
field = _ ( " About me " )
elif params [ " field " ] == " profile-link-google " :
2018-06-18 09:30:05 +00:00
field = _ ( " Google link " )
2018-06-16 11:16:15 +00:00
elif params [ " field " ] == " profile-link-facebook " :
2018-06-18 09:30:05 +00:00
field = _ ( " Facebook link " )
2018-06-16 11:16:15 +00:00
elif params [ " field " ] == " profile-link-twitter " :
2018-06-18 09:30:05 +00:00
field = _ ( " Twitter link " )
2018-06-16 11:16:15 +00:00
elif params [ " field " ] == " profile-link-reddit " :
2018-06-18 09:30:05 +00:00
field = _ ( " Reddit link " )
2018-06-16 11:16:15 +00:00
elif params [ " field " ] == " profile-link-twitch " :
2018-06-18 09:30:05 +00:00
field = _ ( " Twitch link " )
2018-06-16 11:16:15 +00:00
elif params [ " field " ] == " profile-link-psn " :
2018-06-18 09:30:05 +00:00
field = _ ( " PSN link " )
2018-06-16 11:16:15 +00:00
elif params [ " field " ] == " profile-link-vk " :
2018-06-18 09:30:05 +00:00
field = _ ( " VK link " )
2018-06-16 11:16:15 +00:00
elif params [ " field " ] == " profile-link-xbl " :
2018-06-18 09:30:05 +00:00
field = _ ( " XVL link " )
2018-06-16 11:16:15 +00:00
elif params [ " field " ] == " profile-link-steam " :
2018-06-18 09:30:05 +00:00
field = _ ( " Steam link " )
2018-06-16 11:16:15 +00:00
else :
field = _ ( " Unknown " )
embed [ " title " ] = _ ( " Edited {target} ' s profile " ) . format ( target = params [ " target " ] )
params [ " desc " ] = _ ( " {field} field changed to: {desc} " ) . format ( field = field , desc = params [ " desc " ] )
2018-06-16 12:55:51 +00:00
elif action == 27 :
2018-06-22 10:18:22 +00:00
link = " https:// {wiki} .gamepedia.com/Special:CommentPermalink/ {commentid} " . format ( wiki = settings [ " wiki " ] , commentid = params [ " commentid " ] )
#link = "https://{wiki}.gamepedia.com/UserProfile:{target}".format(wiki=settings["wiki"], target=params["target"].replace(" ", "_").replace(')', '\)'))
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Deleted a comment on {target} ' s profile " ) . format ( target = params [ " target " ] )
2018-06-16 12:55:51 +00:00
elif action == 20 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ " + params [ " user " ] . replace ( " " , " _ " ) . replace ( ' ) ' , ' \ ) ' )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Changed group membership for {target} " ) . format ( target = params [ " user " ] )
2018-06-16 11:16:15 +00:00
if params [ " old_groups " ] . count ( ' ' ) < params [ " new_groups " ] . count ( ' ' ) :
embed [ " thumbnail " ] [ " url " ] = " https://i.imgur.com/WnGhF5g.gif "
if len ( params [ " old_groups " ] ) < 4 :
params [ " old_groups " ] = _ ( " none " )
if len ( params [ " new_groups " ] ) < 4 :
params [ " new_groups " ] = _ ( " none " )
2018-06-18 09:30:05 +00:00
reason = " | {desc} " . format ( desc = params [ " desc " ] ) if params [ " desc " ] != _ ( " No description provided " ) else " "
params [ " desc " ] = _ ( " Groups changed from {old_groups} to {new_groups} {reason} " ) . format ( old_groups = params [ " old_groups " ] , new_groups = params [ " new_groups " ] , reason = reason )
2018-06-16 12:55:51 +00:00
elif action == 2 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Protected {target} " ) . format ( target = params [ " title " ] )
2018-06-16 11:16:15 +00:00
params [ " desc " ] = params [ " settings " ] + " | " + params [ " desc " ]
2018-06-16 12:55:51 +00:00
elif action == 3 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Changed protection level for {article} " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
params [ " desc " ] = params [ " settings " ] + " | " + params [ " desc " ]
elif action == 4 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Removed protection from {article} " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
elif action == 9 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Changed visibility of revision(s) on page {article} " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
elif action == 11 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
embed [ " title " ] = _ ( " Imported {article} with {count} revision(s) " ) . format ( article = params [ " title " ] , count = params [ " amount " ] )
2018-06-16 12:55:51 +00:00
elif action == 8 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
embed [ " title " ] = _ ( " Restored {article} " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
elif action == 10 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/Special:RecentChanges " . format ( wiki = settings [ " wiki " ] )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Changed visibility of log events " )
2018-06-16 12:55:51 +00:00
elif action == 12 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/Special:RecentChanges " . format ( wiki = settings [ " wiki " ] )
embed [ " title " ] = _ ( " Imported interwiki " )
2018-06-16 12:55:51 +00:00
elif action == 21 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/Special:RecentChanges " . format ( wiki = settings [ " wiki " ] )
embed [ " title " ] = _ ( " Edited abuse filter number {number} " ) . format ( number = params [ " filternr " ] )
2018-06-18 09:30:05 +00:00
elif action == 13 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Merged revision histories of {article} into {dest} " ) . format ( article = params [ " title " ] , dest = params [ " destination " ] )
2018-06-16 12:55:51 +00:00
elif action == 22 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/Special:Interwiki " . format ( wiki = settings [ " wiki " ] )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Added an entry to the interwiki table " )
2018-06-16 11:16:15 +00:00
params [ " desc " ] = _ ( " Prefix: {prefix} , website: {website} | {desc} " ) . format ( desc = params [ " desc " ] , prefix = params [ " prefix " ] , website = params [ " website " ] )
2018-06-16 12:55:51 +00:00
elif action == 23 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/Special:Interwiki " . format ( wiki = settings [ " wiki " ] )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Edited an entry in interwiki table " )
2018-06-16 11:16:15 +00:00
params [ " desc " ] = _ ( " Prefix: {prefix} , website: {website} | {desc} " ) . format ( desc = params [ " desc " ] , prefix = params [ " prefix " ] , website = params [ " website " ] )
2018-06-16 12:55:51 +00:00
elif action == 24 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/Special:Interwiki " . format ( wiki = settings [ " wiki " ] )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Deleted an entry in interwiki table " )
2018-06-16 11:16:15 +00:00
params [ " desc " ] = _ ( " Prefix: {prefix} | {desc} " ) . format ( desc = params [ " desc " ] , prefix = params [ " prefix " ] )
2018-06-16 12:55:51 +00:00
elif action == 30 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 09:30:05 +00:00
embed [ " title " ] = _ ( " Changed the content model of the page {article} " ) . format ( article = params [ " title " ] )
2018-06-16 11:16:15 +00:00
params [ " desc " ] = _ ( " Model changed from {old} to {new} : {reason} " ) . format ( old = params [ " oldmodel " ] , new = params [ " newmodel " ] , reason = params [ " desc " ] )
2018-06-16 12:55:51 +00:00
elif action == 31 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
embed [ " title " ] = _ ( " Edited the sprite for {article} " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
elif action == 32 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
embed [ " title " ] = _ ( " Created the sprite sheet for {article} " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
elif action == 33 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
embed [ " title " ] = _ ( " Edited the slice for {article} " ) . format ( article = params [ " title " ] )
2018-06-16 12:55:51 +00:00
elif action == 34 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 15:26:03 +00:00
embed [ " title " ] = _ ( " Created a tag \" {tag} \" " ) . format ( tag = params [ " additional " ] [ " tag " ] )
2018-06-25 10:45:48 +00:00
recent_changes . update_tags ( )
2018-06-16 12:55:51 +00:00
elif action == 35 :
2018-06-16 11:16:15 +00:00
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 15:26:03 +00:00
embed [ " title " ] = _ ( " Deleted a tag \" {tag} \" " ) . format ( tag = params [ " additional " ] [ " tag " ] )
2018-06-25 10:45:48 +00:00
recent_changes . update_tags ( )
2018-06-18 09:30:05 +00:00
elif action == 36 :
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 15:26:03 +00:00
embed [ " title " ] = _ ( " Activated a tag \" {tag} \" " ) . format ( tag = params [ " additional " ] [ " tag " ] )
2018-06-18 09:30:05 +00:00
elif action == 38 :
link = " https:// {wiki} .gamepedia.com/ {article} " . format ( wiki = settings [ " wiki " ] , article = article_encoded )
2018-06-18 15:26:03 +00:00
embed [ " title " ] = _ ( " Deactivated a tag \" {tag} \" " ) . format ( tag = params [ " additional " ] [ " tag " ] )
2018-06-16 11:16:15 +00:00
else :
2018-06-16 12:55:51 +00:00
logging . warning ( " No entry for {event} with params: {params} " . format ( event = action , params = params ) )
embed [ " author " ] [ " name " ] = params [ " user " ]
2018-06-16 11:16:15 +00:00
embed [ " author " ] [ " url " ] = author_url
2018-06-21 23:35:24 +00:00
embed [ " author " ] [ " icon_url " ] = STATIC [ " icon " ]
2018-06-16 11:16:15 +00:00
embed [ " url " ] = link
if " desc " not in params :
params [ " desc " ] = " "
embed [ " description " ] = params [ " desc " ]
embed [ " color " ] = random . randrange ( 1 , 16777215 ) if colornumber is None else math . floor ( colornumber )
2018-06-18 21:31:53 +00:00
embed [ " timestamp " ] = STATIC [ " timestamp " ]
2018-06-25 09:57:00 +00:00
if STATIC [ " tags " ] :
2018-06-25 10:45:48 +00:00
tag_displayname = [ ]
2018-06-25 09:57:00 +00:00
if " fields " not in embed :
embed [ " fields " ] = [ ]
2018-06-25 10:45:48 +00:00
for tag in STATIC [ " tags " ] :
if tag in recent_changes . tags :
tag_displayname . append ( recent_changes . tags [ tag ] )
else :
tag_displayname . append ( tag )
embed [ " fields " ] . append ( { " name " : " Tags " , " value " : " , " . join ( tag_displayname ) } )
2018-06-16 11:16:15 +00:00
data [ " embeds " ] . append ( dict ( embed ) )
2018-06-18 09:30:05 +00:00
data [ ' avatar_url ' ] = settings [ " avatars " ] [ " embed " ]
2018-06-16 11:16:15 +00:00
formatted_embed = json . dumps ( data , indent = 4 )
headers = { ' Content-Type ' : ' application/json ' }
2018-06-16 18:27:01 +00:00
#logging.debug(data)
2018-06-16 11:16:15 +00:00
result = requests . post ( settings [ " webhookURL " ] , data = formatted_embed , headers = headers )
2018-06-24 10:51:57 +00:00
if result . status_code != requests . codes . ok :
handle_discord_http ( result . status_code , formatted_embed , headers )
def handle_discord_http ( code , formatted_embed , headers ) :
if code == 204 : #message went through
return
elif code == 400 : #HTTP BAD REQUEST
logging . error ( " Following message has been rejected by Discord, please submit a bug on our bugtracker adding it: " )
logging . error ( formatted_embed )
elif code == 401 : #HTTP UNAUTHORIZED
logging . error ( " Webhook URL is invalid or no longer in use, please replace it with proper one. " )
elif code == 429 :
logging . error ( " We are sending too many requests to the Discord, slowing down... " )
time . sleep ( 20.0 )
result = requests . post ( settings [ " webhookURL " ] , data = formatted_embed , headers = headers ) #TODO Replace this solution with less obscure one
elif code > 500 and code < 600 :
logging . error ( " Discord have trouble processing the event, and because the HTTP code returned is 500> it means we blame them. " )
time . sleep ( 20.0 )
result = requests . post ( settings [ " webhookURL " ] , data = formatted_embed , headers = headers )
2018-06-16 11:16:15 +00:00
2018-06-18 21:31:53 +00:00
def first_pass ( change ) : #I've decided to split the embed formatter and change handler, maybe it's more messy this way, I don't know
2018-06-15 13:56:35 +00:00
parsedcomment = ( BeautifulSoup ( change [ " parsedcomment " ] , " lxml " ) ) . get_text ( )
2018-06-18 15:26:03 +00:00
logging . debug ( change )
2018-06-25 09:57:00 +00:00
STATIC_VARS = { " timestamp " : change [ " timestamp " ] , " tags " : change [ " tags " ] }
2018-06-15 13:56:35 +00:00
if not parsedcomment :
2018-06-16 11:16:15 +00:00
parsedcomment = _ ( " No description provided " )
2018-06-15 13:56:35 +00:00
if change [ " type " ] == " edit " :
2018-06-18 21:31:53 +00:00
STATIC_VARS = { * * STATIC_VARS , * * { " color " : settings [ " appearance " ] [ " edit " ] [ " color " ] , " icon " : settings [ " appearance " ] [ " edit " ] [ " icon " ] } }
webhook_formatter ( 1 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , oldrev = change [ " old_revid " ] , pageid = change [ " pageid " ] , diff = change [ " revid " ] , size = change [ " newlen " ] - change [ " oldlen " ] , minor = True if " minor " in change else False )
2018-06-15 17:49:31 +00:00
elif change [ " type " ] == " log " :
logtype = change [ " logtype " ]
logaction = change [ " logaction " ]
2018-06-18 21:31:53 +00:00
combination = " {logtype} / {logaction} " . format ( logtype = logtype , logaction = logaction )
logging . debug ( " combination is {} " . format ( combination ) )
try :
settings [ " appearance " ] [ combination ]
except KeyError :
STATIC_VARS = { * * STATIC_VARS , * * { " color " : " " , " icon " : " " } }
logging . error ( " No value in the settings has been given for {} " . format ( combination ) )
STATIC_VARS = { * * STATIC_VARS , * * { " color " : settings [ " appearance " ] [ combination ] [ " color " ] , " icon " : settings [ " appearance " ] [ combination ] [ " icon " ] } }
2018-06-15 17:49:31 +00:00
if logtype == " protect " and logaction == " protect " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 2 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , settings = change [ " logparams " ] [ " description " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " protect " and logaction == " modify " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 3 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , settings = change [ " logparams " ] [ " description " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " protect " and logaction == " unprotect " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 4 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " upload " and logaction == " overwrite " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 5 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , overwrite = True )
2018-06-15 17:49:31 +00:00
elif logtype == " upload " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 5 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , overwrite = False )
2018-06-15 17:49:31 +00:00
elif logtype == " delete " and logaction == " delete " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 6 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " delete " and logaction == " delete_redir " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 7 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " delete " and logaction == " restore " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 8 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " delete " and logaction == " revision " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 9 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " delete " and logaction == " event " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 10 , STATIC_VARS , user = change [ " user " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " import " and logaction == " upload " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 11 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , amount = change [ " logparams " ] [ " count " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " import " and logaction == " interwiki " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 12 , STATIC_VARS , user = change [ " user " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " merge " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 13 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , destination = change [ " logparams " ] [ " dest_title " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " move " and logaction == " move " :
2018-06-22 13:06:51 +00:00
webhook_formatter ( 14 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , supress = True if " suppressredirect " in change [ " logparams " ] else False , target = change [ " logparams " ] [ ' target_title ' ] )
2018-06-15 17:49:31 +00:00
elif logtype == " move " and logaction == " move_redir " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 15 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , target = change [ " logparams " ] [ " target_title " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " protect " and logaction == " move_prot " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 16 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , target = change [ " logparams " ] [ " oldtitle_title " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " block " and logaction == " block " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 17 , STATIC_VARS , user = change [ " user " ] , blocked_user = change [ " title " ] , desc = parsedcomment , duration = change [ " logparams " ] [ " duration " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " block " and logaction == " unblock " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 18 , STATIC_VARS , user = change [ " user " ] , blocked_user = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " block " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 19 , STATIC_VARS , user = change [ " user " ] , blocked_user = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " rights " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 20 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , old_groups = ' ' . join ( change [ " logparams " ] [ " oldgroups " ] ) , new_groups = ' ' . join ( change [ " logparams " ] [ " newgroups " ] ) )
2018-06-15 17:49:31 +00:00
elif logtype == " abusefilter " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 21 , STATIC_VARS , user = change [ " user " ] , desc = parsedcomment , filternr = change [ " logparams " ] [ ' 1 ' ] )
2018-06-15 17:49:31 +00:00
elif logtype == " interwiki " and logaction == " iw_add " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 22 , STATIC_VARS , user = change [ " user " ] , desc = parsedcomment , prefix = change [ " logparams " ] [ ' 0 ' ] , website = change [ " logparams " ] [ ' 1 ' ] )
2018-06-15 17:49:31 +00:00
elif logtype == " interwiki " and logaction == " iw_edit " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 23 , STATIC_VARS , user = change [ " user " ] , desc = parsedcomment , prefix = change [ " logparams " ] [ ' 0 ' ] , website = change [ " logparams " ] [ ' 1 ' ] )
2018-06-15 17:49:31 +00:00
elif logtype == " interwiki " and logaction == " iw_delete " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 24 , STATIC_VARS , user = change [ " user " ] , desc = parsedcomment , prefix = change [ " logparams " ] [ ' 0 ' ] )
2018-06-15 17:49:31 +00:00
elif logtype == " curseprofile " and logaction == " comment-created " :
2018-06-22 10:18:22 +00:00
webhook_formatter ( 25 , STATIC_VARS , user = change [ " user " ] , target = change [ " title " ] . split ( ' : ' ) [ 1 ] , commentid = change [ " logparams " ] [ " 0 " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " curseprofile " and logaction == " comment-edited " :
2018-06-22 10:18:22 +00:00
webhook_formatter ( 26 , STATIC_VARS , user = change [ " user " ] , target = change [ " title " ] . split ( ' : ' ) [ 1 ] , commentid = change [ " logparams " ] [ " 0 " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " curseprofile " and logaction == " comment-deleted " :
2018-06-22 10:18:22 +00:00
webhook_formatter ( 27 , STATIC_VARS , user = change [ " user " ] , target = change [ " title " ] . split ( ' : ' ) [ 1 ] , commentid = change [ " logparams " ] [ " 0 " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " curseprofile " and logaction == " profile-edited " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 28 , STATIC_VARS , user = change [ " user " ] , target = change [ " title " ] . split ( ' : ' ) [ 1 ] , field = change [ " logparams " ] [ ' 0 ' ] , desc = change [ " parsedcomment " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " curseprofile " and logaction == " comment-replied " :
2018-06-22 10:18:22 +00:00
webhook_formatter ( 29 , STATIC_VARS , user = change [ " user " ] , target = change [ " title " ] . split ( ' : ' ) [ 1 ] , commentid = change [ " logparams " ] [ " 0 " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " contentmodel " and logaction == " change " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 30 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , oldmodel = change [ " logparams " ] [ " oldmodel " ] , newmodel = change [ " logparams " ] [ " newmodel " ] )
elif logtype == " sprite " and logaction == " sprite " :
webhook_formatter ( 31 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " sprite " and logaction == " sheet " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 32 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " sprite " and logaction == " slice " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 33 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
elif logtype == " managetags " and logaction == " create " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 34 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , additional = change [ " logparams " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " managetags " and logaction == " delete " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 35 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , additional = change [ " logparams " ] )
2018-06-18 09:30:05 +00:00
elif logtype == " managetags " and logaction == " activate " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 36 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , additional = change [ " logparams " ] )
2018-06-18 09:30:05 +00:00
elif logtype == " managetags " and logaction == " deactivate " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 38 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , additional = change [ " logparams " ] )
2018-06-15 17:49:31 +00:00
elif logtype == " tag " and logaction == " update " :
2018-06-18 21:31:53 +00:00
webhook_formatter ( 39 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment )
2018-06-15 17:49:31 +00:00
else :
logging . warning ( " No entry matches given change! " )
print ( change )
2018-06-18 09:30:05 +00:00
send ( _ ( " Unable to process the event " ) , _ ( " error " ) , settings [ " avatars " ] [ " no_event " ] )
2018-06-15 17:49:31 +00:00
return
2018-06-17 12:39:22 +00:00
if change [ " type " ] == " external " : #not sure what happens then, but it's listed as possible type
logging . warning ( " External event happened, ignoring. " )
print ( change )
return
elif change [ " type " ] == " new " : #new page
2018-06-18 21:31:53 +00:00
STATIC_VARS = { * * STATIC_VARS , * * { " color " : settings [ " appearance " ] [ " new " ] [ " color " ] , " icon " : settings [ " appearance " ] [ " new " ] [ " icon " ] } }
webhook_formatter ( 37 , STATIC_VARS , user = change [ " user " ] , title = change [ " title " ] , desc = parsedcomment , oldrev = change [ " old_revid " ] , pageid = change [ " pageid " ] , diff = change [ " revid " ] , size = change [ " newlen " ] )
2018-06-21 23:35:24 +00:00
def day_overview_request ( ) :
logging . info ( " Fetching daily overview... This may take up to 30 seconds! " )
timestamp = ( datetime . datetime . utcnow ( ) - datetime . timedelta ( hours = 24 ) ) . isoformat ( timespec = ' milliseconds ' )
logging . debug ( " timestamp is {} " . format ( timestamp ) )
complete = False
result = [ ]
passes = 0
continuearg = " "
while not complete and passes < 10 :
request = recent_changes . safe_request ( " https:// {wiki} .gamepedia.com/api.php?action=query&format=json&list=recentchanges&rcend= {timestamp} Z&rcprop=title % 7Ctimestamp % 7Csizes % 7Cloginfo % 7Cuser&rcshow=!bot&rclimit=500&rctype=edit % 7Cnew % 7Clog % 7Ccategorize {continuearg} " . format ( wiki = settings [ " wiki " ] , timestamp = timestamp , continuearg = continuearg ) )
if request :
try :
request = request . json ( )
rc = request [ ' query ' ] [ ' recentchanges ' ]
continuearg = request [ " continue " ] [ " rccontinue " ] if " continue " in request else None
except ValueError :
logging . warning ( " ValueError in fetching changes " )
self . downtime_controller ( )
complete = 2
except KeyError :
logging . warning ( " Wiki returned %s " % ( request . json ( ) ) )
complete = 2
else :
result + = rc
if continuearg :
continuearg = " &rccontinue= {} " . format ( continuearg )
passes + = 1
logging . debug ( " continuing requesting next pages of recent changes with {} passes and continuearg being {} " . format ( passes , continuearg ) )
time . sleep ( 3.0 )
else :
complete = 1
else :
complete = 2
if passes == 10 :
logging . debug ( " quit the loop because there been too many passes " )
return ( result , complete )
def add_to_dict ( dictionary , key ) :
if key in dictionary :
dictionary [ key ] + = 1
else :
dictionary [ key ] = 1
return dictionary
2018-06-15 13:56:35 +00:00
2018-06-21 23:35:24 +00:00
def day_overview ( ) : #time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time()))
#(datetime.datetime.utcnow()+datetime.timedelta(hours=0)).isoformat(timespec='milliseconds')+'Z'
result = day_overview_request ( )
if result [ 1 ] == 1 :
activity = defaultdict ( dict )
hours = defaultdict ( dict )
edits = 0
files = 0
admin = 0
changed_bytes = 0
new_articles = 0
for item in result [ 0 ] :
activity = add_to_dict ( activity , item [ " user " ] )
hours = add_to_dict ( hours , datetime . datetime . strptime ( item [ " timestamp " ] , " % Y- % m- %d T % H: % M: % SZ " ) . hour )
if item [ " type " ] == " edit " :
edits + = 1
changed_bytes + = item [ " newlen " ] - item [ " oldlen " ]
if item [ " type " ] == " new " :
if item [ " ns " ] == 0 :
new_articles + = 1
changed_bytes + = item [ " newlen " ]
if item [ " type " ] == " log " :
files = files + 1 if item [ " logtype " ] == item [ " logaction " ] == " upload " else files
admin = admin + 1 if item [ " logtype " ] in [ " delete " , " merge " , " block " , " protect " , " import " , " rights " , " abusefilter " , " interwiki " , " managetags " ] else admin
2018-06-24 20:12:09 +00:00
overall = round ( new_articles + edits * 0.1 + files * 0.3 + admin * 0.1 + math . fabs ( changed_bytes * 0.001 ) , 2 )
2018-06-21 23:35:24 +00:00
embed = defaultdict ( dict )
embed [ " title " ] = _ ( " Daily overview " )
embed [ " url " ] = " https:// {wiki} .gamepedia.com/Special:Statistics " . format ( wiki = settings [ " wiki " ] )
embed [ " color " ] = settings [ " appearance " ] [ " daily_overview " ] [ " color " ]
2018-06-25 09:21:07 +00:00
embed [ " author " ] [ " icon_url " ] = settings [ " appearance " ] [ " daily_overview " ] [ " icon " ]
embed [ " author " ] [ " name " ] = settings [ " wikiname " ]
embed [ " author " ] [ " url " ] = " https:// {wiki} .gamepedia.com/ " . format ( wiki = settings [ " wiki " ] )
2018-06-21 23:35:24 +00:00
if activity :
v = activity . values ( )
active_users = [ ]
for user , numberu in Counter ( activity ) . most_common ( list ( v ) . count ( max ( v ) ) ) : #find most active users
active_users . append ( user )
the_one = random . choice ( active_users )
v = hours . values ( )
active_hours = [ ]
for hour , numberh in Counter ( hours ) . most_common ( list ( v ) . count ( max ( v ) ) ) : #find most active users
active_hours . append ( str ( hour ) )
2018-06-25 09:21:07 +00:00
usramount = _ ( " ( {} actions) " ) . format ( numberu )
houramount = _ ( " UTC ( {} actions) " ) . format ( numberh )
2018-06-21 23:35:24 +00:00
else :
active_users = [ _ ( " But nobody came " ) ] #a reference to my favorite game of all the time, sorry ^_^
active_hours = [ _ ( " But nobody came " ) ]
2018-06-25 09:21:07 +00:00
usramount = " "
houramount = " "
2018-06-21 23:35:24 +00:00
embed [ " fields " ] = [ ]
2018-06-25 09:21:07 +00:00
fields = ( ( _ ( " Most active users " ) , ' , ' . join ( active_users ) + usramount ) , ( _ ( " Edits made " ) , edits ) , ( _ ( " New files " ) , files ) , ( _ ( " Admin actions " ) , admin ) , ( _ ( " Bytes changed " ) , changed_bytes ) , ( _ ( " New articles " ) , new_articles ) , ( _ ( " Unique contributors " ) , str ( len ( activity ) ) ) , ( _ ( " Most active hours " ) , ' , ' . join ( active_hours ) + houramount ) , ( _ ( " Day score " ) , str ( overall ) ) )
2018-06-21 23:35:24 +00:00
for name , value in fields :
embed [ " fields " ] . append ( { " name " : name , " value " : value } )
data = { }
data [ " embeds " ] = [ dict ( embed ) ]
formatted_embed = json . dumps ( data , indent = 4 )
headers = { ' Content-Type ' : ' application/json ' }
logging . debug ( formatted_embed )
result = requests . post ( settings [ " webhookURL " ] , data = formatted_embed , headers = headers )
else :
logging . debug ( " function requesting changes for day overview returned with error code " )
class recent_changes_class ( object ) :
2018-06-15 13:56:35 +00:00
starttime = time . time ( )
ids = [ ]
2018-06-16 11:16:15 +00:00
map_ips = { }
2018-06-15 13:56:35 +00:00
recent_id = 0
downtimecredibility = 0
2018-06-16 11:16:15 +00:00
last_downtime = 0
2018-06-21 23:35:24 +00:00
clock = 0
2018-06-25 10:45:48 +00:00
tags = { }
2018-06-17 12:57:06 +00:00
if settings [ " limitrefetch " ] != - 1 :
with open ( " lastchange.txt " , " r " ) as record :
2018-06-23 14:14:37 +00:00
file_content = record . read ( ) . strip ( )
if file_content :
file_id = int ( file_content )
logging . debug ( " File_id is {val} " . format ( val = file_id ) )
else :
logging . debug ( " File is empty " )
file_id = 999999999
2018-06-17 12:57:06 +00:00
else :
2018-06-18 21:31:53 +00:00
file_id = 999999999 #such value won't cause trouble, and it will make sure no refetch happens
2018-06-15 13:56:35 +00:00
def add_cache ( self , change ) :
self . ids . append ( change [ " rcid " ] )
2018-06-17 12:39:22 +00:00
#self.recent_id = change["rcid"]
2018-06-15 13:56:35 +00:00
if len ( self . ids ) > settings [ " limit " ] + 5 :
self . ids . pop ( 0 )
2018-06-17 12:39:22 +00:00
def fetch ( self , amount = settings [ " limit " ] ) :
2018-06-18 13:34:27 +00:00
last_check = self . fetch_changes ( amount = amount )
self . recent_id = last_check if last_check is not None else self . recent_id
2018-06-17 12:57:06 +00:00
if settings [ " limitrefetch " ] != - 1 and self . recent_id != self . file_id :
2018-06-17 12:39:22 +00:00
self . file_id = self . recent_id
with open ( " lastchange.txt " , " w " ) as record :
record . write ( str ( self . file_id ) )
logging . debug ( " Most recent rcid is: {} " . format ( self . recent_id ) )
def fetch_changes ( self , amount , clean = False ) :
2018-06-18 15:26:03 +00:00
if len ( self . ids ) == 0 :
logging . debug ( " ids is empty, triggering clean fetch " )
2018-06-15 13:56:35 +00:00
clean = True
2018-06-17 12:39:22 +00:00
changes = self . safe_request ( " https:// {wiki} .gamepedia.com/api.php?action=query&format=json&list=recentchanges&rcshow=!bot&rcprop=title % 7Ctimestamp % 7Cids % 7Cloginfo % 7Cparsedcomment % 7Csizes % 7Cflags % 7Ctags % 7Cuser&rclimit= {amount} &rctype=edit % 7Cnew % 7Clog % 7Cexternal " . format ( wiki = settings [ " wiki " ] , amount = amount ) )
2018-06-15 13:56:35 +00:00
if changes :
try :
changes = changes . json ( ) [ ' query ' ] [ ' recentchanges ' ]
changes . reverse ( )
except ValueError :
logging . warning ( " ValueError in fetching changes " )
self . downtime_controller ( )
return None
except KeyError :
2018-06-21 23:35:24 +00:00
logging . warning ( " Wiki returned %s " % ( changes . json ( ) ) )
2018-06-15 13:56:35 +00:00
return None
else :
2018-06-18 09:30:05 +00:00
if self . downtimecredibility > 0 :
self . downtimecredibility - = 1
2018-06-16 11:26:21 +00:00
for change in changes :
2018-06-15 13:56:35 +00:00
if change [ " rcid " ] in self . ids :
continue
self . add_cache ( change )
2018-06-17 12:39:22 +00:00
if clean and not ( self . recent_id == 0 and change [ " rcid " ] > self . file_id ) :
logging . debug ( " Rejected {val} " . format ( val = change [ " rcid " ] ) )
2018-06-15 13:56:35 +00:00
continue
2018-06-16 12:55:51 +00:00
first_pass ( change )
2018-06-23 14:27:57 +00:00
time . sleep ( 3.0 ) #sadly, the time here needs to be quite high, otherwise we are being rate-limited by the Discord, especially during re-fetch
2018-06-17 12:39:22 +00:00
return change [ " rcid " ]
2018-06-15 13:56:35 +00:00
def safe_request ( self , url ) :
try :
request = requests . get ( url , timeout = 10 , headers = settings [ " header " ] )
except requests . exceptions . Timeout :
2018-06-18 13:34:27 +00:00
logging . warning ( " Reached timeout error for request on link {url} " . format ( url = url ) )
2018-06-15 13:56:35 +00:00
self . downtime_controller ( )
return None
except requests . exceptions . ConnectionError :
2018-06-18 13:34:27 +00:00
logging . warning ( " Reached connection error for request on link {url} " . format ( url = url ) )
2018-06-15 13:56:35 +00:00
self . downtime_controller ( )
return None
else :
return request
2018-06-18 13:34:27 +00:00
def check_connection ( self , looped = False ) :
2018-06-15 13:56:35 +00:00
online = 0
for website in [ " https://google.com " , " https://instagram.com " , " https://steamcommunity.com " ] :
try :
requests . get ( website , timeout = 10 )
online + = 1
except requests . exceptions . ConnectionError :
pass
except requests . exceptions . Timeout :
pass
if online < 1 :
logging . error ( " Failure when checking Internet connection at {time} " . format ( time = time . strftime ( " %a , %d % b % Y % H: % M: % S " , time . localtime ( ) ) ) )
2018-06-18 13:34:27 +00:00
self . downtimecredibility = 0
if looped == False :
while 1 : #recursed loop, check for connection (every 10 seconds) as long as three services are down, don't do anything else
if self . check_connection ( looped = True ) :
break
time . sleep ( 10 )
2018-06-15 13:56:35 +00:00
return False
return True
def downtime_controller ( self ) :
2018-06-18 13:34:27 +00:00
if settings [ " show_updown_messages " ] == False :
return
2018-06-15 13:56:35 +00:00
if self . downtimecredibility < 60 :
self . downtimecredibility + = 15
else :
2018-06-18 13:34:27 +00:00
if ( time . time ( ) - self . last_downtime ) > 1800 and self . check_connection ( ) : #check if last downtime happened within 30 minutes, if yes, don't send a message
send ( _ ( " {wiki} seems to be down or unreachable. " ) . format ( wiki = settings [ " wikiname " ] ) , _ ( " Connection status " ) , settings [ " avatars " ] [ " connection_failed " ] )
2018-06-16 11:16:15 +00:00
self . last_downtime = time . time ( )
2018-06-22 13:21:28 +00:00
def clear_cache ( self ) :
self . map_ips = { }
2018-06-25 10:45:48 +00:00
def update_tags ( self ) :
tags_read = safe_read ( self . safe_request ( " https:// {wiki} .gamepedia.com/api.php?action=query&format=json&list=tags&tgprop=name % 7Cdisplayname " . format ( wiki = settings [ " wiki " ] ) ) , " query " , " tags " )
if tags_read :
for tag in tags_read :
self . tags [ tag [ " name " ] ] = ( BeautifulSoup ( tag [ " displayname " ] , " lxml " ) ) . get_text ( )
else :
logging . warning ( " Could not retrive tags. Internal names will be used! " )
2018-06-21 23:35:24 +00:00
recent_changes = recent_changes_class ( )
2018-06-25 10:45:48 +00:00
recent_changes . update_tags ( )
time . sleep ( 1.0 )
2018-06-17 12:57:06 +00:00
recent_changes . fetch ( amount = settings [ " limitrefetch " ] if settings [ " limitrefetch " ] != - 1 else settings [ " limit " ] )
2018-06-21 23:35:24 +00:00
schedule . every ( settings [ " cooldown " ] ) . seconds . do ( recent_changes . fetch )
if 1 == 2 : #dummy for future translations
print ( _ ( " {wiki} is back up! " ) )
2018-06-18 15:26:03 +00:00
2018-06-21 23:35:24 +00:00
if settings [ " overview " ] :
2018-06-22 15:14:13 +00:00
schedule . every ( ) . day . at ( " {} : {} " . format ( time . strptime ( settings [ " overview_time " ] , ' % H: % M ' ) . tm_hour , time . strptime ( settings [ " overview_time " ] , ' % H: % M ' ) . tm_min ) ) . do ( day_overview )
2018-06-22 13:21:28 +00:00
schedule . every ( ) . day . at ( " 00:00 " ) . do ( recent_changes . clear_cache )
2018-06-15 13:56:35 +00:00
while 1 :
2018-06-21 23:35:24 +00:00
time . sleep ( 1.0 )
schedule . run_pending ( )