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
|
import os
from django.conf import settings
from django.test.client import Client
from twython import Twython
from bs4 import BeautifulSoup
from medium import Client as MediumClient
import flickrapi
import facebook
from photos.models import LuxImage
def absolute_urls_for_syndication(s):
soup = BeautifulSoup(s, "lxml")
for a in soup.find_all('a'):
if a['href'][:1] == "/":
a['href'] = "https://luxagraf.net%s" % a['href']
return soup
def post_to_medium(item):
client = MediumClient(application_id=settings.MEDIUM_CLIENT_ID, application_secret=settings.MEDIUM_CLIENT_SECRET)
client.access_token = settings.MEDIUM_INT_TOKEN
user = client.get_current_user()
head = '<p><i>This was originally posted <a href="https://luxagraf.net%s" rel="canonical">on my own site</a>.</i></p>' % item.get_absolute_url()
body = "%s %s" % (head, absolute_urls_for_syndication(item.body_html))
# Create a post.
post = client.create_post(
user_id=user["id"],
title=item.title,
content=body,
content_format="html",
publish_status="public",
canonical_url="https://luxagraf.net%s" % item.get_absolute_url(),
license="all-rights-reserved"
)
return post["url"]
def build_facebook_feed():
print("+++++++++++++building+++++++++++")
c = Client()
response = c.get('/iafeed.xml', HTTP_HOST='127.0.0.1')
f = open("%siafeed.xml" % settings.FLATFILES_ROOT, 'wb')
f.write(response.content)
f.close()
def post_to_twitter(obj, ctype):
print("content type is" + ctype)
t = Twython(settings.TWITTER_API_KEY, settings.TWITTER_API_SECRET, settings.TWITTER_ACCESS_TOKEN, settings.TWITTER_ACCESS_SECRET)
imgs = []
if ctype == "lux image":
p = open(obj.get_largest_image(), 'rb')
if obj.caption:
status = obj.caption
else:
status = obj.title
response = t.upload_media(media=p)
imgs.append(response)
elif ctype == "lux note":
status = obj.body_markdown
# parse out images and send seperately
soup = BeautifulSoup(status, "lxml")
loop = 0
for img in soup.find_all('img'):
src = img['src'].split("images/")[1]
i = LuxImage.objects.get(image__icontains=src)
p = open(i.get_largest_image(), 'rb')
response = t.upload_media(media=p)
imgs.append(response)
loop = loop+1
if loop == 3:
break
soup.find_all('img').replaceWith("")
# truncate message
if status.length > 140:
try:
status = status.split("|")[0] + obj.get_absolute_url()
except:
status = status[:140] + obj.get_absolute_url()
print(status)
try:
geo = t.reverse_geocode(lat=obj.latitude, lon=obj.longitude, accuracy=1500, granularity="city")
geo_id = geo['result']['places'][0]['id']
except:
pass
try:
status = t.update_status(status=status, media_ids=[img['media_id'] for img in imgs], place_id=geo_id)
except:
try:
status = t.update_status(status=status, media_ids=[img['media_id'] for img in imgs])
except:
status = t.update_status(status=status)
def post_photo_to_flickr(photo):
TOKEN_FILE = os.path.join(settings.PROJ_ROOT, "config/flickrToken")
token = open(TOKEN_FILE).read()
flickr = flickrapi.FlickrAPI(settings.FLICKR_API_KEY, settings.FLICKR_API_SECRET, token=token)
sent = flickr.upload(filename=photo.get_image_path_by_size("original"), title=photo.title, description=photo.caption)
photo_id = sent.find('photoid').text
photo.flickr_id = photo_id
try:
flickr.photos.geo.setLocation(photo_id=photo_id, lat=photo.latitude, lon=photo.longitude, accuracy=12)
except:
pass
def post_to_facebook(obj, ctype):
from syndication.models import FBOAuthToken
token = FBOAuthToken.objects.latest()
graph = facebook.GraphAPI(access_token=token.long_token, version='2.2')
if ctype == "lux image":
p = open(obj.get_largest_image(), 'rb')
if obj.caption:
message = obj.caption
else:
message = obj.title
try:
fb_response = graph.put_photo(p, message=message)
print(fb_response)
except facebook.GraphAPIError as e:
print('Something went wrong:', e.type, e.message)
|