SHARE
TWEET

Untitled

a guest Dec 22nd, 2017 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from github import Github
  4. from github import InputFileContent
  5. import base64
  6. import re
  7. import time
  8. import requests
  9. from warnings import warn
  10. PATH = "C:\Yadkee\\"
  11.  
  12.  
  13. def _login():
  14.     """Return github and github user objects"""
  15.     with open(PATH + "github.credentials") as f:
  16.         user, password = re.match('User="(.*?)", Password="(.*?)"', f.read()).groups()
  17.     gh = Github(user, password)
  18.     ghMe = gh.get_user()
  19.     if gh.rate_limiting[0] < 200:
  20.         warn("There are only %d interactions left!" % gh.rate_limiting[0])
  21.     return gh, ghMe
  22.  
  23.  
  24. GH, GHME = _login()
  25.  
  26.  
  27. def _insert_newlines(string, every=128):
  28.     return "\n".join(string[i:i + every] for i in range(0, len(string), every))
  29.  
  30.  
  31. def _encode(b):
  32.     return _insert_newlines(base64.b85encode(b).decode("ascii"))
  33.    
  34.    
  35. def _decode(b):
  36.     return base64.b85decode(b.replace("\n", "").encode("ascii"))
  37.  
  38.  
  39. def _correct_description(description):
  40.     if not description.startswith("*"):
  41.         description = "*" + description
  42.     return description.replace(" ", "")
  43.  
  44.  
  45. def _is_description_correct(description):
  46.     return description.startswith("*") and " " not in description
  47.  
  48.  
  49. def create_gist(description, public=False):
  50.     """Create a gist with a description and return itself"""
  51.     description = _correct_description(description)
  52.     if get_gist(description):
  53.         raise Exception("It already exists a gist with that description (%s)" % description)
  54.     files = {"__info__": InputFileContent("-Created at %s" % time.strftime("%H:%M of %d/%m/%Y"), "__info__")}
  55.     return GHME.create_gist(public, files, description)
  56.    
  57.    
  58. def get_gist(description):
  59.     """Return the gist that has a certain description"""
  60.     description = _correct_description(description).lower()
  61.     for gist in GHME.get_gists():
  62.         if gist.description.lower() == description:
  63.             return gist
  64.     else:
  65.         return None
  66.    
  67.    
  68. def list_gists():
  69.     """Return {description1: (file1, file2), d2: (f1, f2, f3, f4), d3: (f1), ...}
  70.         Only returns those gist which description follows the naming convention"""
  71.     output = {}
  72.     for gist in GHME.get_gists():
  73.         if _is_description_correct(gist.description):
  74.             output[gist.description] = tuple(gist.files.keys())
  75.     return output
  76.  
  77.  
  78. def _download_gist_files(gist, ignore=""):
  79.     return dict((i.filename, InputFileContent(requests.get(i.raw_url).text, i.filename)) for i in gist.files.values() if i.filename != ignore)
  80.  
  81.  
  82. class File():
  83.     @classmethod
  84.     def from_gistfile(cls, gistfile):
  85.         text = requests.get(gistfile.raw_url).text
  86.         header, body = text.split("\n", 1)
  87.        
  88.         name = gistfile.filename
  89.         encoded = True if header[0] is ">" else False
  90.         data = _decode(body) if encoded else body
  91.         note = header[header.find(";") + 2:]
  92.         modified = header[1:header.find(";")]
  93.         return cls(name, data, note, encoded, modified)
  94.    
  95.     def __repr__(self):
  96.         return 'File(name="%s", size=%d)' % (self.name, self.size)
  97.    
  98.     def __init__(self, name, data, note="", encoded=True, modified=None):
  99.         self.name = name
  100.         self.data = data
  101.         self.note = note
  102.         self.encoded = encoded
  103.         self.modified = modified
  104.         self.size = len(data)
  105.        
  106.     @property
  107.     def text(self):
  108.         initialChr = ">" if self.encoded else "-"
  109.         date = time.strftime("%d/%m/%Y, %H:%M; ")
  110.         header = initialChr + date + self.note + "\n"
  111.         body = _encode(self.data) if self.encoded else self.data.decode("utf-8")
  112.         return header + body
  113.  
  114.     def to_input(self):
  115.         return InputFileContent(self.text, self.name)
  116.  
  117.  
  118. def upload_to(gist, file):
  119.     files = _download_gist_files(gist, file.name)
  120.     files[file.name] = file.to_input()
  121.     gist.edit(gist.description, files)
  122.    
  123.  
  124. def download_from(gist, filename):
  125.     for i in gist.files.values():
  126.         if i.filename == filename:
  127.             return File.from_gistfile(i)
  128.        
  129.        
  130. def delete_from(gist, filename):
  131.     files = _download_gist_files(gist, ignore=filename)
  132.     gist.edit(gist.description, files)
  133.    
  134.  
  135. remove_from = delete_from
  136.        
  137.  
  138. def rename_from(gist, oldname, newname):
  139.     files = _download_gist_files(gist)
  140.     files[oldname].new_name = newname
  141.     gist.edit(gist.description, files)
  142.    
  143.  
  144. def rename(gist, newdescription):
  145.     files = _download_gist_files(gist)
  146.     gist.edit(_correct_description(newdescription), files)
  147.    
  148.    
  149. def main():
  150.     print("TEST")
  151.     # No tests by now
  152.     print("END")
  153.  
  154.  
  155. if __name__ == "__main__":
  156.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top