Sunset Bot


In [3]:
import requests, json, random, re, time
from IPython.display import display, Image
from bs4 import BeautifulSoup
import urllib
import twitter

In [4]:
ckey = ''
csecret = ''
tkey = ''
tsecret = ''
from define import set_credential_variables

In [5]:
# you can put your credentials directly here -- I just used a define.py file not to push them to GitHub
api = twitter.Api(consumer_key=ckey,
                      consumer_secret=csecret,
                      access_token_key=tkey,
                      access_token_secret=tsecret)

In [5]:
search = 'sunset'
url = 'https://commons.wikimedia.org/w/api.php?action=query&list=allimages&aiprop=url|name|title|extmetadata&aifrom=' + search + '&format=json'
response = requests.get(url)

In [6]:
adjectifs = ['intéressante', 'superbe', 'stupéfiante', 'stupéfiante, magique', 'impressionnante', 'incroyable', 'intrigante', '']
def get_adjective():
    # random.choice(adjectifs)
    if len(adjectifs) > 0:
        return adjectifs.pop()
    return ""

def display_image(url):
    img_response = urllib.request.urlopen(url).read()
    img_obj = Image(img_response, width=200)
    display(img_obj)

In [7]:
def get_credit(extmetadata):
    if not 'Artist' in extmetadata.keys():
        return None
    author_link = extmetadata['Artist']['value']
    author_soup = BeautifulSoup(author_link, "html.parser")
    author = author_soup.find('a').string

    license = extmetadata['LicenseShortName']['value']
        
    return {'license': license, 'author': author}

In [44]:
r_num = re.compile(r"(^.*)(/.{1,2}/.{1,2}/)(.*$)")
def get_thumbnail(img_link):
    match = re.split(r_num, img_link)
    if len(match) > 2:
        return match[1] + '/thumb' + match[2] + match[3] + "/800px-" + match[3]
    else:
        return None

In [47]:
imgs = response.json()

r_jpg = re.compile(r"File:(.*)\.(jpg|JPG|jpeg)$")
limit = 6
i = 0
for img in imgs['query']['allimages']:
    
    if i > limit:
        break
    filename = img['title']
    title = re.match(r_jpg, filename)
    if title:
        attr = get_credit(img['extmetadata'])
        if attr:
            
            phrase = "Le wikipédien " + attr['author'] + " a nommé cette " + get_adjective() + " photo «"
            phrase += title.group(1)
            phrase += "». Elle est sous licence " + attr['license'] + ". "
            
            print(len(phrase))
            #print(img)
            print(phrase)
            
            thumb = get_thumbnail(img['url'])
            
            if thumb:
                display_image(thumb)
            else:
                print('Erreur avec', img['url'])
            
            
            result = api.PostUpdate(phrase, thumb)

            print(result)
            
            time.sleep(90)
            i += 1


