forked from hnykda/BatchGeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchgeoedit.py
executable file
·216 lines (158 loc) · 7.63 KB
/
batchgeoedit.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# encoding: utf-8
'''
@author: Daniel Hnyk
@copyright: 2015 Daniel Hnyk
@license: BSD
@contact: [email protected]
@deffield updated: 19.04.2015
'''
'''
TODO
-----
* border exceptions - if there are really twice...
'''
import sys
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from argparse import ArgumentParser
from argparse import RawDescriptionHelpFormatter
from time import sleep
__all__ = []
__version__ = 0.2
__date__ = '2015-04-18'
__updated__ = '2017-08-14'
program_name = os.path.basename( sys.argv[0] )
program_version = "v%s" % __version__
program_build_date = str( __updated__ )
program_version_message = '%%(prog)s %s (%s)' % ( program_version, program_build_date )
program_shortdesc = __import__( '__main__' ).__doc__.split( "\n" )[1]
program_license = '''%s
Created by Daniel Hnyk on %s.
Copyright 2015 Daniel Hnyk. All rights reserved.
Licensed under the BSD.
Distributed on an "AS IS" basis without warranties
or conditions of any kind, either express or implied.
USAGE
''' % ( program_shortdesc, str( __date__ ) )
# Setup argument parser
parser = ArgumentParser( description = program_license, formatter_class = RawDescriptionHelpFormatter )
parser.add_argument( "-v", "--verbose", dest = "verbose", action = "store_true", help = "Verbose mode." )
parser.add_argument( "-d", "--driver", choices = ["firefox", "phantomjs"], default = "firefox", help = "Which driver should be used (default: %(default)s)" )
parser.add_argument( "-f", "--file", default = "input.html", help = "Input file formated in HTML which should be pasted into Long Description (default: %(default)s)" )
parser.add_argument( "-c", "--codes", required = True, type = str, help = "Codes of geocaches separated by comma. E.g. 'GC3YJME,GC3YJMX'" )
parser.add_argument( '-V', '--version', action = 'version', version = program_version_message )
parser.add_argument( "-m", "--maternal_url", default = "http://www.geocaching.com/geocache/", type = str, help = "Maternal URL of geocaches (default: %(default)s)" )
parser.add_argument( "-l", "--logins", required = True, help = "Login names and passwords in 'user1:passwd1,user2:passwd2'" )
parser.add_argument( '-s', "--submit", action = 'store_true', help = "If changes should be submited." )
parser.add_argument( '-b', "--border", default = "<!--DONTCHANGE-->", help = "Every long description must contain this string exactly TWICE. Everything between these two occurences will be replaced. (default: %(default)s)" )
parser.add_argument( '--log', default = "out.log", help = "Name of log file. (default: %(default)s)" )
# Process arguments
args = parser.parse_args()
codes = args.codes.split( "," )
users = args.logins.split( "," )
content = ""
with open( args.file, "r", encoding = "utf8" ) as ifile:
content = ifile.read()
# print( content )
dr = ""
if args.driver == "firefox":
dr = webdriver.Firefox()
elif args.driver == "phantomjs":
dr = webdriver.PhantomJS()
dr.set_window_size( 1400, 1000 )
def verb( st ):
if args.verbose:
print( st )
with open( args.log, "a", encoding = "utf8" ) as logfile:
logfile.write( st + "\n" )
class Cache():
code = ""
url = ""
def click( self, xpath ):
dr.find_element_by_xpath( xpath ).click()
def sign_out( self ):
# sign out
self.click( "//*[contains(@class, 'li-user-toggle')]" )
self.click( "//*[contains(@class, 'sign-out')]" )
#body = dr.find_element_by_tag_name( "body" )
#body.send_keys( Keys.ALT + Keys.SHIFT + 's' )
#self.click( '//*[@id="ctl00_hlSignOut"]' )
sleep( 1 )
def __init__( self, code ):
self.code = code
self.url = args.maternal_url + code
verb( "Processing: " + self.code )
trying = True
for user in users:
if trying:
login_name, password = user.split( ":" )
verb( "Username: " + login_name )
dr.get( self.url )
name_of_cache = dr.find_element_by_xpath( '//*[@id="ctl00_ContentBody_CacheName"]' ).text
verb( "Name of cache: " + name_of_cache )
try:
# sign in form
self.click( '//*[@id="hlSignIn"]' )
lgn = dr.find_element_by_xpath( '//*[@id="Username"]' )
lgn.send_keys( login_name )
psw = dr.find_element_by_xpath( '//*[@id="Password"]' )
psw.send_keys( password )
# check box remember me (maybe not necessary)
#self.click( '//*[@id="ctl00_cbRememberMe"]' )
# submit login form
self.click( '//*[@id="Login"]' )
except NoSuchElementException:
verb( "You are already login" )
try:
# click on edit link
#self.click( '//*[@id="ctl00_ContentBody_GeoNav_adminTools"]/li[2]/a' )
#self.find_element_by_link_text("View / Edit Log / Images")
link = dr.find_element_by_xpath( '//a[contains(text(),"View / Edit Log / Images")]' )
href = link.get_attribute( "href" )
dr.get(href)
self.click( '//*[@id="ctl00_ContentBody_LogBookPanel1_lnkBtnEdit"]' )
# inser to text area of long description
textarea = dr.find_element_by_xpath( '//*[@id="ctl00_ContentBody_LogBookPanel1_uxLogInfo"]' )
text = textarea.get_attribute( "value" )
#verb( '------OLD TEXT----' )
#verb( text )
#verb( '------END OF OLD TEXT-----\n' )
if not args.border: # if something is set
ftf = text.find( 'FTF' )
stf = text.find( 'STF' )
prefix = ''
if ftf > 0:
prefix = '(FTF)'
if stf > 0:
prefix = '(STF)'
start_pos = text.find( args.border )
if start_pos > 0:
dont_change = '\n' + text[start_pos:]
else:
dont_change = '\n'+r'=^.^= Ƹ̵̡Ӝ̵̨̄Ʒ' + '\n\n' + r'*´¨)' + '\n' + r'¸.•´¸.•*´¨) ¸.•*¨)' + '\n' + r'(¸.•´ (¸.•` ¤ Mayus & María ¤'
textarea.clear() # clear textarea
verb( '------OLD TEXT SAVED----' )
verb( dont_change )
verb( '------OLD TEXT SAVED-----\n' )
verb( '------NEW TEXT----' )
verb( prefix + '\n' + content + '\n' + dont_change )
verb( '------END OF NEW TEXT-----\n' )
textarea.send_keys( prefix + '\n' + content + '\n' + dont_change)
if args.submit:
# submit edit
self.click( '//*[@id="ctl00_ContentBody_LogBookPanel1_btnSubmitLog"]' )
sleep( 1 )
#self.sign_out()
trying = False
except NoSuchElementException:
verb( "You have no rights to edit this geocache!" )
self.sign_out()
dr.get( self.url )
body = dr.find_element_by_tag_name( "body" )
body.send_keys( Keys.CONTROL + 't' )
if __name__ == "__main__":
for code in codes:
Cache( code )
sys.exit( 0 )