import datetime from random import randint from django.core.exceptions import ObjectDoesNotExist from django.template.defaultfilters import striptags from django.core.mail import EmailMessage from django.conf import settings from django.core.mail import send_mail from links.models import Link import requests import json def sync_pinboard_links(): PB_URL = "https://api.pinboard.in/v1/posts/all?results=70&format=json" r = requests.get(PB_URL, auth=((settings.PIN_USER, settings.PIN_PASS))) links = json.loads(r.text) for link in links: try: # check to see if link exists row = Link.objects.get(url=link['href']) print("already have" + row.title) except ObjectDoesNotExist: md = get_markdown(link['href']) l, created = Link.objects.get_or_create( title=link['description'], url=link['href'], description=link['extended'], pub_date=datetime.datetime.strptime(link['time'], "%Y-%m-%dT%H:%M:%SZ"), status=0, body_markdown=md['markdown'] ) if created: print(l.title) for t in link['tags'].split(" "): l.tags.add(t) email_link(l) def get_markdown(source): url = "http://heckyesmarkdown.com/go/?read=1&preview=0&showframe=0&output=json&u=%s" % (source) r = requests.get(url, timeout=15.001) data = r.json() return data def email_link(link): """ Sends an imported link to Gmail (never hurts to have backups) """ subject = "[link] %s" % 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', ['sng@luxagraf.net']) send_mail( subject, body, 'sng@luxagraf.net', ['sng@luxagraf.net'] ) def random_link(): total = Link.objects.all().count() pk = randint(1, total) try: link = Link.objects.get(pk=pk) except: try: link = Link.objects.get(pk=pk+1) except: link = Link.objects.get(pk=pk+91) subject = "today's link: %s" % link.title body = "%s\n\n%s\n\n\nvisit site:%s\n\n\ndelete link: https://live.luxagraf.net/admin/links/link/%s/" % (link.title, link.description, link.url, link.pk) msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['sng@luxagraf.net']) msg.send()