<commit_before>from pymongo import MongoClient
from bson.json_util import dumps
from bson.objectid import ObjectId
import datetime

import requests
import json

AUTHENTICATION_MODULE_HOST = 'http://192.168.127.12:'
AUTHENTICATION_MODULE_PORT = '9999'

def send_notification_to_authentication(user_id, message_title, message_body):
    headers = {'Content-type': 'application/x-www-form-urlencoded'}
    url = AUTHENTICATION_MODULE_HOST + AUTHENTICATION_MODULE_PORT + '/send_notification'
    data = {'user_id': user_id, 'message_title': message_title, 'message_body': message_body}
    requests.post(url, headers = headers, data = data)

def create_notification(user_id, partial_trips, icon_id):
    notification = {}
    notification['userID'] = user_id
    trip = user_trips.find_one({'_id': partial_trips[0]})
    line = trip['line']
    start_time = trip['startTime']
    notification['partialTrips'] = partial_trips

    notification['text'] = "Alert: Bus {} is arriving at {}".format(line, 
                            start_time)
    notification['time'] = datetime.datetime.now()
    notification['iconID'] = icon_id
    print notification
    return notification

def main():
    global user
    global mongo_client
    global db
    global bookings_collection
    global notifications_collection
    global user_trips
    mongo_client = MongoClient('192.168.127.12', 27017)
    db = mongo_client.monad1
    bookings_collection = db.BookedTrip
    user_trips = db.UserTrip
    notifications_collection = db.Notifications
    now = datetime.datetime.now()
    for booking in bookings_collection.find({'userID' : 1}):
        list_trips = booking['partialTrips']
        trip = user_trips.find_one({'_id' : list_trips[0]})
        user_id = trip['userID']
        start_time = trip['startTime']
        dif = datetime.timedelta(minutes = 30)
        if start_time - now < dif and start_time > now:
            notification = create_notification(user_id, list_trips, 3)
            try: 
                notifications_collection.insert_one(notification)
            except pymongo.errors.PyMongoError as e:
                print e
                
            send_notification_to_authentication(user_id, 'MoNAD', notification['text'])

if __name__ == '__main__':
    main()<commit_msg>Fixing some code for notifications regarding 30 min to departure<commit_after>from pymongo import MongoClient
from bson.json_util import dumps
from bson.objectid import ObjectId
import datetime

import requests
import json

AUTHENTICATION_MODULE_HOST = 'http://192.168.127.12:'
AUTHENTICATION_MODULE_PORT = '9999'

def send_notification_to_authentication(user_id, message_title, message_body):
    headers = {'Content-type': 'application/x-www-form-urlencoded'}
    url = AUTHENTICATION_MODULE_HOST + AUTHENTICATION_MODULE_PORT + '/send_notification'
    data = {'user_id': user_id, 'message_title': message_title, 'message_body': message_body}

def parse_user_trip(user_trips_list, user_trip):
    if 'next' in user_trip.keys():
        user_trips_list.append(user_trip)
        return parse_user_trip(user_trips_list,
                               user_trips.find_one({'_id' : ObjectId(user_trip['next'])}))
    else:
        user_trips_list.append(user_trip)
        return user_trips_list
    requests.post(url, headers = headers, data = data)

def create_notification(user_id, partial_trips, icon_id):
    notification = {}
    notification['userID'] = user_id
    trip = user_trips.find_one({'_id': partial_trips[0]})
    line = trip['line']
    start_time = trip['startTime']
    user_trip = user_trips.find_one({'_id' : partial_trips[0]})
    # print user_trip
    user_trips_list = parse_user_trip(list(), user_trip)
    notification['partialTrips'] = user_trips_list
    notification['text'] = "Alert: Bus {} is arriving at {}".format(line, 
                            start_time)
    notification['time'] = datetime.datetime.now()
    notification['iconID'] = icon_id
    print notification['userID'] , start_time 
    return notification 

def main():
    global user
    global mongo_client
    global db
    global bookings_collection
    global notifications_collection
    global user_trips
    mongo_client = MongoClient('192.168.127.12', 27017)
    db = mongo_client.monad1
    bookings_collection = db.BookedTrip
    user_trips = db.UserTrip
    notifications_collection = db.Notifications
    now = datetime.datetime.now()
    for booking in bookings_collection.find({'userID' : 1}):
        list_trips = booking['partialTrips']
        trip = user_trips.find_one({'_id' : list_trips[0]})
        user_id = trip['userID']
        start_time = trip['startTime']
        dif = datetime.timedelta(minutes = 30)
        if start_time - now < dif and start_time > now:
            notification = create_notification(user_id, list_trips, 3)
            try: 
                notifications_collection.insert_one(notification)
            except pymongo.errors.PyMongoError as e:
                print e
                
            send_notification_to_authentication(user_id, 'MoNAD', notification['text'])
            

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.10007742934572203, 'fraction_numerical': 0.015292295780100659, 'mean_word_length': 3.3862478777589136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2352478', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1550, 'n_words': 327}