Exemple #1
0
def _import_service(url, title, cls='webfeed'):
    api = 'webfeed'

    if 'flickr.com' in url:
        m = re.search(
            r'flickr.com/services/feeds/photos_public\.gne\?id=([0-9@A-Z]+)', url)
        if m:
            url = m.groups()[0]
        url = url.replace('format=atom', 'format=rss_200')
        api = 'flickr'
        cls = 'photos'
    elif 'twitter.com' in url:
        m = re.search(r'twitter.com/1/statuses/user_timeline/(\w+)\.', url)
        if m:
            url = m.groups()[0]
            api = 'twitter'
            cls = 'sms'
    elif 'vimeo.com' in url:
        m = re.search(r'vimeo.com/([\w/]+)/\w+/rss', url)
        if m:
            url = m.groups()[0]
            url = url.replace('channels/', 'channel/')
            url = url.replace('groups/', 'group/')
            api = 'vimeo'
            cls = 'videos'
    elif 'youtube.com' in url:
        m = re.search(r'gdata.youtube.com/feeds/api/users/(\w+)', url)
        if m:
            url = m.groups()[0]
        api = 'youtube'
        cls = 'videos'
    elif 'yelp.com/syndicate' in url:
        api = 'yelp'
        cls = 'reviews'

    try:
        try:
            service = Service.objects.get(api=api, url=url)
        except Service.DoesNotExist:
            if api in ('vimeo', 'webfeed', 'yelp', 'youtube'):
                display = 'both'
            else:
                display = 'content'
            service = Service(api=api, cls=cls, url=url, name=title,
                              display=display)
            service.save()
    except:
        pass
