Created
October 19, 2019 10:45
-
-
Save ahmedash95/1ca41a5add4b3446867bdfbd9041c671 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func TweetScreenShot(username string, tweetId string) (string, error) { | |
chromedpContext, cancelCtxt := chromedp.NewContext(context.Background()) // create new tab | |
defer cancelCtxt() | |
// capture screenShot of an element | |
fname := fmt.Sprintf("%s-%s.png", username, tweetId) | |
url := fmt.Sprintf("https://twitter.com/%s/status/%s", username, tweetId) | |
var buf []byte | |
if err := chromedp.Run(chromedpContext, elementScreenshot(url, `document.querySelector("#permalink-overlay-dialog > div.PermalinkOverlay-content > div > div > div.permalink.light-inline-actions.stream-uncapped.original-permalink-page > div.permalink-inner.permalink-tweet-container > div")`, &buf)); err != nil { | |
return "", err | |
} | |
fmt.Printf("write pic to path %s\n", fmt.Sprintf("%s/%s", PIC_STORAGE_PATH, fname)) | |
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s", PIC_STORAGE_PATH, fname), buf, 0755); err != nil { | |
return "", err | |
} | |
return fname, nil | |
} | |
// elementScreenshot takes a screenshot of a specific element. | |
func elementScreenshot(urlstr, sel string, res *[]byte) chromedp.Tasks { | |
return chromedp.Tasks{ | |
chromedp.Navigate(urlstr), | |
chromedp.WaitVisible(sel, chromedp.ByJSPath), | |
chromedp.Sleep(time.Second * 3), | |
chromedp.Screenshot(sel, res, chromedp.NodeVisible, chromedp.ByJSPath), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment