from bs4 import BeautifulSoup
import requests

r = requests.get('https://en-marche.fr/emmanuel-macron/le-programme')

soup = BeautifulSoup(r.text, 'html.parser')

proposals = soup.find_all(class_='programme__proposal')

proposals = [p for p in proposals if 'programme__proposal--category' not in p.attrs['class']]

len(proposals)

p = proposals[0]

full_url = 'https://en-marche.fr' + p.find('a').attrs['href']
full_url

full_urls = ['https://en-marche.fr' + p.find('a').attrs['href'] for p in proposals]

full_urls[:10]

r = requests.get(full_url)
soup = BeautifulSoup(r.text, 'html.parser')

figure_tag = soup.find('figure', class_='fullscreen')
figure_tag

"""
Explanation: Emmanuel Macron
C'est marrant à quel point l'iconographie du site d'Emmanuel est soignée. Peut-on en faire une gallerie d'images?
On part de cette url : https://en-marche.fr/emmanuel-macron/le-programme
End of explanation
"""


src_url = 'https://en-marche.fr' + figure_tag('img')[0].attrs['src']
src_url

"""
Explanation: On peut maintenant extraire le lien vers l'image.
End of explanation
"""


from IPython.display import Image

Image(url=src_url)

def extract_img_src(url):
    "Extracts image src url from linked page."
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    figure_tag = soup.find('figure', class_='fullscreen')
    if figure_tag is not None and figure_tag('img') is not None:
        src_url = 'https://en-marche.fr' + figure_tag('img')[0].attrs['src']
        return src_url
    else:
        print("no image for url: {}".format(url))
        return None

"""
Explanation: On peut afficher ceci dans le notebook.
End of explanation
"""


srcs = [extract_img_src(url) for url in full_urls]

srcs = [_ for _ in srcs if _ is not None]

header = """<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Gallerie des photos du site d'Emmanuel Macron</title>
  <style>
  img {width: 100%;}
  </style>
</head>"""

def format_as_img_tag(src):
    return "<img src={} />".format(src)

format_as_img_tag(srcs[2])

with open('galerie_macron.html', 'w') as f:
    body = """<body>
{0}
</body>""".format("\n".join(format_as_img_tag(url) for url in srcs))
    html = header + body + "</html>"
    f.write(html)

"""
Explanation: On peut répeter ce processus et faire une gallerie avec toutes ces images.
End of explanation
"""


r = requests.get('https://www.fillon2017.fr/projet/')
soup = BeautifulSoup(r.text, 'html.parser')

tags = soup.find_all('a', class_='projectItem__inner')

sublinks = [tag.attrs['href'] for tag in tags]

"""
Explanation: Ce sont des belles photos...
François Fillon
Depuis la sortie du programme de François Fillon, on peut répéter la démarche.
End of explanation
"""


sublinks[0]

r = requests.get(sublinks[0])
soup = BeautifulSoup(r.text, 'html.parser')

src = soup.find('div', class_='singleProject__banner bannerWithMask backgroundCover').attrs['style'].split("background-image: url(")[1][1:-3]

def extract_img_src(url):
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser') 
    src = soup.find('div', class_='singleProject__banner bannerWithMask backgroundCover').attrs['style'].split("background-image: url(")[1][1:-3]
    return src

srcs = [extract_img_src(url) for url in sublinks]

srcs

with open('galerie_fillon.html', 'w') as f:
    body = """<body>
{0}
</body>""".format("\n".join(format_as_img_tag(url) for url in srcs))
    html = header + body + "</html>"
    f.write(html)

"""
Explanation: On s'attaque aux pages individuelles.
End of explanation
"""


