Beispiel #1
0
"""

from __future__ import absolute_import

from skrode import schema
from skrode.personas import merge_left
from skrode.services import mk_service, mk_insert_user
from skrode.twitter import insert_user as twitter_insert_user, insert_twitter
from skrode.github import insert_user as gh_insert_user
from skrode.reddit import insert_user as reddit_insert_user

from lobsters import User
from twitter.error import TwitterError
from arrow import utcnow as now

insert_lobsters = mk_service("Lobsters", ["http://lobste.rs"])


def lobsters_external_id(user_or_id):
    if isinstance(user_or_id, User):
        return lobsters_external_id(user_or_id.name)
    else:
        return "lobsters+user:%s" % user_or_id


_insert_user = mk_insert_user(insert_lobsters, lobsters_external_id)


def insert_user(session, twitter_api, user: User, persona=None, when=None):
    when = when or now()
Beispiel #2
0
                           Post, PostDistribution, PostRelationship, Service,
                           get_or_create)
from skrode.services import mk_service

_tw_user_pattern = re.compile(
    "(https?://)twitter.com/(?P<username>[^/?]+)(/.+)?(&.+)?")
_tw_datetime_pattern = "%a %b %d %H:%M:%S +0000 %Y"
_tw_url_pattern = re.compile(
    "https://twitter.com/((?P<username>[^/]+)|(i/web))/status/(?P<id>\d+)")


def twitter_external_user_id(fk):
    return "twitter+user:{}".format(fk)


insert_twitter = mk_service("Twitter", ["http://twitter.com"])


def insert_handle(session, user: User, persona=None):
    """
  Insert a Twitter Handle, creating a Persona for it if there isn't one.


  If the Handle is already known, just linked to another Persona, steal it.
  """

    external_id = twitter_external_user_id(user.id)
    handle = session.query(Account).filter_by(external_id=external_id).first()
    if not handle:
        if not persona:
            persona = Persona()
Beispiel #3
0
"""
Helpers for dealing with github.com and gist.github.io
"""

from __future__ import absolute_import

import re

from skrode.schema import (Persona, Human, Account, Name, AccountRelationship,
                           Service, get_or_create)
from skrode.services import mk_service, mk_insert_user
from skrode.personas import merge_left

from arrow import utcnow as now

insert_reddit = mk_service("Reddit", ["http://reddit.com"])

_reddit_user_pattern = re.compile(
    r"(https?://)?(reddit.com)/u/(?P<username>[^/?]+)(/.+)?(&.+)?")


def external_id(username):
    m = re.match(_reddit_user_pattern, username)
    if m:
        username = m.group("username")

    return "reddit+user:%s" % username


insert_user = mk_insert_user(insert_reddit, external_id)
Beispiel #4
0
"""
A hackernews backend.
"""

from __future__ import absolute_import

from skrode.services import mk_service, mk_insert_user

insert_hn = mk_service("Hackernews", ["http://news.ycombinator.com"])


def hn_external_id(id):
    return "hackernews+user:%s" % id


insert_user = mk_insert_user(insert_hn, hn_external_id)
Beispiel #5
0
"""
A BBDB module for trying to find keybase identities related to a profile
"""

from keybase import Api, Proof, NoSuchUserException
from skrode import schema
from skrode.services import mk_service, normalize_url, mk_insert_user
from skrode.twitter import insert_twitter, insert_user as twitter_insert_user
from skrode.personas import merge_left


insert_keybase = mk_service("Keybase", ["http://keybase.io"])

def keybase_external_id(user_id):
  return "keybase+user:{}".format(user_id)


_insert_user = mk_insert_user(insert_keybase, keybase_external_id)


def insert_user(session, kb_user, persona=None, when=None, twitter_api=None):
  kb_account = _insert_user(session, kb_user.id,
                            persona=persona, when=when)

  name = schema.get_or_create(session, schema.Name,
                              name=kb_user.username,
                              account=kb_account)

  for proof in kb_user.proofs:
    if proof.proof_type == "generic_web_site":
      # FIXME: do something with this.
Beispiel #6
0
Helpers for dealing with github.com and gist.github.io
"""

from __future__ import absolute_import

import re

from skrode.schema import (Persona, Human, Account, Name, AccountRelationship,
                           Service, get_or_create)
from skrode.services import mk_service, mk_insert_user
from skrode.personas import merge_left

from arrow import utcnow as now

insert_github = mk_service(
    "Github",
    ["http://github.com", "http://gist.github.io", "http://github.io"])

_gh_user_pattern = re.compile(
    r"(https?://)?((gist\.)?github\.(io|com))/(?P<username>[^/?]+)(/.+)?(&.+)?"
)


def external_id(username):
    m = re.match(_gh_user_pattern, username)
    if m:
        username = m.group("username")

    return "github+user:%s" % username

Beispiel #7
0
"""
Helpers for working with telephone numbers.
"""

from __future__ import absolute_import

from skrode import schema
from skrode.services import mk_service

from phonenumbers import (format_number as format_phonenumber, parse as
                          parse_phonenumber, PhoneNumberFormat)

insert_phone_service = mk_service("telephone", [])


def insert_phone_number(session, persona, number):
    """
  Parses a phone number, inserting a new Account record for the given persona's telephone number.
  """

    parsed_number = format_phonenumber(parse_phonenumber(number),
                                       PhoneNumberFormat.RFC3966)
    phone_account = schema.Account(external_id=parsed_number,
                                   persona=persona,
                                   service=insert_phone_service(session))
    session.add(phone_account)
    session.commit()
    return phone_account
Beispiel #8
0
"""
Helpers for dealing with facebook.com
"""

from __future__ import absolute_import

import re

from skrode.schema import (Persona, Human, Account, Name, AccountRelationship,
                           Service, get_or_create)
from skrode.services import mk_service, mk_insert_user
from skrode.personas import merge_left

from arrow import utcnow as now

insert_facebook = mk_service("Facebook",
                             ["http://facebook.com", "http://messenger.com"])

_fb_user_pattern = re.compile(
    r"(https?://)?(facebook.com)/(?P<username>[^/?]+)(/.+)?(&.+)?")


def external_id(username):
    m = re.match(_fb_user_pattern, username)
    if m:
        username = m.group("username")

    return "facebook+user:%s" % username


insert_user = mk_insert_user(insert_facebook, external_id)
Beispiel #9
0
"""
Helpers for working with (merging/splitting) personas.
"""

from sqlalchemy import asc, func, inspect, join, or_, select, union

from skrode import schema
from skrode.schema import get_or_create
from skrode.services import mk_insert_user, mk_service
from skrode.telephones import insert_phone_number

null_service = mk_service("namesvc", [])


def _nullsvc_fk(id):
    return "namesvc+user:%s" % id


insert_user = mk_insert_user(null_service, _nullsvc_fk)


def insert_name(session, persona, name):
    """Add a name to the given persona by linking it through a null service."""

    nullsvc = null_service(session)
    nullact = session.query(schema.Account)\
                     .filter_by(service=nullsvc,
                                persona=persona)\
                     .first()
    if not nullact:
        nullact = schema.Account(service=nullsvc,