Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from github import Github
- from github import InputFileContent
- import base64
- import re
- import time
- import requests
- from warnings import warn
- PATH = "C:\Yadkee\\"
- def _login():
- """Return github and github user objects"""
- with open(PATH + "github.credentials") as f:
- user, password = re.match('User="(.*?)", Password="(.*?)"', f.read()).groups()
- gh = Github(user, password)
- ghMe = gh.get_user()
- if gh.rate_limiting[0] < 200:
- warn("There are only %d interactions left!" % gh.rate_limiting[0])
- return gh, ghMe
- GH, GHME = _login()
- def _insert_newlines(string, every=128):
- return "\n".join(string[i:i + every] for i in range(0, len(string), every))
- def _encode(b):
- return _insert_newlines(base64.b85encode(b).decode("ascii"))
- def _decode(b):
- return base64.b85decode(b.replace("\n", "").encode("ascii"))
- def _correct_description(description):
- if not description.startswith("*"):
- description = "*" + description
- return description.replace(" ", "")
- def _is_description_correct(description):
- return description.startswith("*") and " " not in description
- def create_gist(description, public=False):
- """Create a gist with a description and return itself"""
- description = _correct_description(description)
- if get_gist(description):
- raise Exception("It already exists a gist with that description (%s)" % description)
- files = {"__info__": InputFileContent("-Created at %s" % time.strftime("%H:%M of %d/%m/%Y"), "__info__")}
- return GHME.create_gist(public, files, description)
- def get_gist(description):
- """Return the gist that has a certain description"""
- description = _correct_description(description).lower()
- for gist in GHME.get_gists():
- if gist.description.lower() == description:
- return gist
- else:
- return None
- def list_gists():
- """Return {description1: (file1, file2), d2: (f1, f2, f3, f4), d3: (f1), ...}
- Only returns those gist which description follows the naming convention"""
- output = {}
- for gist in GHME.get_gists():
- if _is_description_correct(gist.description):
- output[gist.description] = tuple(gist.files.keys())
- return output
- def _download_gist_files(gist, ignore=""):
- return dict((i.filename, InputFileContent(requests.get(i.raw_url).text, i.filename)) for i in gist.files.values() if i.filename != ignore)
- class File():
- @classmethod
- def from_gistfile(cls, gistfile):
- text = requests.get(gistfile.raw_url).text
- header, body = text.split("\n", 1)
- name = gistfile.filename
- encoded = True if header[0] is ">" else False
- data = _decode(body) if encoded else body
- note = header[header.find(";") + 2:]
- modified = header[1:header.find(";")]
- return cls(name, data, note, encoded, modified)
- def __repr__(self):
- return 'File(name="%s", size=%d)' % (self.name, self.size)
- def __init__(self, name, data, note="", encoded=True, modified=None):
- self.name = name
- self.data = data
- self.note = note
- self.encoded = encoded
- self.modified = modified
- self.size = len(data)
- @property
- def text(self):
- initialChr = ">" if self.encoded else "-"
- date = time.strftime("%d/%m/%Y, %H:%M; ")
- header = initialChr + date + self.note + "\n"
- body = _encode(self.data) if self.encoded else self.data.decode("utf-8")
- return header + body
- def to_input(self):
- return InputFileContent(self.text, self.name)
- def upload_to(gist, file):
- files = _download_gist_files(gist, file.name)
- files[file.name] = file.to_input()
- gist.edit(gist.description, files)
- def download_from(gist, filename):
- for i in gist.files.values():
- if i.filename == filename:
- return File.from_gistfile(i)
- def delete_from(gist, filename):
- files = _download_gist_files(gist, ignore=filename)
- gist.edit(gist.description, files)
- remove_from = delete_from
- def rename_from(gist, oldname, newname):
- files = _download_gist_files(gist)
- files[oldname].new_name = newname
- gist.edit(gist.description, files)
- def rename(gist, newdescription):
- files = _download_gist_files(gist)
- gist.edit(_correct_description(newdescription), files)
- def main():
- print("TEST")
- # No tests by now
- print("END")
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment