-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyk2.py
160 lines (105 loc) · 4.1 KB
/
dyk2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pywikibot, re, calendar
sagatave = "Veidne:Vai tu zināji/Sagatave"#"Dalībnieks:Edgars2007/Dyk sagatave"
sagatave2 = "Veidne:Vai tu zināji/Sagatave"
ieteikumi = "Veidne:Vai tu zināji/Ieteiktie fakti"
import datetime
from datetime import date, timedelta
yesterday = date.today() + timedelta(1)
lastday = yesterday.strftime('%d')
currDate = datetime.datetime.now()
daysInMonth = datetime.datetime.now()
dienas =1
sakums = int(lastday)
daysmonth = calendar.monthrange(currDate.year, currDate.month)[1]
summarytext = "Bots: faktu pievienošana Sākumlapai"
#file = open("dz-vieta-hokejisti.txt", "w", encoding='utf-8')
site = pywikibot.Site("lv", "wikipedia", user='Edgars2007')
def formatImage(group1, group2, group3):
group3 = group3 or ''
group2 = group2 or ''
group1 = group1 or ''
if group3!='':
string = "{{#ifeq:{{{2|}}}|att|{{space}}"+group2+"}}"+group3
else:
string = group1+"{{#ifeq:{{{2|}}}|att|"+group2+"}}"
return string
def getFacts(text,end):
splittedtext = text.split('\n<!--dyk diena -->\n')
splittedtext = splittedtext[1:end]
splittedtextRET = '\n<!--dyk diena -->\n'.join(splittedtext)
splittedtextRET = '<!--dyk diena -->\n'+splittedtextRET
usedfacts = splittedtext[::-1]
used = '\n\n'.join(usedfacts)
return splittedtext,used,splittedtextRET
def cleanPage(text,usedFacts,splittedtextRET):
archivetext = "{{ombox | text = Saraksta sākumā ir jaunākie fakti}}"
usedFacts = re.sub(r'(\[\[Attēls:.*\|)(\d+px)\]\]', r'\g<1>50px]]',usedFacts)
newarchive = archivetext+"\n\n"+usedFacts
text = text.replace(splittedtextRET,'')
text = text.replace(archivetext,newarchive)
return text
def addFacts(text,comment):
oldtext = "}}<noinclude>\n{{dokumentācija|Veidne:Vai tu zināji/doc}}</noinclude>"
newtext = str(comment)+"\n}}<noinclude>\n{{dokumentācija|Veidne:Vai tu zināji/doc}}</noinclude>"
text = text.replace(oldtext,newtext)
return text
def addFactsF(facts):
#pywikibot.output(facts)
nextDay = sakums
factjoiner = []
counter = 0
for day in facts:
#pywikibot.output(day)
#"""
#if counter<days:
if nextDay==daysmonth:
switcher = str(nextDay)+'|0'
comment = nextDay
nextDay = 1
else:
switcher = nextDay
comment = nextDay
nextDay += 1
#dfsdf = day[counter]
counter +=1
searchres = re.search('(\[\[Attēls:.*\|\d+px\]\])\n([^$]+)',day)
if searchres:
file = searchres.group(1)
dayfacts = searchres.group(2)
dayfactsFILE = re.search('(.)(<small>.+?<\/small>)([\.\?\!\,\:])?',dayfacts)
if dayfactsFILE:
gr1 = dayfactsFILE.group(1) or ''
gr2 = dayfactsFILE.group(2) or ''
gr3 = dayfactsFILE.group(3) or ''
imageformatterOLD = gr1+gr2+gr3
imageformatter = formatImage(gr1, gr2, gr3)
dayfacts = dayfacts.replace(imageformatterOLD,imageformatter)
#formatfile
tojoiner = "<!--"+str(comment)+". datums\n-->|"+str(switcher)+"={{#ifeq:{{{2|}}}|att|"+str(file)+"}}\n"+str(dayfacts)
factjoiner.append(tojoiner)
joinedFacts = '\n'.join(factjoiner)
#pywikibot.output(joinedFacts)
return joinedFacts
#"""
def main():
saglapa = pywikibot.Page(site,sagatave)
saglapaFROMcopy = pywikibot.Page(site,sagatave2)
ietlapa = pywikibot.Page(site,ieteikumi)
sagatT = saglapaFROMcopy.get()
ieteikumuText = ietlapa.get()
#pywikibot.output(ieteikumuText)
sagataveText = saglapa.get()
splittedtext,used,splittedtextRET = getFacts(ieteikumuText,dienas+1)
factstoadd = addFactsF(splittedtext)
if '... ...' in factstoadd or '[[Attēls:...|' in factstoadd: return 0
sagataveNEW = addFacts(sagatT,factstoadd)
saglapa.text = sagataveNEW
ieteikumiNEW = cleanPage(ieteikumuText,used,splittedtextRET)
ietlapa.text = ieteikumiNEW
#pywikibot.showDiff(ieteikumuText, ieteikumiNEW)
#pywikibot.showDiff(sagataveText, sagataveNEW)
saglapa.save(summary=summarytext, botflag=False, minor=False)
ietlapa.save(summary=summarytext, botflag=False, minor=False)
return 1
#if __name__ == "__main__":
# main()