Created
December 4, 2014 20:10
-
-
Save jcrubino/6007affdea9f871f481c to your computer and use it in GitHub Desktop.
Gogs Http Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from lxml import html # would prefer not to parse html but currently this is the only option | |
# set these to whatever your gogs account is | |
username = "username" | |
password = "password" | |
cookie_filename = "gogs.cookies" | |
gogs_url = "your_gogs_url" | |
gogs_port = "3000" | |
https = False | |
class GogsHTTPClient(object): | |
def __init__(self, username=None, password=None, gogs_url=None, gogs_port=None, https=False): | |
""" | |
A Gogs HTTP(s) Python Client | |
client = GogsHTTPClient(myusername, mypassword, 'http://www.gogsurl.net', '3000') | |
""" | |
self._client = requests.session() | |
self.base_url = 'http://'+gogs_url+':'+gogs_port | |
self.username = username | |
self.password = password | |
self.response = None | |
self.parse = html | |
self.headers = {'Acccept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'User-agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)'} | |
self.login() | |
def csrf(self): | |
return self._client.cookies['_csrf'] | |
def login(self): | |
URL = self.base_url+"/user/login" | |
self._client.get(URL) | |
self.headers["Referer"] = self.base_url | |
data = {"uname":self.username, "password":self.password,"next":'/'} | |
self.response = self._client.post(URL, data=data, headers=self.headers) | |
def create_organization(self, name, email): | |
URL = self.base_url+"/org/create" | |
self._client.get(URL) | |
self.headers["Referer"] = self.base_url | |
data = {'org_name':name, 'email':email, '_csrf':self.csrf()} | |
self.response = self._client.post(url=URL, data=data, headers=self.headers) | |
def update_organization(self, organization_name, email,fullname=None, description="", website="", location=""): | |
""" | |
If data is blank those fields will be erased. | |
action:update | |
uname | |
fullname | |
desc | |
website | |
location | |
avatar - email of uname | |
""" | |
URL = self.base_url+"/org/{0}/settings".format(organization_name) | |
if fullname == None: | |
fullname = organization_name | |
self._client.get(URL) | |
self.headers["Referer"] = self.base_url+"/org/{0}/settings".format(organization_name) | |
data = {'_csrf':self.csrf(), | |
'action': 'update', | |
'uname': organization_name, | |
'fullname':fullname, | |
"email":email, | |
"desc":description, | |
"website":website, | |
"location":location, | |
"avatar":email} | |
self.response = self._client.post(url=URL, data=data, headers=self.headers) | |
def delete_organization(self, organization_name): | |
""" | |
Request http://base_url/org/organization_name/settings/delete | |
""" | |
URL = self.base_url+"/org/{0}/settings/delete".format(organization_name) | |
self._client.get(URL) | |
self.headers["Referer"] = self.base_url+"/org/{0}/settings/delete".format(organization_name) | |
data = {'_csrf':self.csrf()} | |
self.response = self._client.post(url=URL, data=data, headers=self.headers) | |
def invite_user(self, organization_name, username): | |
''' | |
Request http://base_url/org/organization_name/invitations/new | |
uname: username | |
''' | |
URL = self.base_url+"org/{0}/invitations/new".format(organization_name) | |
self._client.get(URL) | |
self.headers["Referer"] = URL | |
data = {'_csrf':self.csrf(), 'uname':username} | |
self.response = self._client.post(url=URL, data=data, headers=self.headers) | |
def create_organizational_repo(self, organization_name, repo_name=None, repo_description=None): | |
URL = self.base_url+'/org/'+organization_name | |
#r = self._client.get(URL) | |
#call = html.fromstring(r.text) | |
#stub = call.cssselect("#org-home-repo-list > div.clear > a")[-1].attrib['href'] | |
#url = self.base_url+stub | |
self._client.get(URL) | |
self.headers["Referer"] = URL | |
return stub | |
# sign in | |
test = GogsHTTPClient(username, password, gogs_url, gogs_port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment