@@ -40,25 +40,73 @@ def create_gist(username,TOKEN,verbose = 0):
4040 # Create a local file to upload
4141 FILEPATH = "/content/" + filename
4242 with open (FILEPATH , "w" , encoding = "utf-8" ) as f :
43- f .write ("hello, world \n " )
43+ f .write ("0 " )
4444
4545 # Read content
4646 with open (FILEPATH , "r" , encoding = "utf-8" ) as f :
4747 content = f .read ()
4848 filename = os .path .basename (FILEPATH )
4949
50- import smtplib
51- from email .mime .text import MIMEText
52-
5350 # Create the gist
54- DESCRIPTION = "Example gist created from Colab via .env and PyGithub "
51+ DESCRIPTION = "No action posted "
5552 gist = g .get_user ().create_gist (
5653 public = True ,
5754 files = {filename : InputFileContent (content )},
5855 description = DESCRIPTION ,
5956 )
57+ if verbose > 0 :
58+ print ("Gist created!" )
59+ print ("Web view:" , gist .html_url )
60+ print ("Raw file:" , gist .files [filename ].raw_url )
61+ return filename
62+
63+ def update_gist (username ,TOKEN ,NEW_CONTENT ,verbose = 0 ):
64+ FILENAME = username + "-actions.txt" # must match the file inside the Gist
65+ import os , requests
66+ from datetime import datetime , timezone , timedelta
67+ from github import Github , Auth , InputFileContent
68+
69+ # --- Config you provide ---
70+ now_paris = datetime .now (timezone (timedelta (hours = 2 )))
71+ date_str = now_paris .strftime ("%Y-%m-%d %H:%M:%S" )
72+ FILENAME_MATCH = FILENAME # <-- put your filename here
73+ NEW_DESCRIPTION = "Updated action"
74+
75+ # --- Optional: confirm identity & scopes (helps debug 403/404) ---
76+ r = requests .get ("https://api.github.com/user" ,
77+ headers = {"authorization" : f"token { TOKEN } " ,
78+ "Accept" :"application/vnd.github+json" },
79+ timeout = 20 )
80+ r .raise_for_status ()
81+ print ("Authenticated as:" , r .json ().get ("login" ))
82+ print ("Token scopes (X-OAuth-Scopes):" , r .headers .get ("X-OAuth-Scopes" ))
83+
84+ # --- Auth (new-style) ---
85+ g = Github (auth = Auth .Token (TOKEN ))
86+ me = g .get_user ()
87+
88+ # --- Find gists that contain the filename, choose the most recently updated ---
89+ candidates = []
90+ for gs in me .get_gists (): # includes your secret gists
91+ if FILENAME_MATCH in gs .files :
92+ candidates .append (gs )
93+
94+ if not candidates :
95+ raise RuntimeError (f"No gist owned by you contains a file named '{ FILENAME_MATCH } '." )
96+
97+ # pick the most recently updated gist
98+ candidates .sort (key = lambda x : x .updated_at or datetime (1970 ,1 ,1 , tzinfo = timezone .utc ), reverse = True )
99+ gist = candidates [0 ]
100+ print (f"Resolved gist: { gist .id } | desc: { gist .description !r} | updated_at: { gist .updated_at } " )
101+
102+ # --- Update that file (adds the file if it didn't exist; here it does) ---
103+ gist .edit (
104+ description = NEW_DESCRIPTION ,
105+ files = {FILENAME_MATCH : InputFileContent (NEW_CONTENT )}
106+ )
107+ if verbose > 0 :
108+ print ("Gist updated!" )
109+ print ("Web view:" , gist .html_url )
110+ print ("Raw file:" , gist .files [FILENAME_MATCH ].raw_url )
60111
61- print ("Gist created!" )
62- print ("Web view:" , gist .html_url )
63- print ("Raw file:" , gist .files [filename ].raw_url )
64- return ()
112+ return
0 commit comments