Exemple #2
0
def api(request, **args):
    authed = request.user.is_authenticated() and request.user.is_staff
    if not authed:
        return HttpResponseForbidden()

    cmd = args.get('cmd', '')

    method = request.POST.get('method', 'get')
    id = request.POST.get('id', None)

    # Add/edit services
    if cmd == 'service':
        s = {
            'api': request.POST.get('api', ''),
            'name': request.POST.get('name', ''),
            'cls': request.POST.get('cls', ''),
            'url': request.POST.get('url', ''),
            'display': request.POST.get('display', 'content'),
            'public': bool(request.POST.get('public', False)),
            'home': bool(request.POST.get('home', False)),
            'active': bool(request.POST.get('active', False)),
        }
        miss = {}

        # Data validation
        if method == 'post':
            if not s['name']:
                miss['name'] = True
                method = 'get'
            if s['api'] != 'selfposts' and not s['url'] \
               and request.POST.get('timeline', 'user') == 'user':
                miss['url'] = True
                method = 'get'

        # Special cases, predefined
        if s['api'] in ('delicious', 'digg', 'greader', 'lastfm',
                        'stumbleupon', 'yelp'):
            s['display'] = 'both'

        # Save
        if method == 'post':
            try:
                try:
                    if not id:
                        raise Service.DoesNotExist
                    srv = Service.objects.get(id=id)
                except Service.DoesNotExist:
                    srv = Service()
                for k, v in s.items():
                    setattr(srv, k, v)
            except:
                pass

            try:
                basic_user = request.POST.get('basic_user', None)
                basic_pass = request.POST.get('basic_pass', None)
                access_token = request.POST.get('access_token', None)

                auth = request.POST.get('auth', 'none')
                if auth == 'basic' and basic_user and basic_pass:
                    srv.creds = basic_user + ':' + basic_pass
                elif auth == 'oauth':
                    srv.creds = auth
                elif access_token:
                    srv.creds = access_token
                elif auth == 'none':
                    srv.creds = ''

                s['need_import'] = True if not srv.id else False
                srv.save()
                id = srv.id
            except:
                pass

        # Get
        if id:
            try:
                srv = Service.objects.get(id=id)
                if not len(miss):
                    s.update({
                        'id': srv.id,
                        'api': srv.api,
                        'name': srv.name,
                        'cls': srv.cls,
                        'url': srv.url,
                        'creds': srv.creds,
                        'display': srv.display,
                        'public': srv.public,
                        'home': srv.home,
                        'active': srv.active,
                    })
                else:
                    s['id'] = srv.id
                s['delete'] = _('delete')
            except Service.DoesNotExist:
                pass
        else:
            s['home'] = True
            s['active'] = True

        if not 'creds' in s:
            s['creds'] = ''

        # Setup fields
        s['fields'] = [
            {'type': 'text', 'name': 'name',
             'value': s['name'], 'label': _('Short name'),
             'miss': miss.get('name', False)},
            {'type': 'text', 'name': 'cls',
             'value': s['cls'], 'label': _('Class name'),
             'hint': _('Any name for the service classification; a category.')}
        ]

        if s['api'] == 'webfeed':
            s['fields'].append({'type': 'text', 'name': 'url',
                                'value': s['url'], 'label': _('URL'),
                                'miss': miss.get('url', False)})

        elif s['api'] in ('fb', 'friendfeed', 'twitter', 'identica'):
            v = 'user' if s['url'] else 'home'
            s['fields'].append({'type': 'select', 'name': 'timeline',
                                'options': (('user', _('User timeline')),
                                            ('home', _('Home timeline'))),
                                'value': v, 'label': _('Timeline')})
            s['fields'].append({'type': 'text', 'name': 'url',
                                'value': s['url'], 'label': _('ID/Username'),
                                'deps': {'timeline': 'user'}})

        elif s['api'] != 'selfposts':
            s['fields'].append({'type': 'text', 'name': 'url',
                                'value': s['url'], 'label': _('ID/Username'),
                                'miss': miss.get('url', False)})

        if s['api'] in ('webfeed', 'friendfeed', 'identica', 'twitter'):
            basic_user = ''
            if s['creds'] == 'oauth':
                v = 'oauth'
            elif s['creds']:
                v = 'basic'
                basic_user = s['creds'].split(':', 1)[0]
            else:
                v = 'none'

            s['fields'].append({'type': 'select', 'name': 'auth',
                                'options': (('none', _('none')),
                                            ('basic', _('Basic')),
                                            ('oauth', _('OAuth'))),
                                'value': v, 'label': _('Authorization')})

            if 'id' in s:
                s['fields'].append({'type': 'link', 'name': 'oauth_conf',
                                    'value': _('configure access'),
                                    'href': '#', 'label': '',
                                    'deps': {'auth': 'oauth'}})

            s['fields'].append({'type': 'text', 'name': 'basic_user',
                                'value': basic_user,
                                'label': _('Basic username'),
                                'deps': {'auth': 'basic'}})
            s['fields'].append({'type': 'password', 'name': 'basic_pass',
                                'value': '', 'label': _('Basic password'),
                                'deps': {'auth': 'basic'}})

        if s['api'] == 'fb':
            s['fields'].append({'type': 'text', 'name': 'access_token',
                                'value': s['creds'],
                                'label': _('Access token')})
            s['need_fb_accesstoken'] = _('get')

        if s['api'] in ('webfeed', 'flickr', 'youtube', 'vimeo'):
            s['fields'].append({'type': 'select', 'name': 'display',
                                'options': (('both', _('Title and Contents')),
                                            ('content', _('Contents only')),
                                            ('title', _('Title only'))),
                                'value': s['display'],
                                'label': _("Display entries'")})

        s['fields'].append({'type': 'checkbox', 'name': 'public',
                            'checked': s['public'], 'label': _('Public'),
                            'hint': _('Public services are visible to anyone.')})

        s['fields'].append({'type': 'checkbox', 'name': 'home',
                            'checked': s['home'], 'label': _('Home'),
                            'hint': _('If unchecked, this stream will be still active, but hidden and thus visible only via custom lists.')})

        if s['api'] != 'selfposts':
            s['fields'].append({'type': 'checkbox', 'name': 'active',
                                'checked': s['active'], 'label': _('Active'),
                                'hint': _('If not active, this service will not be further updated.')})

        if 'creds' in s:
            del s['creds']

        s['action'] = request.build_absolute_uri()
        s['save'] = _('Save')
        s['cancel'] = _('Cancel')

        # print(json.dumps(s, indent=2))
        return HttpResponse(json.dumps(s), content_type='application/json')

    # Import
    elif cmd == 'import' and id:
        try:
            service = Service.objects.get(id=id)
            mod = __import__('glifestream.apis.%s' %
                             service.api, {}, {}, ['API'])
            mod_api = getattr(mod, 'API')
            api = mod_api(service, False, False)
            api.run()
        except:
            pass

    return HttpResponse()