93
Le wikipédien SachinBnz a nommé cette  photo «Sunset!!». Elle est sous licence CC BY-SA 4.0. 
{"created_at": "Fri Jul 01 01:35:55 +0000 2016", "hashtags": [], "id": 748691572091478016, "id_str": "748691572091478016", "lang": "fr", "media": [{"display_url": "pic.twitter.com/y4i8XfD0RU", "expanded_url": "http://twitter.com/palroggbot/status/748691572091478016/photo/1", "id": 748691567796424704, "media_url": "http://pbs.twimg.com/media/CmPjA6DWEAAFbEm.jpg", "media_url_https": "https://pbs.twimg.com/media/CmPjA6DWEAAFbEm.jpg", "type": "photo", "url": "https://t.co/y4i8XfD0RU"}], "source": "<a href=\"http://paulronga.ch/pancake\" rel=\"nofollow\">Pancake Generator</a>", "text": "Le wikip\u00e9dien SachinBnz a nomm\u00e9 cette  photo \u00abSunset!!\u00bb. Elle est sous licence CC BY-SA 4.0. https://t.co/y4i8XfD0RU", "urls": [], "user": {"created_at": "Fri May 20 18:48:39 +0000 2016", "description": "Une petite chose manipul\u00e9e par @palrogg. Image CC Rnewkirk7954", "followers_count": 2, "friends_count": 2, "id": 733731175798349827, "lang": "fr", "name": "Brobot", "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_image_url": "http://pbs.twimg.com/profile_images/748641777280819200/duHPFkgR_normal.jpg", "profile_link_color": "981CEB", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "screen_name": "palroggbot", "statuses_count": 5}, "user_mentions": []}
96
Le wikipédien Ashish Sharma a nommé cette  photo «Sunset!». Elle est sous licence CC BY-SA 4.0. 
{"created_at": "Fri Jul 01 01:37:27 +0000 2016", "hashtags": [], "id": 748691958583988224, "id_str": "748691958583988224", "lang": "fr", "media": [{"display_url": "pic.twitter.com/7XBfyqIbN6", "expanded_url": "http://twitter.com/palroggbot/status/748691958583988224/photo/1", "id": 748691954330927104, "media_url": "http://pbs.twimg.com/media/CmPjXaAWgAAgbnb.jpg", "media_url_https": "https://pbs.twimg.com/media/CmPjXaAWgAAgbnb.jpg", "type": "photo", "url": "https://t.co/7XBfyqIbN6"}], "source": "<a href=\"http://paulronga.ch/pancake\" rel=\"nofollow\">Pancake Generator</a>", "text": "Le wikip\u00e9dien Ashish Sharma a nomm\u00e9 cette  photo \u00abSunset!\u00bb. Elle est sous licence CC BY-SA 4.0. https://t.co/7XBfyqIbN6", "urls": [], "user": {"created_at": "Fri May 20 18:48:39 +0000 2016", "description": "Une petite chose manipul\u00e9e par @palrogg. Image CC Rnewkirk7954", "followers_count": 2, "friends_count": 2, "id": 733731175798349827, "lang": "fr", "name": "Brobot", "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_image_url": "http://pbs.twimg.com/profile_images/748641777280819200/duHPFkgR_normal.jpg", "profile_link_color": "981CEB", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "screen_name": "palroggbot", "statuses_count": 6}, "user_mentions": []}
98
Le wikipédien Arpit chhonker a nommé cette  photo «Sunset!@». Elle est sous licence CC BY-SA 4.0. 
{"created_at": "Fri Jul 01 01:39:00 +0000 2016", "hashtags": [], "id": 748692347869814784, "id_str": "748692347869814784", "lang": "fr", "media": [{"display_url": "pic.twitter.com/8CJQEDLj1q", "expanded_url": "http://twitter.com/palroggbot/status/748692347869814784/photo/1", "id": 748692342832558080, "media_url": "http://pbs.twimg.com/media/CmPjuBSW8AAjnze.jpg", "media_url_https": "https://pbs.twimg.com/media/CmPjuBSW8AAjnze.jpg", "type": "photo", "url": "https://t.co/8CJQEDLj1q"}], "source": "<a href=\"http://paulronga.ch/pancake\" rel=\"nofollow\">Pancake Generator</a>", "text": "Le wikip\u00e9dien Arpit chhonker a nomm\u00e9 cette  photo \u00abSunset!@\u00bb. Elle est sous licence CC BY-SA 4.0. https://t.co/8CJQEDLj1q", "urls": [], "user": {"created_at": "Fri May 20 18:48:39 +0000 2016", "description": "Une petite chose manipul\u00e9e par @palrogg. Image CC Rnewkirk7954", "followers_count": 2, "friends_count": 2, "id": 733731175798349827, "lang": "fr", "name": "Brobot", "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_image_url": "http://pbs.twimg.com/profile_images/748641777280819200/duHPFkgR_normal.jpg", "profile_link_color": "981CEB", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "screen_name": "palroggbot", "statuses_count": 6}, "user_mentions": []}
99
Le wikipédien Hibatuun a nommé cette  photo «Sunset's beauty». Elle est sous licence CC BY-SA 4.0. 
{"created_at": "Fri Jul 01 01:40:32 +0000 2016", "hashtags": [], "id": 748692735947857921, "id_str": "748692735947857921", "lang": "fr", "media": [{"display_url": "pic.twitter.com/7DGbRhwzJa", "expanded_url": "http://twitter.com/palroggbot/status/748692735947857921/photo/1", "id": 748692731879383040, "media_url": "http://pbs.twimg.com/media/CmPkEqmWYAA9FoN.jpg", "media_url_https": "https://pbs.twimg.com/media/CmPkEqmWYAA9FoN.jpg", "type": "photo", "url": "https://t.co/7DGbRhwzJa"}], "source": "<a href=\"http://paulronga.ch/pancake\" rel=\"nofollow\">Pancake Generator</a>", "text": "Le wikip\u00e9dien Hibatuun a nomm\u00e9 cette  photo \u00abSunset's beauty\u00bb. Elle est sous licence CC BY-SA 4.0. https://t.co/7DGbRhwzJa", "urls": [], "user": {"created_at": "Fri May 20 18:48:39 +0000 2016", "description": "Une petite chose manipul\u00e9e par @palrogg. Image CC Rnewkirk7954", "followers_count": 2, "friends_count": 2, "id": 733731175798349827, "lang": "fr", "name": "Brobot", "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_image_url": "http://pbs.twimg.com/profile_images/748641777280819200/duHPFkgR_normal.jpg", "profile_link_color": "981CEB", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "screen_name": "palroggbot", "statuses_count": 8}, "user_mentions": []}
100
Le wikipédien Dti a nommé cette  photo «Sunset(0) 2005-11-21». Elle est sous licence Public domain. 
{"created_at": "Fri Jul 01 01:42:04 +0000 2016", "hashtags": [], "id": 748693121932881920, "id_str": "748693121932881920", "lang": "fr", "media": [{"display_url": "pic.twitter.com/tWxR35uDdr", "expanded_url": "http://twitter.com/palroggbot/status/748693121932881920/photo/1", "id": 748693118740987906, "media_url": "http://pbs.twimg.com/media/CmPkbLxWAAIy0qU.jpg", "media_url_https": "https://pbs.twimg.com/media/CmPkbLxWAAIy0qU.jpg", "type": "photo", "url": "https://t.co/tWxR35uDdr"}], "source": "<a href=\"http://paulronga.ch/pancake\" rel=\"nofollow\">Pancake Generator</a>", "text": "Le wikip\u00e9dien Dti a nomm\u00e9 cette  photo \u00abSunset(0) 2005-11-21\u00bb. Elle est sous licence Public domain. https://t.co/tWxR35uDdr", "urls": [], "user": {"created_at": "Fri May 20 18:48:39 +0000 2016", "description": "Une petite chose manipul\u00e9e par @palrogg. Image CC Rnewkirk7954", "followers_count": 2, "friends_count": 2, "id": 733731175798349827, "lang": "fr", "name": "Brobot", "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_image_url": "http://pbs.twimg.com/profile_images/748641777280819200/duHPFkgR_normal.jpg", "profile_link_color": "981CEB", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "screen_name": "palroggbot", "statuses_count": 9}, "user_mentions": []}
100
Le wikipédien Dti a nommé cette  photo «Sunset(1) 2005-11-21». Elle est sous licence Public domain. 
{"created_at": "Fri Jul 01 01:43:37 +0000 2016", "hashtags": [], "id": 748693509880868864, "id_str": "748693509880868864", "lang": "fr", "media": [{"display_url": "pic.twitter.com/lZyQjxvpsT", "expanded_url": "http://twitter.com/palroggbot/status/748693509880868864/photo/1", "id": 748693505028005888, "media_url": "http://pbs.twimg.com/media/CmPkxqzWIAA5VeI.jpg", "media_url_https": "https://pbs.twimg.com/media/CmPkxqzWIAA5VeI.jpg", "type": "photo", "url": "https://t.co/lZyQjxvpsT"}], "source": "<a href=\"http://paulronga.ch/pancake\" rel=\"nofollow\">Pancake Generator</a>", "text": "Le wikip\u00e9dien Dti a nomm\u00e9 cette  photo \u00abSunset(1) 2005-11-21\u00bb. Elle est sous licence Public domain. https://t.co/lZyQjxvpsT", "urls": [], "user": {"created_at": "Fri May 20 18:48:39 +0000 2016", "description": "Une petite chose manipul\u00e9e par @palrogg. Image CC Rnewkirk7954", "followers_count": 2, "friends_count": 2, "id": 733731175798349827, "lang": "fr", "name": "Brobot", "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_image_url": "http://pbs.twimg.com/profile_images/748641777280819200/duHPFkgR_normal.jpg", "profile_link_color": "981CEB", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "screen_name": "palroggbot", "statuses_count": 10}, "user_mentions": []}
116
Le wikipédien allen watkin a nommé cette  photo «Sunset(natural) (3899828071)». Elle est sous licence CC BY-SA 2.0. 
{"created_at": "Fri Jul 01 01:45:09 +0000 2016", "hashtags": [], "id": 748693897778429952, "id_str": "748693897778429952", "lang": "fr", "media": [{"display_url": "pic.twitter.com/XC4EGKX4se", "expanded_url": "http://twitter.com/palroggbot/status/748693897778429952/photo/1", "id": 748693892351025152, "media_url": "http://pbs.twimg.com/media/CmPlINsWYAAbeyw.jpg", "media_url_https": "https://pbs.twimg.com/media/CmPlINsWYAAbeyw.jpg", "type": "photo", "url": "https://t.co/XC4EGKX4se"}], "source": "<a href=\"http://paulronga.ch/pancake\" rel=\"nofollow\">Pancake Generator</a>", "text": "Le wikip\u00e9dien allen watkin a nomm\u00e9 cette  photo \u00abSunset(natural) (3899828071)\u00bb. Elle est sous licence CC BY-SA 2.0. https://t.co/XC4EGKX4se", "urls": [], "user": {"created_at": "Fri May 20 18:48:39 +0000 2016", "description": "Une petite chose manipul\u00e9e par @palrogg. Image CC Rnewkirk7954", "followers_count": 2, "friends_count": 2, "id": 733731175798349827, "lang": "fr", "name": "Brobot", "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_image_url": "http://pbs.twimg.com/profile_images/748641777280819200/duHPFkgR_normal.jpg", "profile_link_color": "981CEB", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "screen_name": "palroggbot", "statuses_count": 11}, "user_mentions": []}