forked from osmcz/greeter-osm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreeter_osm.py
More file actions
executable file
·171 lines (138 loc) · 5.31 KB
/
greeter_osm.py
File metadata and controls
executable file
·171 lines (138 loc) · 5.31 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
based on greeter_osmsk.py 0.7
skript, ktory sleduje newusers rss feed a ked objavi
novacika editujuceho slovensko, posle mu uvitaciu spravu
"""
import argparse
import configparser
import getpass
import logging
import os
import urllib.parse
import bs4
import mechanize
import requests
#pylint: disable=no-member,unsupported-assignment-operation
#pylint: disable=invalid-name
RSSURL = 'https://resultmaps.neis-one.org/newestosmcountryfeed.php?c={}'
CONFIG = 'greeter-osm.conf'
parser = argparse.ArgumentParser(description='send OSM welcome message to '
'a user with the first changeset '
'made in a region')
parser.add_argument('-d',
help='debug mode',
action='store_true',
dest='debug')
parser.add_argument('-l',
metavar='logfile',
help='log to file (implies -d)',
nargs=1,
type=str)
parser.add_argument('-n',
help='do NOT send the actual message',
action='store_true',
dest='nosend')
parser.add_argument('-u',
metavar='user',
help='send message to USER',
nargs=1,
type=str)
options = parser.parse_args()
if options.l:
logging.basicConfig(level=logging.DEBUG, filename=options.l[0])
logging.getLogger("requests").setLevel(logging.INFO)
elif options.debug:
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("requests").setLevel(logging.INFO)
req_cookies = {}
def osm_auth(username, password):
""" Login to OSM.org and returns megchanize.Browser. """
logging.debug('Authenticating..')
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'https://github.com/osmcz/greeter-osm')]
br.open("https://www.openstreetmap.org/login/")
br.select_form(id="login_form")
br["username"] = username
br["password"] = password
br.submit()
return br
def osm_send(browser, subject, message, to):
""" Sends message using mechanize.Browser to user in OSM """
logging.debug('Sending %s : %s \n to %s', subject, message, to)
browser.open("https://www.openstreetmap.org/message/new/{}".format(to))
browser.select_form(id="new_message")
browser["message[title]"] = subject
browser["message[body]"] = message
browser.submit()
config = configparser.RawConfigParser()
currdir = os.getcwd()
config.read(os.path.join(currdir, CONFIG))
senderlogin = config.get('Auth', 'username')
if not senderlogin:
senderlogin = input("Username: ")
senderpass = config.get('Auth', 'password')
if not senderpass:
senderpass = getpass.getpass("Password: ")
browser = osm_auth(senderlogin, senderpass)
rssurl = RSSURL.format(urllib.parse.quote(config.get('main', 'region')))
if not options.u:
r = requests.get(rssurl)
r.raise_for_status()
if r.status_code != 200:
raise Exception('Error getting %s' % rssurl)
soup = bs4.BeautifulSoup(r.text, 'html.parser')
userurls = [x.get_text() for x in soup.find_all('id')]
userurls.reverse()
statusfile = config.get('Files', 'statusfile')
logging.debug('Status file: %s', statusfile)
try:
lastsent = open(statusfile, encoding='utf-8').read().strip()
except IOError:
lastsent = ''
try:
ind = userurls.index(lastsent)
except ValueError:
ind = 0
logging.debug('we left off at %s', lastsent)
else:
userurls = ['xxx/%s' % options.u[0]]
ind = -1
subject = config.get('Messages', 'subject')
mainmessage = config.get('Messages', 'mainmessage')
nosourcemessage = config.get('Messages', 'nosourcemessage')
nocommentmessage = config.get('Messages', 'nocommentmessage')
ideditormessage = config.get('Messages', 'ideditormessage')
for user in userurls[ind+1:]:
rcpt = user.split('/')[-1]
rcpt_quoted = urllib.parse.quote(rcpt)
message = mainmessage.replace('%', ' ').replace('<nick>', rcpt)
r = requests.get('https://openstreetmap.org/user/%s/history' % rcpt_quoted)
soup = bs4.BeautifulSoup(r.text, 'html.parser')
changeset = soup.findAll('a')[0]['href']
logging.debug("Last changeset id: %s", changeset)
r = requests.get('https://openstreetmap.org/api/0.6%s' % changeset)
soup = bs4.BeautifulSoup(r.text, 'html.parser')
tags = {k['k']: k['v'] for k in soup.findAll('tag')}
logging.debug('changeset tags: %s', tags)
if not tags.get('source'):
logging.debug('no source tag used for changeset')
# only add text if user is not using iD
if 'iD' not in tags.get('created_by'):
message += '\n\n' + nosourcemessage
if not tags.get('comment'):
logging.debug('no comment tag used for changeset')
message += '\n\n' + nocommentmessage
if 'iD' in tags.get('created_by'):
logging.debug('iD editor detected')
message += '\n\n' + ideditormessage
logging.debug('sending message to user %s', rcpt)
if not options.nosend:
osm_send(browser, subject, message, rcpt_quoted)
else:
logging.debug('NOT sending (because you said so) the message to user %s', rcpt)
if not options.u:
with open(statusfile, 'w', encoding='utf-8') as f:
f.write(user)