summaryrefslogtreecommitdiff
path: root/app/links/retriever.py
blob: f80782096e6f25d1115ca789243ee71aec5ba9ee (plain)
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
import datetime

from django.core.exceptions import ObjectDoesNotExist
from django.template.defaultfilters import striptags
from django.core.mail import EmailMessage
from django.conf import settings

from links.models import Link
# https://github.com/mgan59/python-pinboard/
#import pinboard

import requests
import json

def sync_pinboard_links():
    PB_URL = "https://api.pinboard.in/v1/posts/all?results=150&format=json"
    r = requests.get(PB_URL, auth=((settings.PIN_USER, settings.PIN_PASS)))
    links = json.loads(r.text)
    print(links)
    for link in links:
        try:
            #check to see if link exists
            row = Link.objects.get(link_id=link['hash'])
            print("already have" + row.title)
        except ObjectDoesNotExist:
            l, created = Link.objects.get_or_create(
                title=link['description'],
                link_id=link['hash'],
                url=link['href'],
                description=link['extended'],
                rating="3",
                pub_date=datetime.datetime.strptime(link['time'], "%Y-%m-%dT%H:%M:%SZ"),
                status=0
            )
            print(l.title)
            if created:
                print(l.title)
                for t in link['tags']:
                    l.tags.add(t)


"""
def sync_pinboard_links_old():
    sync bookmarks from my pinboard account
    dependancies: python-pinboard https://github.com/mgan59/python-pinboard/
    p = pinboard.open(settings.PIN_USER, settings.PIN_PASS)
    dupe = False
    links = p.posts(count=30)
    for link in links:
        try:
            #check to see if link exists
            row = Link.objects.get(link_id=safestr(link['hash']))
        except ObjectDoesNotExist:
            l, created = Link.objects.get_or_create(
                title=link['description'],
                link_id=safestr(link['hash']),
                url=safestr(link['href']),
                description=safestr(link['extended']),
                rating="3",
                pub_date=datetime.datetime.strptime(link['time'], "%Y-%m-%dT%H:%M:%SZ"),
                status=0
            )
            print l.title
            if created:
                print l.title
                for t in link['tags']:
                    l.tags.add(t)
            email_link(l)

"""

def email_link(link):
    """
    Sends an imported link to Gmail (never hurts to have backups)
    """
    subject = link.title
    body = "%s\n\n%s\n\n\nvisit site:%s\n" % (link.title, link.description, link.url)
    msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['luxagraf+links@gmail.com'])
    msg.send()
    msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['sng+links@luxagraf.net'])
    msg.send()