<commit_before>import urllib2

from flask import current_app

from shared import db
from json import dumps
from sqlalchemy.dialects.mysql import INTEGER
from datetime import datetime
from config import google_api_key
from models import Subscription, Message

gcm_url = 'https://android.googleapis.com/gcm/send'


class Gcm(db.Model):
    id = db.Column(INTEGER(unsigned=True), primary_key=True)
    uuid = db.Column(db.VARCHAR(40), nullable=False)
    gcmid = db.Column(db.TEXT, nullable=False)
    timestamp_created = db.Column(db.TIMESTAMP, default=datetime.utcnow)

    def __init__(self, device, gcmid):
        self.uuid = device
        self.gcmid = gcmid

    def __repr__(self):
        return '<Gcm %r>' % self.uuid

    def as_dict(self):
        data = {
            "uuid": self.service.as_dict(),
            "gcm_registration_id": self.gcmId,
            "timestamp": int(self.timestamp_created.strftime('%s')),
        }
        return data

    @staticmethod
    def send_message(message):
        """

        :type message: Message
        """
        subscriptions = Subscription.query.filter_by(service=message.service).all()
        if len(subscriptions) == 0:
            return 0
        gcm_filter = Gcm.query.filter(Gcm.uuid.in_([l.device for l in subscriptions])).all()

        devices = [r.gcmid for r in gcm_filter]

        if len(devices) > 0:
            data = {"message": message.as_dict(), "encrypted": False}
            Gcm.gcm_send(devices, data)

        if len(gcm_filter) > 0:
            uuids = [g.uuid for g in gcm_filter]
            gcm_subscriptions = Subscription.query.filter_by(service=message.service).filter(Subscription.device.in_(uuids)).all()
            last_message = Message.query.order_by(Message.id.desc()).first()
            for l in gcm_subscriptions:
                l.timestamp_checked = datetime.utcnow()
                l.last_read = last_message.id if last_message else 0
            db.session.commit()
        return len(gcm_filter)

    @staticmethod
    def gcm_send(ids, data):
        data = {
            "registration_ids": ids,
            "data": data,
        }

        if current_app.config['TESTING'] is True:
            current_app.config['TESTING_GCM'](data)
        else:
            headers = {
                'Authorization': 'key=%s' % google_api_key,
                'Content-Type': 'application/json',
            }
            req = urllib2.Request('https://android.googleapis.com/gcm/send', dumps(data), headers)
            urllib2.urlopen(req).read()<commit_msg>Replace urllib with requests and cleanup GCM code<commit_after>import sys
from flask import current_app
from shared import db
from sqlalchemy.dialects.mysql import INTEGER
from datetime import datetime
from config import google_api_key
from models import Subscription, Message
import requests

gcm_url = 'https://android.googleapis.com/gcm/send'


class Gcm(db.Model):
    id = db.Column(INTEGER(unsigned=True), primary_key=True)
    uuid = db.Column(db.VARCHAR(40), nullable=False)
    gcmid = db.Column(db.TEXT, nullable=False)
    timestamp_created = db.Column(db.TIMESTAMP, default=datetime.utcnow)

    def __init__(self, device, gcmid):
        self.uuid = device
        self.gcmid = gcmid

    def __repr__(self):
        return '<Gcm %r>' % self.uuid

    def as_dict(self):
        data = {
            "uuid": self.service.as_dict(),
            "gcm_registration_id": self.gcmId,
            "timestamp": int(self.timestamp_created.strftime('%s')),
        }
        return data

    @staticmethod
    def send_message(message):
        """

        :type message: Message
        """
        subscriptions = Subscription.query.filter_by(service=message.service).all()
        if len(subscriptions) == 0:
            return 0
        gcm_devices = Gcm.query.filter(Gcm.uuid.in_([l.device for l in subscriptions])).all()

        if len(gcm_devices) > 0:
            data = dict(message=message.as_dict(), encrypted=False)
            Gcm.gcm_send([r.gcmid for r in gcm_devices], data)

        if len(gcm_devices) > 0:
            uuids = [g.uuid for g in gcm_devices]
            gcm_subscriptions = Subscription.query.filter_by(service=message.service).filter(Subscription.device.in_(uuids)).all()
            last_message = Message.query.order_by(Message.id.desc()).first()
            for l in gcm_subscriptions:
                l.timestamp_checked = datetime.utcnow()
                l.last_read = last_message.id if last_message else 0
            db.session.commit()
        return len(gcm_devices)

    @staticmethod
    def gcm_send(ids, data):
        url = 'https://android.googleapis.com/gcm/send'
        headers = dict(Authorization='key={}'.format(google_api_key))
        data = dict(registration_ids=ids, data=data)

        if current_app.config['TESTING'] is True:
            current_app.config['TESTING_GCM'](data)
        else:
            requests.post(url, json=data, headers=headers)
{'fraction_non_alphanumeric': 0.11410204901566895, 'fraction_numerical': 0.0034150261149055844, 'mean_word_length': 2.5162429378531073, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '599544', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1515, 'n_words': 316}