summaryrefslogtreecommitdiff
path: root/app/links/retriever.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/links/retriever.py')
-rw-r--r--app/links/retriever.py104
1 files changed, 32 insertions, 72 deletions
diff --git a/app/links/retriever.py b/app/links/retriever.py
index bcbc8fa..9e7aae5 100644
--- a/app/links/retriever.py
+++ b/app/links/retriever.py
@@ -5,15 +5,33 @@ from django.template.defaultfilters import striptags
from django.core.mail import EmailMessage
from django.conf import settings
-
-from utils.strutils import safestr,unquotehtml
from links.models import Link
-from utils import pinboard
+# https://github.com/mgan59/python-pinboard/
+import pinboard
+
+
+def safestr(s):
+ """
+ Safely corerce *anything* to a string. If the object can't be str'd, an
+ empty string will be returned.
+
+ You can (and I do) use this for really crappy unicode handling, but it's
+ a bit like killing a mosquito with a bazooka.
+ """
+ if s is None:
+ return ""
+ if isinstance(s, unicode):
+ return s.encode('ascii', 'xmlcharrefreplace')
+ else:
+ try:
+ return str(s)
+ except:
+ return ""
+
def sync_pinboard_links():
"""
sync bookmarks from my pinboard account
-
dependancies: python-pinboard https://github.com/mgan59/python-pinboard/
"""
p = pinboard.open(settings.PIN_USER, settings.PIN_PASS)
@@ -22,17 +40,17 @@ def sync_pinboard_links():
for link in links:
try:
#check to see if link exists
- row = Link.objects.get(link_id=safestr(link['hash']))
+ 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
- )
+ 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
@@ -40,72 +58,14 @@ def sync_pinboard_links():
l.tags.add(t)
email_link(l)
-'''
-def sync_delicious_links(*args, **kwargs):
- b = delicious.get_all(settings.DELICIOUS_USER, settings.DELICIOUS_PASS)
- dupe = False
- for post in b:
- taglist = []
- try:
- row = Link.objects.get(magnolia_id=safestr(post['hash']))
- # If the row exists already, set the dupe flag
- dupe = True
- except ObjectDoesNotExist:
- #f = copy_file(safestr(post.findtext('screenshot')), safestr(info['id']))
- #fake the image since delicious doesn't offer them
- local_image_url = "%s/%s.jpg" %(safestr(datetime.datetime.today().strftime("%b").lower()), safestr(post['hash']))
- tags = str(post['tag']).split(" ")
- for tag in tags:
- taglist.append(tag)
- for tag in taglist:
- if tag == '2lux':
- status = 1
- break
- else:
- status = 0
- descr = markdown.markdown(unquotehtml(safestr(post['extended'])), safe_mode = False)
- l, created = Link.objects.get_or_create(
- title = post['description'],
- magnolia_id = safestr(post['hash']),
- url = safestr(post['href']),
- description = descr,
- screen_url = local_image_url,
- #fake the rating since delicious doesn't offer such things
- rating = "3",
- pub_date = datetime.datetime.strptime(post['time'], "%Y-%m-%dT%H:%M:%SZ"),
- status = status,
- enable_comments = True,
- tags = ", ".join(t for t in taglist if t != "2lux")
- )
-
- email_link(l)
- if l.status == 1:
- pass
- #post_to_tumblr(l)
- #send_to_deliciousfb(l)
- if(dupe):
- break
-
-
-'''
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)
+ 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()
-
-'''
-def send_to_delicious(link):
- del_tags = ''
- tags = link.tags.split(',')
- for tag in tags:
- del_tags += tag.strip().replace(' ','_')+' '
- delicious.add(settings.DELICIOUS_USER, settings.DELICIOUS_PASS, link.url, link.title, tags = del_tags, extended = striptags(link.description), dt =safestr(link.pub_date), replace="no")
-
-'''