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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
import time, datetime, urllib
from django.core.exceptions import ObjectDoesNotExist
from django.template.defaultfilters import slugify,striptags
from django.core.mail import EmailMessage
from django.utils.encoding import smart_unicode
from strutils import safestr,unquotehtml
from APIClients import MagnoliaClient
import pydelicious as delicious
import markdown2 as markdown
from links.models import Link
from django.conf import settings
def flickr_datetime_to_datetime(fdt):
from datetime import datetime
from time import strptime
date_parts = strptime(fdt, '%Y-%m-%dT%H:%M:%S%Z')
return datetime(*date_parts[0:6])
def sync_magnolia_links(*args, **kwargs):
#Number of links to retrieve at one time
get_num = 15
BASE_PATH = 'http://ma.gnolia.com/api/rest/1/'
client = MagnoliaClient(BASE_PATH, settings.MAGNOLIA_API_KEY)
data = client.bookmarks_find(person="luxagraf",limit=get_num)
dupe = False
for post in data.findall('bookmarks/bookmark'):
taglist = []
info = dict((k, smart_unicode(post.get(k))) for k in post.keys())
try:
row = Link.objects.get(magnolia_id=safestr(info['id']))
# If the row exists already, set the dupe flag
dupe = True
except ObjectDoesNotExist:
f = copy_file(safestr(post.findtext('screenshot')), safestr(info['id']))
local_image_url = "%s/%s.jpg" %(safestr(datetime.datetime.today().strftime("%b").lower()), safestr(info['id']))
for t in post.findall('tags/tag'):
tag = dict((k, smart_unicode(t.get(k))) for k in t.keys())
taglist.append(tag['name'])
print tag['name']
for tag in taglist:
if tag == '2lux':
status = 1
break
else:
status = 0
descr = markdown.markdown(unquotehtml(safestr(post.findtext('description'))), safe_mode = False)
l, created = Link.objects.get_or_create(
title = post.findtext('title'),
magnolia_id = safestr(info['id']),
url = safestr(post.findtext('url')),
description = descr,
screen_url = local_image_url,
rating = safestr(info['rating']),
pub_date = datetime.datetime(*(time.strptime(str(info['created'][:-6]), '%Y-%m-%dT%H:%M:%S')[0:6])),
status = status,
enable_comments = True,
tags = ", ".join(t for t in taglist if t != "2lux")
)
email_link(l)
send_to_delicious(l)
if l.status == 1:
post_to_tumblr(l)
send_to_deliciousfb(l)
if(dupe):
break
def sync_delicious_links(*args, **kwargs):
b = delicious.get(settings.DELICIOUS_USER, settings.DELICIOUS_PASS)
dupe = False
for post in b['posts']:
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:
post_to_tumblr(l)
send_to_deliciousfb(l)
if(dupe):
break
"""
b, created = Link.objects.get_or_create(
url = info['href'],
description = info['extended'],
tags = info.get('tag', ''),
date = parsedate(info['time']),
title = info['description']
)
for b in bookmarks.bookmarks.bookmark:
try:
row = Link.objects.get(magnolia_id=safestr(b.id))
# If the row exists already, set the dupe flag
print b.title.PCDATA
dupe = True
except ObjectDoesNotExist:
tags=", ".join(t.name for t in b.tags.tag if t.name != "2lux")
# grab the photo (local copies are good, should I ever change bookmark services)
local_image_url = "%s/%s.jpg" %(safestr(datetime.datetime.today().strftime("%b").lower()), safestr(b.id))
f = copy_file(safestr(b.screenshot.PCDATA), safestr(b.id))
# set the privacy flag (default to false to be on the safe side)
public = 0
if safestr(b.private) == 'false': public=1
# Create and save a new link obj
l = Link.objects.create(
title = safestr(b.title.PCDATA),
magnolia_id = safestr(b.id),
url = safestr(b.url.PCDATA),
description = unquotehtml(safestr(b.description.PCDATA)),
screen_url = local_image_url,
rating = safestr(b.rating),
pub_date = datetime.datetime(*(time.strptime(str(b.created[:-6]), '%Y-%m-%dT%H:%M:%S')[0:6])),
status = public,
enable_comments = True,
)
email_link(l)
send_to_delicious(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\non ma.gnolia: http://ma.gnolia.com/people/luxagraf/bookmarks/%s\n\non luxagraf: http://luxagraf.net/link/%s/" %(link.title, link.description, link.url, link.magnolia_id, link.id)
msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['luxagraf@gmail.com'])
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")
def copy_file(url, id):
filename="/home/luxagraf/webapps/static/images/magnolia_thumbs/%s/%s.jpg" %(datetime.datetime.today().strftime("%b").lower(), id)
urllib.urlretrieve(url, filename)
return id
def post_to_tumblr(link):
from links import tumblr
blog = settings.TUMBLR_URL
user= settings.TUMBLR_USER
password = settings.TUMBLR_PASSWORD
api = tumblr.Api(blog,user,password)
post = api.write_link(link.url,name=link.title,description=link.description,date=safestr(link.pub_date))
def send_to_deliciousfb(link):
"""Wanted my links to go to Facebook and since the Facebook API is needlessly complex
I just created a second delicious account and added the feed via facebook"""
del_tags = ''
tags = link.tags.split(',')
for tag in tags:
del_tags += tag.strip().replace(' ','_')+' '
desc = link.description.replace('<blockquote>','\n“')
desc = desc.replace('</blockquote>','”\n')
desc = striptags(desc)
delicious.add(settings.DELTOFACEUSER, settings.DELTOFACEPASS, link.url, link.title, tags = del_tags, extended = desc, dt =safestr(link.pub_date), replace="no")
|