Skip to content

Instantly share code, notes, and snippets.

@Adizlois
Last active May 6, 2017 17:29
Show Gist options
  • Save Adizlois/a252bfd2f971e0cfd5d764f6accfd394 to your computer and use it in GitHub Desktop.
Save Adizlois/a252bfd2f971e0cfd5d764f6accfd394 to your computer and use it in GitHub Desktop.
Using Google authentification in order to grab a file that requires so.....
import requests
from bs4 import BeautifulSoup
#THIS SEEMS TO WORK OK....REQUESTS.SESSION() SHOULD DEAL WITH THE COOKIES AND KEEP THE USER LOGGED BUT IT DOESN`T...
class SessionGoogle:
def __init__(self, url_login, url_auth, login, pwd):
self.ses = requests.session()
login_html = self.ses.get(url_login)
soup_login = BeautifulSoup(login_html.content).find('form').find_all('input')
my_dict = {}
for u in soup_login:
if u.has_attr('value'):
my_dict[u['name']] = u['value']
# SET VALUES ID & PASSWD
my_dict['Email'] = login
my_dict['Passwd'] = pwd
self.ses.post(url_auth, data=my_dict)
def get(self, URL):
return self.ses.get(URL,stream=True)
#SET GOOGLE VARIABLES AND LOGIN... (CHANGE "YOMISMO" AND "MISMAMENTE")
url_login = "https://accounts.google.com/ServiceLogin"
url_auth = "https://accounts.google.com/ServiceLoginAuth"
session = SessionGoogle(url_login, url_auth, "YOMISMO", "MISMAMENTE")
#IT WORKS FINE WITH GMAIL
print requests.session().get("https://mail.google.com/mail/").text
#BUT NOT WHEN USED TO GRAB A FILE ELSEWHERE THAT REQUIRES GOOGLE TOKEN....
address="https://five.epicollect.net/api/internal/media/rovdyr?type=photo&format=entry_original&name=a28dca40-30d8-11e7-a656-87e8c7a439a4_1493909441.jpg"
r = session.get(address)
#SAVE FILE
with open('Prueba.jpg', 'wb') as wt:
for block in r.iter_content(1024):
if not block:
break
wt.write(block)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment