Just a blog to share my tricks, code snippets

Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, November 9, 2017

An example of Github Rest API using Python


Suppose I want a python script to get some information of Customer IO's Github:
  • How many total open issues are there across all repositories?
  • Sort the repositories by date updated in descending order.
  • Which repository has the most watchers?

Github has an official REST API v3 document at HERE
Here are some notes before jumping into the script. They are all stated in Github API's document but I just want to make sure you are aware of:
  • For GET request, parameters are passed in the HTTP query string
curl "https://api.github.com/repos/customerio/esdb/subscribers?page=2"
In above example, the parameter page is passed in the query string. For PUT, POST, PATCH, DELETE requests, parameters are passed in the data of the request instead of query string. See the official document for more details
  • Requests that return multiple items will be paginated to 30 items by default. Information of pagination is in the response's header.
curl -i "https://api.github.com/repos/customerio/esdb/subscribers"
You will see the header will has a Link. next indicates the next page and last indicates the number of the last page
Link: <https: api.github.com="" page="2" repositories="" subscribers="">; rel="next", <https: api.github.com="" page="2" repositories="" subscribers="">; rel="last"       
  • You do not need to authenticate your requests. However, the rate limit for unauthenticated requests is only 60 requests per hour. Therefore, it is better to authenticate requests. For requests using Basic Authentication or OAuth, you can make up to 5000 requests per hour. First, you need to create an access token. Github has a guide at HERE
curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/repos/customerio/esdb/subscribers       

Now it is the full script

Friday, September 22, 2017

Write a simple zip file password cracker with Python


In order to extract a zip file, Python provides a zipfile library which help us create, read write, append and list a zip file. However, It only supports ZipCrypto encryption. A complete guide about it is HERE

So the idea is:
- Read a file pw.txt that contains all passwords.
- Try to extract the file for each password
Here is the script:

Note: The zipfile library only support ZipCrypto encryption. That means when you use 7zip to archive a file, remember to use the ZipCrypto encryption method instead of AES-256 one

Thursday, August 31, 2017

Python: Some restrictions when using multiprocessing library on windows


I ran into one of these restrictions today and want to share it. Those restrictions are stated in multiprocessing library document at HERE . However, I want to explain them more and give some examples so we can understand them fully.

Restriction 1: Safe importing of main module


  • Problem:


Running the script below would fail with a RuntimeError

The error should be like this


  • Solution:


In the python document above, it states:
Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).
That means we need to put the all the commands inside a if __name__ == '__main__': block. That will prevent calling a new process (could be unintended) when import that module.
The code should be look like this

Restriction 2: More picklability

Restriction 3: Global variables

Details of o other restrictions will be updated later whenever I have a chance :)

Wednesday, March 8, 2017

Python: get Hipchat 1-1 history chat


Group chat history is only exported by users with admin permission only. However, a normal user can export his 1-1 history chat via Hipchat API.
Below is a simple script to export 1-1 history chat.

How to generate your hipchat access token: An access token is used in place of user and password parameters.
  • Login to your hipchat account on a web browser
  • Go to Account Setting by clicking on your person icon in the upper, right corner.
  • Select API Access.
  • Generate a token with suitable scope (Select "View Messages" in this case since we want to get the history chat)


How to the user ID: User ID is the the ID of the user that you want to get the history chat.

  • Click to open chat with the user
  • The user ID is from the URL


For more information about the API, you can access this Hipchat API page

Saturday, March 4, 2017