-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathosmtagchallenge.py
More file actions
113 lines (82 loc) · 3.29 KB
/
Copy pathosmtagchallenge.py
File metadata and controls
113 lines (82 loc) · 3.29 KB
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
import tweepy
from mastodon import Mastodon
import flickrapi
import json
import random
import os
import config
JLZ_flickr_simple_id = "jeanlouis_zimmermann"
JLZ_flickr_id = "40911451@N00"
album_id = '72177720298086796'
published_file = os.path.join(os.path.dirname(__file__), 'published.json')
def tweet_image(url):
# Authenticate to Twitter
auth = tweepy.OAuthHandler(config.twitter_api_key, config.twitter_api_key_secret)
auth.set_access_token(config.twitter_access_token, config.twitter_access_token_secret)
try:
# Create API object
api = tweepy.API(auth)
# Create a tweet
print("Publication du tweet")
status = api.update_status("#OpenStreetMap tag challenge\n" + url)
return status.id
except TwitterException:
print("Erreur pendant la publication du tweet")
return None
def toot_image(url):
try:
mastodon = Mastodon(
client_id = os.path.join(os.path.dirname(__file__), 'osmtagchallenge.secret'),
api_base_url = 'https://mastodon.social'
)
mastodon.log_in(
config.mastodon_login,
config.mastodon_password
)
print("Publication du toot")
status = mastodon.toot("#OpenStreetMap tag challenge\n" + url)
return status["id"]
except:
print("Erreur pendant la publication du toot")
return None
def get_photos(photoset_id):
flickr = flickrapi.FlickrAPI(config.flickr_api_key, config.flickr_secret, format='parsed-json')
print("Récupération de la première page de l'album")
photos = flickr.photosets.getPhotos(user_id=JLZ_flickr_id, photoset_id=photoset_id)
result = [p["id"] for p in photos["photoset"]["photo"]]
if photos["photoset"]["pages"] != 1:
for page in range(2, photos["photoset"]["pages"]):
print("Récupération de la page " + str(page) + " de l'album")
photos = flickr.photosets.getPhotos(user_id=JLZ_flickr_id, photoset_id=photoset_id, page=page)
result += [p["id"] for p in photos["photoset"]["photo"]]
return result
def build_image_url(image_id, album_id):
return "https://www.flickr.com/photos/" + JLZ_flickr_simple_id + "/" + image_id + "/in/album-" + album_id + "/"
def get_already_published_list():
result = []
try:
with open(published_file, 'r') as f:
result = json.load(f)
except FileNotFoundError:
pass
return result
# get photos
photos = get_photos(album_id)
# get already published photos
already_published = get_already_published_list()
already_published_flat = [x["id"] if isinstance(x, dict) else x for x in already_published]
# only keep new ones
photos = [p for p in photos if p not in already_published_flat]
# randomly select one image
selected = random.choice(photos)
# tweet this image
result_twitter = tweet_image(build_image_url(selected, album_id))
# toot this image
result_mastodon = toot_image(build_image_url(selected, album_id))
# save the tweeted image
if result_twitter is not None or result_mastodon is not None:
already_published.append({"id": selected, "twitter": result_twitter, "mastodon": result_mastodon})
with open(published_file, 'w') as f:
json.dump(already_published, f)
else:
print("Erreur: ni Twitter ni Mastodon n'ont été alimentés.")